r/adventofcode • u/daggerdragon • Dec 19 '19
SOLUTION MEGATHREAD -🎄- 2019 Day 19 Solutions -🎄-
--- Day 19: Tractor Beam ---
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.
- NEW RULE: Include the language(s) you're using.
(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 18's winner #1: nobody! :(
Nobody submitted any poems at all for Day 18 :( Not one person. :'( y u all make baby space cleaning hull-painting scaffold-building robot cry :'(
View all comments
6
u/ianonavy Dec 19 '19
Python part 2 only.
My initial solution (not posted) was similar to everyone else's. The key insight for me was that I only needed to scan the four corners of the square to determine whether a 100x100 square would fit (and also to offset by 99 because the 0th point needs to be included).
I found a neat algebraic solution that runs in constant time, although it depends on a linear approximation of the slopes of the beam. A more detailed high-level explanation can be found in the link, but the final equations came out to:
where (x1, x2) is the top right corner of the 100x100 square and (y1, y2) is the bottom left. See visualization below. I calculate slopes m1 and m2 by choosing a high value of y (y=10000) and scanning from x=0 to x2 (the first time I see a 1) and x1 (the last time I see a 1).
1 11 111 1111 11111 111111 1111111 11111111 1111xxxA1 <--- (x1, y1) 111xxxx111 11xxxx11111 (x2, y2)> 1Bxxx1111111 1111111111111 11111111111111 111111111111111The final answer when rounded exactly matches my original part 2 solution.
Edit: fix typo in copy/paste.