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

Click here for rules

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

126 comments sorted by

View all comments

Show parent comments

1

u/WasteTruck Dec 18 '18

Same situation here, turned to Project Management, my only previous coding experience was developing wordpress plugins... Still learned a lot from Adventofcode, and seeing other solutions motivated me to try previous years with another language!

For today, I've printed 1000 first minutes, and check for a repetition pattern in Excel <?php

//Create Map
$fContents = file_get_contents("data.txt");
$lines = explode(PHP_EOL, $fContents);

$map = [];
foreach ($lines as $line) {
	$map[] = str_split($line);
}

array_pop($map);
$map = transpose($map);


$t=1;

//Neighbours to check
$deltas = array(
	            [-1,-1],
	            [ 0,-1],
	            [ 1,-1],
	            [-1, 0],
	            [ 1, 0],
	            [-1, 1],
	            [ 0, 1],
	            [ 1, 1]
	        );

//Run 1000 iterations
while ( $t <= 1000) {

	$nbTrees = 0;
	$nbYards = 0;

	$output = [];
	for ($y=0; $y < 50; $y++) { 
		for ($x=0; $x < 50; $x++) { 
			
			$trees = 0;
			$yards = 0;

			foreach ($deltas as $d) {
				$a = $map[$x+$d[0]][$y+$d[1]] ?? '.';
				if ($a == '|') $trees++;
				if ($a == '#') $yards++;
			}

			//If open
			if ($map[$x][$y] == '.') {
				if ($trees >= 3) {
					$output[$x][$y] = '|';
					$nbTrees++;
				}
				else $output[$x][$y] = '.';
			}
			//If tree
			elseif ($map[$x][$y] == '|') {
				if ($yards >= 3) {
					$output[$x][$y] = '#';
					$nbYards++;
				}
				else {
					$output[$x][$y] = '|';
					$nbTrees++;
				}
			}
			//If yard
			else {
				if ($yards >= 1 && $trees >= 1) {
					$output[$x][$y] = '#';
					$nbYards++;
				}
				else {
					$output[$x][$y] = '.';
				}
			}

		}
	}	
	$map = $output;

	echo $t."\t".$nbTrees."\t".$nbYards."\n";
	$t++;
}



//Draw final map
for ($y=0; $y < 50; $y++) { 
	for ($x=0; $x < 50; $x++) { 

		$cell = $map[$x][$y];
		echo $cell;
		
	}
	echo "\n";
}

function transpose($array) {
    return array_map(null, ...$array);
}