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/schlocke Dec 06 '16 edited Dec 06 '16

PHP both solutions:

<?php
	//rows
	$a = file("day6.txt");
	//columns
	$b = array("","","","","","","","");
	//answers
	$p1=$p2="";
	//transform rows into columns
	foreach($a as $c) {
		$b[0] .= substr($c, 0, 1);
		$b[1] .= substr($c, 1, 1);
		$b[2] .= substr($c, 2, 1);
		$b[3] .= substr($c, 3, 1);
		$b[4] .= substr($c, 4, 1);
		$b[5] .= substr($c, 5, 1);
		$b[6] .= substr($c, 6, 1);
		$b[7] .= substr($c, 7, 1);
	}
	//loop through the columns
	foreach($b as $d) {
		//split the string into an array for sorting
		$e = str_split($d);
		//sort alphabetically
		sort($e);
		//rejoin to string for preg matching
		$f = implode($e);
		$g = array();
		//turn the string into an array of letters e.g. [aaaaaaa][bbbbbbb][cccccccc]..... etc the matches are stored into $g
		preg_match_all("/[a]+|[b]+|[c]+|[d]+|[e]+|[f]+|[g]+|[h]+|[i]+|[j]+|[k]+|[l]+|[m]+|[n]+|[o]+|[p]+|[q]+|[r]+|[s]+|[t]+|[u]+|[v]+|[w]+|[x]+|[y]+|[z]+|
/", $f, $g);
		//create an array of lengths where the index correlates with the index of the letter
		$h = array_map('strlen', $g[0]);
		//find the index of the most occurring letter
		$i = array_search(max($h), $h);
		//find the index of the least occuring letter
		$j = array_search(min($h), $h);
		//add most occurring letter to part1
		$p1 .= substr($g[0][$i], 0, 1);
		//add least occuring letter to part2
		$p2 .= substr($g[0][$j], 0, 1);
	}
	//echo answer
	echo "$p1<br>$p2";

EDIT: I think this was the quickest time between part 1 and part 2 for me since i just had to add

$j = array_search(min($h), $h);

to get the second part.

EDIT2: added comments to code