Skip to content

Commit

Permalink
Restore 2023-01 part 1 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
bcc32 committed Dec 2, 2023
1 parent 8e737b8 commit fb2840f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
40 changes: 4 additions & 36 deletions 2023/01/sol1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,7 @@
f.readlines.map(&:chomp)
end

pat = /\d|one|two|three|four|five|six|seven|eight|nine/

def dig(s)
case s
when 'one'
'1'
when 'two'
'2'
when 'three'
'3'
when 'four'
'4'
when 'five'
'5'
when 'six'
'6'
when 'seven'
'7'
when 'eight'
'8'
when 'nine'
'9'
else
s
end
end

x = input.map do |s|
matches = s.size.times.map do |i|
s.match(pat, i)
end.compact

(dig(matches.first.to_s) + dig(matches.last.to_s)).to_i
end.sum

p x
p (input.map do |s|
a = s.scan(/\d/)
(a.first + a.last).to_i
end.sum)
41 changes: 41 additions & 0 deletions 2023/01/sol2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
$stdout = File.open('aoc.out', 'w')
input = File.open('aoc.in') do |f|
f.readlines.map(&:chomp)
end

pat = /\d|one|two|three|four|five|six|seven|eight|nine/

def dig(s)
case s
when 'one'
'1'
when 'two'
'2'
when 'three'
'3'
when 'four'
'4'
when 'five'
'5'
when 'six'
'6'
when 'seven'
'7'
when 'eight'
'8'
when 'nine'
'9'
else
s
end
end

x = input.map do |s|
matches = s.size.times.map do |i|
s.match(pat, i)
end.compact

(dig(matches.first.to_s) + dig(matches.last.to_s)).to_i
end.sum

p x

0 comments on commit fb2840f

Please sign in to comment.