r/adventofcode • • 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

Click here for full rules

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 :'(


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

EDIT: Leaderboard capped, thread unlocked at 00:27:59!

15 Upvotes

162 comments sorted by

View all comments

Show parent comment

1

u/e_blake Dec 19 '19

For starters, as I get further along the beam, the numbers get larger, and the 32-bit limits of m4 kick in; my intcode.m4 engine calls out to /bin/sh for operations that look like they would exceed 32 bits (add and less than: either argument > 10 chars; mul: sum of argument lengths > 10 chars). For my puzzle input, I traced 13963 fork() calls to the shell, with many of them being easy textual operations (such as 490730625 * -1) that would be more efficient without a fork. Another idea: instead of tracing the edge exactly, approximate the edge based on what I learned in part 1 (for example, compute the rough slope of the line between y=10 and y=49), then perform a binary search with progressively finer granularity

1

u/e_blake Dec 20 '19

The following addition of a course loop cut things from 56 seconds down to 7, and was easier to figure out than a binary search :)

@@ -34,13 +34,20 @@ define(`write', `ifelse($1, 0, `pushdef(`r', x)define(`y', incr(y))',
 define(`loop', `ifelse(y, 50, `', `ifelse(x, 50,
   `pushdef(`r', x)define(`y', incr(y))', `try(x, y)')loop()')')
 loop()
+define(`coursex', eval(l - tipx))
+define(`coursey', eval(y - tipy))
 forloop_arg(tipy, 49, `define(`part1',
   eval(part1 + r - l))popdef(`l', `r')done')

 # Sweep left edge, and check diagonal of square
-define(`x', tipx)
+define(`x', eval(tipx + 99 / coursex))
 define(`y', eval(tipy + 99))
 define(`write', `define(`data', $1)')
+define(`loop', `try(eval(x + coursex), eval(y + coursey))ifelse(data, 1,
+  `try(eval(x + coursex + 99), eval(y + coursey - 99))ifelse(data, 1,
+  `oneshot(`loop')', `define(`y', eval(y + coursey))define(`x',
+  eval(x + coursex))')', `define(`x', incr(x))')loop()')
+loop()
 define(`loop', `try(x, y)ifelse(data, 1, `try(eval(x + 99),
   eval(y - 99))ifelse(data, 1, `oneshot(`loop')', `define(`y', incr(y))')',
   `define(`x', incr(x))')loop()')