changeset 11:a7fb64b48830

Implement Day 8 - node/map walking Cheated on part 2 by finding out that the routes loop and you need to do the lowest-common-multiple to get a VERY large number
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Dec 2023 16:37:20 +0000
parents 25dda3397797
children 891878e52a31
files day8.rb day8.txt day8b.rb
diffstat 3 files changed, 131 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day8.rb	Sat Dec 09 16:37:20 2023 +0000
@@ -0,0 +1,30 @@
+#! /usr/bin/env ruby
+
+if ARGV.length != 1
+        abort("Incorrect arguments - needs input file")
+elsif not File.exist? (ARGV[0])
+	abort("File #{ARGV[0]} did not exist")
+end
+
+file = ARGV[0]
+
+lines = File.open(file, "r").each_line(chomp: true)
+
+steps = lines.first.each_char.to_a.cycle
+
+node_lines = lines.drop(1).to_a
+
+#first_node, _ = node_lines[0].split("=")
+#first_node.strip!
+
+nodes = node_lines.map {|line| node, rest = line.split("="); left, right = rest.split(","); [node.strip, {"L" => left.strip[1..-1], "R" => right.strip[0..-2]}]}.to_h
+
+cur_node = "AAA"
+step_count = 0
+
+while cur_node != "ZZZ"
+	step_count += 1
+	cur_node = nodes[cur_node][steps.next]
+end
+
+puts step_count
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day8.txt	Sat Dec 09 16:37:20 2023 +0000
@@ -0,0 +1,68 @@
+--- Day 8: Haunted Wasteland ---
+
+You're still riding a camel across Desert Island when you spot a sandstorm quickly approaching. When you turn to warn the Elf, she disappears before your eyes! To be fair, she had just finished warning you about ghosts a few minutes ago.
+
+One of the camel's pouches is labeled "maps" - sure enough, it's full of documents (your puzzle input) about how to navigate the desert. At least, you're pretty sure that's what they are; one of the documents contains a list of left/right instructions, and the rest of the documents seem to describe some kind of network of labeled nodes.
+
+It seems like you're meant to use the left/right instructions to navigate the network. Perhaps if you have the camel follow the same instructions, you can escape the haunted wasteland!
+
+After examining the maps for a bit, two nodes stick out: AAA and ZZZ. You feel like AAA is where you are now, and you have to follow the left/right instructions until you reach ZZZ.
+
+This format defines each node of the network individually. For example:
+
+RL
+
+AAA = (BBB, CCC)
+BBB = (DDD, EEE)
+CCC = (ZZZ, GGG)
+DDD = (DDD, DDD)
+EEE = (EEE, EEE)
+GGG = (GGG, GGG)
+ZZZ = (ZZZ, ZZZ)
+
+Starting with AAA, you need to look up the next element based on the next left/right instruction in your input. In this example, start with AAA and go right (R) by choosing the right element of AAA, CCC. Then, L means to choose the left element of CCC, ZZZ. By following the left/right instructions, you reach ZZZ in 2 steps.
+
+Of course, you might not find ZZZ right away. If you run out of left/right instructions, repeat the whole sequence of instructions as necessary: RL really means RLRLRLRLRLRLRLRL... and so on. For example, here is a situation that takes 6 steps to reach ZZZ:
+
+LLR
+
+AAA = (BBB, BBB)
+BBB = (AAA, ZZZ)
+ZZZ = (ZZZ, ZZZ)
+
+Starting at AAA, follow the left/right instructions. How many steps are required to reach ZZZ?
+
+--- Part Two ---
+
+The sandstorm is upon you and you aren't any closer to escaping the wasteland. You had the camel follow the instructions, but you've barely left your starting position. It's going to take significantly more steps to escape!
+
+What if the map isn't for people - what if the map is for ghosts? Are ghosts even bound by the laws of spacetime? Only one way to find out.
+
+After examining the maps a bit longer, your attention is drawn to a curious fact: the number of nodes with names ending in A is equal to the number ending in Z! If you were a ghost, you'd probably just start at every node that ends with A and follow all of the paths at the same time until they all simultaneously end up at nodes that end with Z.
+
+For example:
+
+LR
+
+11A = (11B, XXX)
+11B = (XXX, 11Z)
+11Z = (11B, XXX)
+22A = (22B, XXX)
+22B = (22C, 22C)
+22C = (22Z, 22Z)
+22Z = (22B, 22B)
+XXX = (XXX, XXX)
+
+Here, there are two starting nodes, 11A and 22A (because they both end with A). As you follow each left/right instruction, use that instruction to simultaneously navigate away from both nodes you're currently on. Repeat this process until all of the nodes you're currently on end with Z. (If only some of the nodes you're on end with Z, they act like any other node and you continue as normal.) In this example, you would proceed as follows:
+
+    Step 0: You are at 11A and 22A.
+    Step 1: You choose all of the left paths, leading you to 11B and 22B.
+    Step 2: You choose all of the right paths, leading you to 11Z and 22C.
+    Step 3: You choose all of the left paths, leading you to 11B and 22Z.
+    Step 4: You choose all of the right paths, leading you to 11Z and 22B.
+    Step 5: You choose all of the left paths, leading you to 11B and 22C.
+    Step 6: You choose all of the right paths, leading you to 11Z and 22Z.
+
+So, in this example, you end up entirely on nodes that end in Z after 6 steps.
+
+Simultaneously start on every node that ends with A. How many steps does it take before you're only on nodes that end with Z?
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day8b.rb	Sat Dec 09 16:37:20 2023 +0000
@@ -0,0 +1,33 @@
+#! /usr/bin/env ruby
+
+if ARGV.length != 1
+        abort("Incorrect arguments - needs input file")
+elsif not File.exist? (ARGV[0])
+	abort("File #{ARGV[0]} did not exist")
+end
+
+file = ARGV[0]
+
+lines = File.open(file, "r").each_line(chomp: true)
+
+steps = lines.first.each_char.to_a
+
+node_lines = lines.drop(1).to_a
+
+#first_node, _ = node_lines[0].split("=")
+#first_node.strip!
+
+nodes = node_lines.map {|line| node, rest = line.split("="); left, right = rest.split(","); [node.strip, {"L" => left.strip[1..-1], "R" => right.strip[0..-2]}]}.to_h
+
+cur_nodes = nodes.keys.filter {|v| v[-1] == "A"}
+
+step_counts = cur_nodes.map do |cur_node| 
+	step_count = 0
+	step_cycle = steps.cycle
+	while cur_node[-1] != "Z"
+		step_count += 1
+		cur_node = nodes[cur_node][step_cycle.next]
+	end
+	step_count
+end
+puts step_counts.reduce(1, :lcm)
\ No newline at end of file