AoC2021/day02/solution.rb

37 lines
515 B
Ruby
Raw Normal View History

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