AOC 2024 12-1
unknown
php
3 months ago
4.5 kB
48
Indexable
<body style="margin-left: 10px; font-family: consolas; font-size: 18px"> <?php $day = 12; $file = "advent$day.txt"; echo "-- Start processing $file --<br><br>"; $input = file_get_contents($file); $rowArray = explode("\r\n", $input); $plotArray = array(); $regionArray = array(); $regionList = array(); for ($i = 0; $i < count($rowArray); $i ++) { $plotArray[$i] = str_split($rowArray[$i]); } $dirArray = [ "N", "E", "S", "W" ]; $objectArray = array(); $rowCount = count($plotArray); $colCount = count($plotArray[0]); $offset = $colCount; $resultCount = 0; $i = 0; for ($row = 0; $row < $rowCount; $row ++) { for ($col = 0; $col < $colCount; $col ++) { $object = new stdClass(); $object->id = $i; $object->row = $row; $object->col = $col; $object->value = $plotArray[$row][$col]; $object->region = false; $object->borders = false; $objectArray[] = $object; $i++; } } function paint($region) { global $objectArray; global $offset; global $toPaint; $id = array_pop($toPaint); $nEdge = true; $eEdge = false; $sEdge = false; $wEdge = false; $borders = 0; if (isset($objectArray[$id-$offset])) $nEdge = false; if ($id % $offset == $offset-1) $eEdge = true; if (!isset($objectArray[$id+$offset])) $sEdge = true; if ($id % $offset == 0) $wEdge = true; $me = $objectArray[$id]->value; if (!$nEdge) { $north = $objectArray[$id-$offset]->value; $nPainted = $objectArray[$id-$offset]->region; if ($me == $north && !$nPainted) array_push($toPaint, $id-$offset); if ($me != $north) $borders++; } else $borders++; // edge if (!$eEdge) { $east = $objectArray[$id+1]->value; $ePainted = $objectArray[$id+1]->region; if ($me == $east && !$ePainted) array_push($toPaint, $id+1); if ($me != $east) $borders++; } else $borders++; // edge if (!$sEdge) { $south = $objectArray[$id+$offset]->value; $sPainted = $objectArray[$id+$offset]->region; if ($me == $south && !$sPainted) array_push($toPaint, $id+$offset); if ($me != $south) $borders++; } else $borders++; // edge if (!$wEdge) { $west = $objectArray[$id-1]->value; $wPainted = $objectArray[$id-1]->region; if ($me == $west && !$wPainted) array_push($toPaint, $id-1); if ($me != $west) $borders++; } else $borders++; // edge // start at ID - set region to 0 $objectArray[$id]->region = $region; $objectArray[$id]->borders = $borders; } //for ($i = 0; $i < count($objectArray); $i++) { //} function findObject($row, $col){ global $objectArray; foreach ($objectArray as $element) { if ($col == $element->col && $row == $element->row) { return $element; } } return false; } function findNoRegion(){ global $objectArray; foreach ($objectArray as $element) { if ($element->region === false) { return $element->id; } } return false; } $done = false; // regions have index 1 $region = 1; while (!$done) { // find first unpainted $noRegion = findNoRegion(); if ($noRegion === false) $done = true; else { // paint it and its neighbours with first region... $toPaint = [$noRegion]; while (count($toPaint) > 0) { paint($region); } } // stop when all are painted same region // increase region $region++; // find first unpainted... // stop when none are unpainted } foreach ($objectArray as $object) { $region = $object->region; $regionArray[$region][] = $object->borders; } for ($i = 1; $i < count($regionArray)+1; $i++) { $plotCount = count($regionArray[$i]); $borderSum = array_sum($regionArray[$i]); echo "Region $i<br>"; echo "Plots = $plotCount<br>"; echo "Perimeter = $borderSum<br><br>"; $resultCount = $resultCount + ($plotCount * $borderSum); } echo "Result Count = $resultCount<br>"; echo "<br><br>-- Finished processing $file --<br>";
Editor is loading...
Leave a Comment