Skip to content

Instantly share code, notes, and snippets.

@carlwiedemann
Created December 11, 2023 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlwiedemann/0c4cee5f0641f771853798f0e0c4c52e to your computer and use it in GitHub Desktop.
Save carlwiedemann/0c4cee5f0641f771853798f0e0c4c52e to your computer and use it in GitHub Desktop.
Advent of Code 2023 day008.rb
require_relative "main"
module Day008
INPUT = File.read('INPUT.txt')
lines = INPUT.to_lines
instructions = lines.shift.chars
lines.shift
graph = lines.map do |line|
parent, children_string = line.split_strip("=")
children = children_string.split_strip(",").map(&:to_an)
[parent, children]
end.to_h
SIDE = %w[L R]
##########
# Part 1 #
##########
cursor1 = "AAA"
i = 0
loop do
side = SIDE.index(instructions[i % instructions.length])
cursor1 = graph[cursor1][side]
break if cursor1 == "ZZZ"
i += 1
end
answer1 = i + 1
pp answer1
##########
# Part 2 #
##########
cursors = graph.keys.filter do |parent|
parent[-1] == "A"
end
sums = cursors.map do |cursor2|
i = 0
loop do
side = SIDE.index(instructions[i % instructions.length])
cursor2 = graph[cursor2][side]
break if cursor2[-1] == "Z"
i += 1
end
i + 1
end
answer2 = sums.lcm
pp answer2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment