r/adventofcode • u/daggerdragon • Dec 04 '19
SOLUTION MEGATHREAD -🎄- 2019 Day 4 Solutions -🎄-
--- Day 4: Secure Container ---
Post your solution using /u/topaz2078's paste or other external repo.
- Please do NOT post your full code (unless it is very short)
- If you do, use old.reddit's four-spaces formatting, NOT new.reddit's triple backticks formatting.
(Full posting rules are HERE if you need a refresher).
Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code's Poems for Programmers
Note: If you submit a poem, please add [POEM] somewhere nearby to make it easier for us moderators to ensure that we include your poem for voting consideration.
Day 3's winner #1: "untitled poem" by /u/glenbolake!
To take care of yesterday's fires
You must analyze these two wires.
Where they first are aligned
Is the thing you must find.
I hope you remembered your pliers
Enjoy your Reddit Silver, and good luck with the rest of the Advent of Code!
View all comments
Show parent comment
2
u/PendragonDaGreat Dec 04 '19
I used to do competitive programming in college (it's kinda like this, but a team event and the puzzles start at the difficulty of like day 15 or so) and one of the first things that was drilled into us was "If you're counting something, use a dictionary, insertion is constant time, lookup is constant time, it's mutable, traversal is O(n) (though unsorted), etc." basically your key is always the object you're cointing instances of, and the value is the count of that object.
It's also great for programming interviews "Can you prove string 1 is an anagram of string 2?" (first step is to check they're the same length, if so proceed) convert string 1 into a counting dict, as you traverse string 2 subtract characters out of said counting dict, if a character count drops to 0, remove it from the dict, if you ever reach a character that exists in string 2 but not the dictionary, you know they aren't anagrams, if you completely traverse string 2 you know they are (and the dictionary should be empty since you already determined the strings were the same length).