Vigyata.AI
Is this your channel?

ADVENT OF CODE | Day 5 Solution using Hash Maps

3.3K views· 164 likes· 5:11· Dec 18, 2024

Subscribe for more content like this :) 0:00 coding challenge 0:59 PART 1 solution 3:23 PART 2 discussion #adventofcode #vlogmas #programming

About This Video

In this Advent of Code Day 5 video, I’m working through the “printer at the North Pole” problem where the Sleigh Launch Safety Manual pages have to be printed in a specific order. The input gives you ordering rules like “X | Y,” which means page X must come before page Y, plus a bunch of page sequences to validate. For Part 1, the task is to check which sequences follow the rules, take the middle page number from each valid sequence, and sum those middle values. I solve Part 1 using hash maps because I really wanted constant-time lookups while checking ordering constraints. I build a nested hashmap where I store relationships both ways (what comes before and what comes after). Then, for each sequence, I validate it by ensuring pages before the current page have a “before” relationship, and pages after have an “after” relationship. Once a sequence is valid, I grab the middle element using integer division by two and add it to the total. I didn’t have time to implement Part 2, but I talk through how I’d start: I’d model the rules as a directed graph and keep an additional hashmap tracking how many elements must come before/after each node (basically the buffer each page needs). From there, I’d try sorting/using the graph to see whether invalid sequences can be reordered to become compliant.

Frequently Asked Questions

🎬 More from CS Jackie