Skip to content

Instantly share code, notes, and snippets.

@madebydna
Created December 2, 2021 05:36
Show Gist options
  • Save madebydna/a60116ac452dc8734093f649709bb217 to your computer and use it in GitHub Desktop.
Save madebydna/a60116ac452dc8734093f649709bb217 to your computer and use it in GitHub Desktop.
Advent of Code, Day 2
instructions = File.readlines("input_d2.txt").map(&:chomp!)
x, y = 0, 0
instructions.each do |cmd|
direction, amt = cmd.match(/(forward|up|down) (\d+)/)[1..-1]
case direction
when "forward"
x += amt.to_i
when "up"
y -= amt.to_i
when "down"
y += amt.to_i
end
end
puts "Part 1: #{x * y}"
x, y, aim = 0, 0, 0
instructions.each do |cmd|
direction, amt = cmd.match(/(forward|up|down) (\d+)/)[1..-1]
amt = amt.to_i
case direction
when "forward"
x += amt
y += aim * amt
when "up"
aim -= amt
when "down"
aim += amt
end
end
puts "Part 2: #{x * y}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment