-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.rb
34 lines (32 loc) · 859 Bytes
/
day01.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# https://adventofcode.com/2023/day/01
module Advent
module Year2023
class Day01 < Advent::Challenge
def part1
pattern = /\d/
input_lines
.map { |line| line.scan pattern }
.map { |matches| [matches.first, matches.last].join.to_i }
.sum
end
def part2
pattern = /(?=(\d|one|two|three|four|five|six|seven|eight|nine))/
input_lines
.map { |line| line.scan(pattern).flatten }
.map { |matches|
nums = matches.map { |n| replace_num(n) }
[nums.first, nums.last].join.to_i
}
.sum
end
def replace_num(num)
names = %w[zero one two three four five six seven eight nine]
if names.include? num
names.index(num).to_s
else
num
end
end
end
end
end