
Another early start. Too early, as the puzzle description took several goes of re-reading before I could understand what it was asking me, and once I read Part 2 my brain immediately went down the routes of combinatorics (have you seen the input? hah!) and general algorithms to maximise the distance travelled between the nodes in a graph.
Wrong, brain, wrong. There’s a much simpler approach – the longest step will always be to a square of the same colour, a fact that was mentioned in the puzzle text and I completely failed to register until hours later. Just group the colours together, start with red, and when you run out of one colour, pick the one that costs the most to get to next.
fn part2(data: &str, waypoints: &[char]) -> usize { let mut unique_instructions = data.chars().collect::<HashSet<_>>(); let mut pointer = (data.len() - unique_instructions.len()) * waypoints.len(); while !unique_instructions.is_empty() { let mut winning_instr = None; let mut best_move_result = pointer; for candidate in &unique_instructions { let mut possible_pointer = pointer +1; while waypoints[possible_pointer % waypoints.len()] != *candidate { possible_pointer +=1; } if possible_pointer > best_move_result { best_move_result = Some(possible_pointer); winning_instr = *candidate; } } if let Some(best_found) = best_move_result { pointer = best_found; unique_instructions.remove(&winning_instr); } else { panic!("No instruction gives a move. This is impossible.") } } pointer}
Admittedly, this is somewhat more of a general solution than the absolute fastest.
As for the Apprentice, they read the description and, brow furrowed, explained their idea – to create an enormously long list of waypoints and then track his way along. “I don’t think this will work,” they explained, starting with the aforementioned ridiculously long string and
waypoints = "roygbvroygbvroygbvroygbvroygbvroygbvroygbv...."waypoints = list(waypoints)data = "obbbv"for char in data: while True: current_waypoint = waypoints.pop(0) distance +=1 if waypoint == char: breakprint(distance)
“This won’t work for really big inputs though, I’ll run out of characters and my program will be one massive string”. Cue a lesson which prompted amazement – “You can multiply strings by numbers!?” Try it, I suggested. Funnily enough, the program ran for a long time and eventually crashed.
Dropping a hint, I asked: “Can you think of a differente way that doesn’t involve storing huge amounts of data?” Again, the answer surprised me. “Can I push the letter back on the end of the list?” Cue an explanation of the difference between a Deque and a List and why we might want to use one over the other.
Part two was equally mind bending. The Apprentice grasped the idea for the solution far quicker than I had after a couple of questions around the distance covered by R, RR and RRR, and B and BB. They came up with a theory – number of letters in the input multiplied by six, plus 15 (5+4+3+2+1). Funnily enough, the test cases failed, each being close but not quite right.
After much back and forth we decided that the multiply by six was correct, we just needed to account for the jumps between letters. Cue the suggestion of a lookup table which the Apprentice translated into the world’s longest nested if statement. This was a massive mistake and just resulted in non-debuggable spaghetti.
End of the session: too many bugs. But this will be an easy second star when we come back to it.
Leave a Reply