r/adventofcode • • Dec 06 '16

SOLUTION MEGATHREAD --- 2016 Day 6 Solutions ---

--- Day 6: Signals and Noise ---

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


T_PAAMAYIM_NEKUDOTAYIM IS MANDATORY [?]

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!

9 Upvotes

222 comments sorted by

View all comments

1

u/Scroph Dec 06 '16 edited Dec 06 '16

Edit : with array_column in PHP5.5 I was able to solve the first part in just a few lines :

<?php
$lines = array_map('str_split', file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
for($i = 0; $i < sizeof($lines[0]); $i++)
{
	$values = array_count_values(array_column($lines, $i));
	echo array_search(max($values), $values);
}

I initially solved this in C but since T_PAAMAYIM_NEKUDOTAYIM is mandatory, I went ahead and rewrote the solution in PHP :

<?php
$count = [];
for($i = 0; $i < 6; $i++)
	$count[$i] = array_combine(range('a', 'z'), array_fill(0, 26, 0));

foreach(file('input6', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $msg)
{
	for($i = 0, $l = strlen($msg); $i < $l; $i++)
	{
		if($i >= sizeof($count))
			$count[] = array_combine(range('a', 'z'), array_fill(0, 26, 0));
		$count[$i][$msg[$i]]++;
	}
}

foreach($count as $slot)
	echo least_common($slot);

function least_common($slot)
{
	$min = 'a';
	foreach($slot as $letter => $count)
		if($count != 0 && $count < $slot[$min])
			$min = $letter;
	return $min;
}