Skip to content

Commit

Permalink
Day 14 Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Marshall Davis committed Dec 14, 2024
1 parent c2af297 commit e3fe69e
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions 2024/14/puzzle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

$inputStream = fopen('input.txt', 'r+');

class Robot
{
public function __construct(public int $y, public int $x, public int $horizontal, public int $vertical)
{
}
}

$height = 103;
$width = 101;
$time = 100;

$robots = [];
while (!feof($inputStream)) {
$parameters = [];
preg_match('/p=(?<px>-?\d+?),(?<py>-?\d+?) v=(?<vx>-?\d+?),(?<vy>-?\d+?)$/', trim(fgets($inputStream)),
$parameters);
$robots[] = new Robot($parameters['py'], $parameters['px'], $parameters['vx'], $parameters['vy']);
}

$grid = array_pad([], $height, array_pad([], $width, 0));

foreach ($robots as $robot) {
$grid[$robot->x][$robot->y]++;
}

foreach ($grid as $row) {
echo implode(array_map(fn (int $c) => $c === 0 ? '.':$c, $row))."\n";
}
echo "\n";

for ($t = 1; $t <= $time; $t++) {
/** @var Robot $robot */
foreach ($robots as $robot) {
$robot->x += $robot->horizontal;
if ($robot->x >= $width) {
$robot->x -= $width;
}
if ($robot->x < 0) {
$robot->x = $width + $robot->x;
}

$robot->y += $robot->vertical;
if ($robot->y >= $height) {
$robot->y -= $height;
}
if ($robot->y < 0) {
$robot->y = $height + $robot->y;
}
}




$grid = array_pad([], $height, array_pad([], $width, 0));

foreach ($robots as $robot) {
$grid[$robot->y][$robot->x]++;
}

foreach ($grid as $row) {
echo implode(array_map(fn (int $c) => $c === 0 ? '.':$c, $row))."\n";
}

echo "\n";
}

$q1 = 0;
$q2 = 0;
$q3 = 0;
$q4 = 0;

foreach ($robots as $robot) {
if ($robot->x >= ceil($width/2) && $robot->y < floor($height/2)) {
$q2++;
}
if ($robot->x < floor($width/2) && $robot->y < floor($height/2)) {
$q1++;
}
if ($robot->x < floor($width/2) && $robot->y >=ceil($height/2)) {
$q3++;
}
if ($robot->x >= ceil($width/2) && $robot->y >=ceil($height/2)) {
$q4++;
}
}
echo "$q1 - $q2 - $q3 - $q4\n";
$safe = $q1 * $q2 * $q3 * $q4;
echo "safety: $safe\n";

echo 256598166 <= $safe ? 'WRONG!':'';
echo PHP_EOL;

0 comments on commit e3fe69e

Please sign in to comment.