AoC2021/day02/solution.rb

33 lines
557 B
Ruby
Raw Normal View History

2021-12-02 12:39:40 +00:00
#!/bin/env ruby
commands = File.readlines('./input').map(&:split).map{ [_1.to_sym,_2.to_i] }
# Part 1
dist, depth = 0,0
commands.each do |command, unit|
case command
when :forward
dist += unit
when :down
depth += unit
when :up
depth -= unit
end
end
p dist * depth
# Part 2
dist, depth, aim = 0,0,0
commands.each do |command, unit|
case command
when :forward
depth += aim * unit
dist += unit
when :down
aim += unit
when :up
aim -= unit
end
end
p dist * depth