Created
December 2, 2021 16:25
-
-
Save colintsteele/ddf96f8950f844f60e50eb056ec9b8f7 to your computer and use it in GitHub Desktop.
AoC#2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pinput = <<-PUZ | |
# your input goes here | |
PUZ | |
sub = Submarine.new(pinput.split("\n")) | |
sub.go_brr | |
sub.where | |
class Submarine | |
attr_reader :aim, :horizontal_pos, :vertical_pos, :movements | |
def initialize(puzzle_input) | |
@horizontal_pos = 0 | |
@vertical_pos = 0 | |
@aim = 0 | |
@movements = puzzle_input | |
end | |
def where | |
@horizontal_pos * @vertical_pos | |
end | |
def go_brr | |
@movements.each do |movement| | |
(/(?<direction>\w+) (?<far>\d)/).match(movement) do |go| | |
self.send("move_#{go[:direction]}", go[:far].to_i) | |
end | |
end | |
end | |
private | |
def move_forward(x) | |
@horizontal_pos += x | |
@vertical_pos += (@aim * x) | |
end | |
def move_up(x) | |
@aim -= x | |
end | |
def move_down(x) | |
@aim += x | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment