r/adventofcode • • Dec 14 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 14 Solutions -🎄-

--- Day 14: Chocolate Charts ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The 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: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 14

Transcript:

The Christmas/Advent Research & Development (C.A.R.D.) department at AoC, Inc. just published a new white paper on ___.


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:19:39!

16 Upvotes

179 comments sorted by

View all comments

Show parent comment

3

u/spytheman66 Dec 14 '18

I ended up implementing batching - checking for loop termination just once per 1000 iterations.
This reduced my PHP solution runtime from ~33s (checking after each new recipe generation) to ~17s:

#!/usr/bin/env php
<?php 
include("common.php");
$n = (int) join('', read_input());
$c = 10; [$e0,$e1]=[0,1]; $s = "37"; $ls=strlen($s);
while(true){
    $a = (int) $s[ $e0 ]; 
    $b = (int) $s[ $e1 ];
    $sum = $a + $b; $s .= $sum; $ls += strlen("{$sum}");
    $e0 = ( $e0 + $a + 1 ) % $ls ; 
    $e1 = ( $e1 + $b + 1 ) % $ls ;
    if ( $ls > $n + $c ) break;
}
printf("Part 1 answer (next {$c} recipes) is: %s\n", substr($s, $n, $c));

[$e0,$e1]=[0,1]; $s = "37"; $ls=strlen($s); $sn = "{$n}"; $nsn = 2*strlen($sn); $batchSize=1000; $batchNSN = $batchSize+$nsn;
$i=0;while(true){
    $a = (int) $s[ $e0 ]; 
    $b = (int) $s[ $e1 ];
    $sum = $a + $b; $s .= $sum; $ls += strlen("{$sum}");
    $e0 = ( $e0 + $a + 1 ) % $ls ; 
    $e1 = ( $e1 + $b + 1 ) % $ls ;
    if(0===$i % $batchSize){
        if(0===$i % 1000000) printf("Iteration: %10d, ls: %10d\n", $i, $ls);
        if(strpos($slast=substr($s, $ls-$batchNSN), $sn)!==false) break;
    }
    $i++;
}
printf("Part 2 answer (n recipes before input appear) is: %d\n", strpos($s, $sn));

2

u/xthexder Dec 14 '18

Yeah, I agree; checking periodically is the next logical step. Makes the solution more general without sacrificing perf.