This December I participated in Advent of Code.

For part 1, I optimized a little. Like only storing the representation of each 3 way computer group once. Also 3d arrays. For part 2… I flirted a little with BK (ehm, can’t remember what that stands for), but that program seemed to never stop. So I did it differently. Just like part 1 asked me to create all the 3 way groups, I now moved on to create all the 4 way groups, 5 way groups etc. As each computer had about 13 friends, this shouldn’t take forever to get through. I think it took 35 minutes. I stopped when I only had 1 group of the current size. Oh yeah, and I changed the data structure a little. (BK was sort of the other way around. Trying to grow 1 large set greedily.)
Part 1:
<?php
//////////////////////////////////
$input = file_get_contents('./d23input2.txt', true);
foreach(preg_split("/((\r?\n)|(\r\n?))/", $input) as $line) {
if(strlen($line)>2) {
$conn1[] = explode("-", $line);
}
}
foreach($conn1 as $comp) {
$comp1 = $comp[0];
$comp2 = $comp[1];
$conn2[$comp1][$comp2] = 1;
$conn2[$comp2][$comp1] = 1;
}
foreach($conn2 as $comp1 => $c1con) {
foreach($c1con as $comp2 => $cona) {
foreach($c1con as $comp3 => $conb) {
// make sure comp2 and comp3 are different
if(isset($conn2[$comp2][$comp3])) {
// only store the 3 way connection alphabetically, so as not to store it 6 times
if(strcmp($comp1,$comp2) < 0 && strcmp($comp2,$comp3) < 0) {
// only store this set, if one of the computers begin with t
if(substr($comp1, 0, 1) == "t" || substr($comp2, 0, 1) == "t" || substr($comp3, 0, 1) == "t") {
$conn3[$comp1][$comp2][$comp3] = 1;
}
}
}
}
}
}
$sum = 0;
foreach($conn3 as $a) {
foreach($a as $b) {
foreach($b as $c) {
$sum += $c;
}
}
}
print("$sum\n");
?>
Part 2:
<?php
//////////////////////////////////
$input = file_get_contents('./d23input1.txt', true);
foreach(preg_split("/((\r?\n)|(\r\n?))/", $input) as $line) {
if(strlen($line)>2) {
$conn1[] = explode("-", $line);
}
}
foreach($conn1 as $comp) {
$comp1 = $comp[0];
$comp2 = $comp[1];
$conn2[$comp1][$comp2] = 1;
$conn2[$comp2][$comp1] = 1;
}
foreach($conn2 as $comp1 => $c1con) {
foreach($c1con as $comp2 => $cona) {
foreach($c1con as $comp3 => $conb) {
// make sure comp2 and comp3 are different
if(isset($conn2[$comp2][$comp3])) {
// only store the 3 way connection alphabetically, so as not to store it 6 times
if(strcmp($comp1,$comp2) < 0 && strcmp($comp2,$comp3) < 0) {
//$conn3[$comp1][$comp2][$comp3] = 1;
$conn3b[] = array($comp1, $comp2, $comp3);
}
}
}
}
}
$allcomp = array_keys($conn2);
$multiple = sizeof($conn3b);
$connx = $conn3b;
while($multiple > 1) {
print("No. of sets in the iteration: $multiple\n");
$conny = array();
// build new arrays of length y from length x - y = x+1
foreach($connx as $wayx) {
foreach($allcomp as $key => $newcomp) {
// test that computer is not already in array
if(!in_array($newcomp, $wayx)) {
$newgood = 1;
// test that new is connected to all in wayx
foreach($wayx as $oldcomp) {
if(!isset($conn2[$oldcomp][$newcomp])) {
$newgood = 0;
break;
}
}
if($newgood == 1) {
// create new wayy
$wayytmp = $wayx;
array_push($wayytmp, $newcomp);
sort($wayytmp);
if(!in_array($wayytmp, $conny)) {
$conny[] = $wayytmp;
}
}
}
}
}
$multiple = sizeof($conny);
if($multiple == 1) {
print("Max set found!\n");
print_r($conny);
}
$connx = $conny;
}
?>