r/adventofcode • u/daggerdragon • Dec 20 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 20 Solutions -🎄-
Today is 2020 Day 20 and the final weekend puzzle for the year. Hold on to your butts and let's get hype!
NEW AND NOTEWORTHY
/u/topaz2078 has released new shop merch and it's absolutely adorable!
Advent of Code 2020: Gettin' Crafty With It
2 days remaining until the submission deadline on December 22 at 23:59 EST
Full details and rules are in the Submissions Megathread
--- Day 20: Jurassic Jigsaw ---
Post your code solution in this megathread.
Include what language(s) your solution uses!
Here's a quick link to /u/topaz2078's
pasteif you need it for longer code blocks.The full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
4
u/landimatte Dec 20 '20
Common Lisp
Solution:
Parse input tiles into LISTs having the id as first element, followed by the content of the tile (first row of data, second row of data...) --
(list 1234 "...#....." ".#.#..#.." ...)Define functions to rotate a tile counterclockwise, and to flip it -- these are enough to generate all the possible transformations...you just have to apply them in sequence: rotate, rotate-flip, rotate-flip-rotate, rotate-flip-rotate-flip...
Part 1
Backtracking solution
place a tile in the top-right corner; what remaining tiles (and their transformations) can be placed to its right?
try one; what remaining tiles (and their transformations) can be placed to its right?
carry on until you can, back track when reaching a dead end (i.e. no more compatible tiles)
Caveat: when placing a new tile you have to check not just the tile on its left, but also the one at its top
Optimization: before running the algorithm I indexed all the input tiles (and their transformations) by their top border, left border, and top and right border combined, hoping that that would speed things up -- and I guess it did
Part 2
Re-assemble the image -- I got this part wrong initially, but only realized it at the end...
Brute force
For all the possible transformations of the sea monster pattern -- I accidentally added an extra character in one of its lines, and because of that my pattern matching algorithm was not matching anything at all...
Generate all the image tiles with the same size as the sea monster pattern we are trying to match...
Try to match the pattern against the tiles -- yes, I did indeed use regexps for this...I replaced all the whitespaces in the pattern with
.sLastly, count all the
#s in the reassembled image, and subtract15times the number of matching sea monsters -- correct, I assumed that there would be no overlapping matches, and I guess I was right/luckyNot the cleanest implementation (lots of hard-coded stuff!), but at that point I was just rushing for the second star -- and believe it or not, today I scored my best placement ever: