Resolve day 1

main
Guillaume Dott 2021-11-21 17:50:40 +01:00
commit a0754de631
3 changed files with 36 additions and 0 deletions

1
1/input 100644

File diff suppressed because one or more lines are too long

24
1/script.rb 100755
View File

@ -0,0 +1,24 @@
#!/usr/bin/env ruby
require_relative '../common'
def part1
input = Input.new(__dir__).readlines.join
input.count('(') - input.count(')')
end
def part2
input = Input.new(__dir__).readlines.join
input.chars.each_with_index.inject(0) do |floor, (char, i)|
return i if floor < 0
char == '(' ? floor + 1 : floor - 1
end
nil
end
puts "=== Part 1 ==="
puts part1
puts "=== Part 2 ==="
puts part2

11
common.rb 100644
View File

@ -0,0 +1,11 @@
class Input
FILE = 'input'
def initialize(directory, file = nil)
@filename = "#{directory}/#{file || FILE}"
end
def readlines
File.readlines(@filename).reject { |line| line.start_with?('#') }
end
end