Advent of Code 2024 Day 10
Part 1
Brief terror, then excitement
I usually skim a page before reading everything closely.
Today, I saw:
- A grid
- and what looked like paths
And I feared this would be another shortest path challenge.
Then I read it.
And breathed a sigh of relief...at least for Part 1.
I need to find all valid paths.
That...I can do!
Start from 0
I have to find all the 0s:
input = input .split('\n') .map( line => line.split('').map(char => +char) ) let zeros = [] for (let r = 0; r <p>0s: found!</p> <h4> Attempt to move in increments of 1 </h4> <p>From each 0, a valid path is nine steps where each number is one greater than the last, ending at 9.</p> <p>This sounds like a job for recursion.</p> <p>I need a base case:</p> <ol> <li>Current number is not exactly one greater than the previous</li> <li>Current number is 9</li> </ol> <p>Here's how my algorithm should work:<br> </p> <pre class="brush:php;toolbar:false">Input: 1. Originating number 2. Current coordinates Get current number If it is not exactly one greater than the originating number Return false Else If it is 9 Return the current coordinates If it is not 9 Continue with the coordinates in each orthogonal direction
Having now written it, the part I missed was tracking the ledger of valid end coordinates.
I struggled with this for some time.
I kept getting an error that wrongly made me think I couldn't pass in a Set or even an array.
But, thankfully, I just forgot to pass it into further calls of the recursive function.
Here's my working recursive algorithm:
let dirs = [[-1,0],[0,-1],[0,1],[1,0]] function pathFinder(num, coord, memo) { let current = input[coord[0]][coord[1]] if (current - num !== 1) { return false } else if (current == 9) { memo.add(coord.join(',')) return } else { dirs.forEach(dir => { if ( coord[0] + dir[0] >= 0 && coord[0] + dir[0] = 0 && coord[1] + dir[1] <p>Since I must start with the coordinates of 0s, my first call uses -1:<br> </p> <pre class="brush:php;toolbar:false">pathFinder(-1, zeroCoordinate, matches)
Lastly, to get the correct score, I iterate through each zero, generating the unique set of destination 9s, keep and sum up the sizes of the sets:
let part1 = zeros.map(z => { let matches = new Set() pathFinder(-1, z, matches) return matches.size }).reduce((a, c) => a + c)
Quick tests, quick results
It generated the correct answer for the small example input.
And for the larger example input.
And...
...for my puzzle input!!!
Woohoo!!!
What will Part 2 challenge me with?
Part 2
Umm, that seems too easy
Is it possible that the way I wrote my algorithm in Part 1 means this will only require a few small changes to get the correct answer?
Count 'em all!
Right now, I add each valid 9 to a set.
For Part 2, I think I just need to increment a counter for each valid 9.
Worth a try!
Change Set to Array and voila!
Correct answer for the example.
Correct answer for my puzzle input.
Wow. Wow. Wow.
On to the next day...which is likely to be much harder.
The above is the detailed content of Hoof It. For more information, please follow other related articles on the PHP Chinese website!

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

This tutorial demonstrates creating dynamic page boxes loaded via AJAX, enabling instant refresh without full page reloads. It leverages jQuery and JavaScript. Think of it as a custom Facebook-style content box loader. Key Concepts: AJAX and jQuery

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

This JavaScript library leverages the window.name property to manage session data without relying on cookies. It offers a robust solution for storing and retrieving session variables across browsers. The library provides three core methods: Session


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
