1
Fork 0

feat: Day 2

This commit is contained in:
Lucas Schwiderski 2021-12-09 09:19:27 +01:00
parent 6835506dea
commit a216d73e9e
3 changed files with 47 additions and 1 deletions

19
puzzles/02_1/main.rb Normal file
View file

@ -0,0 +1,19 @@
def run(f)
horizontal = 0
depth = 0
f.each do |line|
command, distance = line.split(' ')
distance = Integer(distance)
case command
when 'forward'
horizontal += distance
when 'down'
depth += distance
when 'up'
depth -= distance
end
end
return horizontal * depth
end

21
puzzles/02_2/main.rb Normal file
View file

@ -0,0 +1,21 @@
def run(f)
horizontal = 0
depth = 0
aim = 0
f.each do |line|
command, distance = line.split(' ')
distance = Integer(distance)
case command
when 'forward'
horizontal += distance
depth += (aim * distance)
when 'down'
aim += distance
when 'up'
aim -= distance
end
end
return horizontal * depth
end

8
run.rb
View file

@ -5,7 +5,13 @@ 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')
if part == '1'
f = File.open('./puzzles/%s_%s/input.txt' % [puzzle, part], 'r')
elsif File.exists?('./puzzles/%s_%s/input.txt' % [puzzle, part])
f = File.open('./puzzles/%s_%s/input.txt' % [puzzle, part], 'r')
else
f = File.open('./puzzles/%s_1/input.txt' % [puzzle, part], 'r')
end
res = run f
puts "Result: " + res.to_s