1
Fork 0

feat: Day 1

This commit is contained in:
Lucas Schwiderski 2021-12-09 09:01:34 +01:00
parent 730af0ae0f
commit 6835506dea
3 changed files with 66 additions and 0 deletions

16
puzzles/01_1/main.rb Normal file
View file

@ -0,0 +1,16 @@
def run(f)
increments = 0
previous = nil
f.each do |line|
measurement = Integer(line)
if previous and measurement > previous
increments += 1
end
previous = measurement
end
return increments
end

37
puzzles/01_2/main.rb Normal file
View file

@ -0,0 +1,37 @@
def shift(arr)
arr[0] = arr[1]
arr[1] = arr[2]
arr[2] = arr[3]
arr[3] = []
end
def push(arr, val)
shift(arr)
if arr[1]
arr[1] << val
end
if arr[2]
arr[2] << val
end
arr[3] << val
end
def run(f)
increments = 0
buffer = []
f.each do |line|
measurement = Integer(line)
push(buffer, measurement)
if buffer[0] and buffer[1] and buffer[0].length() == 3 and buffer[1].length() == 3 and buffer[1].sum() > buffer[0].sum()
increments += 1
end
end
return increments
end

13
run.rb Executable file
View file

@ -0,0 +1,13 @@
#!/usr/bin/env ruby
puzzle = (ARGV[0] ? ARGV[0] : '1').rjust(2, '0')
part = ARGV[1] ? ARGV[1] : '1'
require './puzzles/%s_%s/main.rb' % [puzzle, part]
f = File.open('./puzzles/%s_%s/input.txt' % [puzzle, part], 'r')
res = run f
puts "Result: " + res.to_s
f.close