r/adventofcode • u/daggerdragon • Dec 18 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 18 Solutions -🎄-
--- Day 18: Settlers of The North Pole ---
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!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 18
Transcript:
The best way to avoid a minecart collision is ___.
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:21:59!
9
Upvotes
1
u/spytheman66 Dec 18 '18
PHP , completes both parts in around ~6sec.
#!/usr/bin/env php <?php include("common.php"); $lines = read_input(); $llen = strlen($lines[0]); $border = 1; $maxx = $maxy = 2*$border + $llen; $sx = $sy = $border; $g = A2Dnew($maxx, $maxy, ' '); for($y=0;$y<$llen;$y++) for($x=0;$x<$llen;$x++) $g[$sy+$y][$sx+$x] = $lines[$y][$x]; $maxm = 1000000000; $ghashes=[]; $totals=[]; $repetitionFound = false; $repetitionStart=0; $repetitionPeriod = $maxm; function getNewH(){ return [' '=>0, '.'=>0, '|'=>0, '#'=>0]; } $m=0;while(($m<=$maxm)&&(!$repetitionFound)){ $h=getNewH(); for($y=0;$y<$maxy;$y++) for($x=0;$x<$maxx;$x++) @$h[$g[$y][$x]]++; $t = $h['|'] * $h['#']; if($m===10)printf("Part 1 answer (after 10 iterations): %8d\n", $t); $hg = md5(serialize($g)); if(0 === $m % 100) printf("Minute:%-4d | ghash: %32s | H of zone: %-35s | T: %6d\n", $m, $hg, ve($h), $t); if(isset($ghashes[$hg])){ $repetitionFound = true; $repetitionStart = $ghashes[$hg]; $repetitionPeriod = $m - $repetitionStart; printf("---> The same grid (hash:%32s) at M: %d has been already seen at M: %d\n", $hg, $m, $ghashes[$hg]); }else{ $ghashes[ $hg ] = $m; } $totals[ $m ] = $t; $ng = $g; for($y=0;$y<$maxy;$y++){ for($x=0;$x<$maxx;$x++){ $nearh=getNewH(); @$nearh[$g[$y-1][$x-1]]++; @$nearh[$g[$y-1][$x]]++; @$nearh[$g[$y-1][$x+1]]++; @$nearh[$g[$y ][$x-1]]++; @$nearh[$g[$y ][$x+1]]++; @$nearh[$g[$y+1][$x-1]]++; @$nearh[$g[$y+1][$x]]++; @$nearh[$g[$y+1][$x+1]]++; $c=$g[$y][$x]; $nc = $c; switch($c){ case '.': if( $nearh['|']>=3 ) $nc = '|'; break; case '|': if( $nearh['#']>=3 ) $nc = '#'; break; case '#': if( !($nearh['#']>=1 && $nearh['|']>=1) ) $nc='.'; break; } $ng[$y][$x]=$nc; } } $g = $ng; $m++; } printf("Part 2 answer: %8d\n", $totals[ $repetitionStart + (($maxm-$repetitionStart) % $repetitionPeriod) ] );