lcm, rotate!, and modulo - Day 08 - Advent of Code 2023

preview_player
Показать описание
Learn how to solve day 8 of Advent of Code 2023 - The Haunted Wasteland - in this Ruby tutorial.

We'll cover:
Parsing the input data into directions and a graph representation
Navigating the graph by rotating through direction turns
Finding paths in the graph from starting to ending nodes
Handling Part 2 where paths start from multiple nodes
Using the modulo operator and lcm method to align ending points
Refactoring to allow re-use for both parts with a block

This solution demonstrates some elegant Ruby data structures and methods like rotate, lcm, and inject to concisely traverse a directed graph. The code walkthrough explains the initial thought process and a failed solution attempt before arriving at the final answer.

#adventofcode #ruby
Рекомендации по теме
Комментарии
Автор

wow, I never heard of 'lcm' before your video. Great idea.

lappi_wojak
Автор

I'm few days behind, but today (10th day) I solved 8th day puzzles. I also went to see on which step each "??A path" reach ??Z node. Thankfully each of them reach only one ??Z node on N step, and then after another N steps reach same ??Z node. I think that if first N was different from second N or if each "??A path" after reaching first ??Z node reached different ??Z nodes, it would be terribly difficult.

piotrtalarczyk
join shbcf.ru