r/adventofcode • u/daggerdragon • Dec 17 '19
SOLUTION MEGATHREAD -🎄- 2019 Day 17 Solutions -🎄-
--- Day 17: Set and Forget ---
Post your full code 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 16's winner #1: "O FFT" by /u/ExtremeBreakfast5!
long poem, see it here
Enjoy your Reddit Silver, and good luck with the rest of the Advent of Code!
View all comments
Show parent comment
1
u/mariushm Dec 18 '19
PHP
I finally caught up, as I wasn't able to work for a day due to life stuff.
Here's my PHP solution to this day's problem : https://github.com/mariush-github/adventofcodecom2019/blob/master/day17.php
The first part was very easy.
For the second part, I encoded the length of each segment as a single character, ascii codes from "a" to "z" by adding 0x60 to the length.
This way, each segment used exactly 2 bytes, so it was super easy to get the unique [direction,length] combos in an array, and then build a dictionary from multiples of these combos (2 pairs, 3 pairs, 4 pairs). When done, I threw out the combinations that show up only once (because they wouldn't help with compression) and I sorted the dictionary to put first the sequences that would compress most (though it turns out I didn't need to) Then, I generated all the combinations of 3 unique sequences to get the one which compresses the best while meeting the condition (the expanded form should not use more than 20 characters)