day02: Solve puzzles

main
Elis Hirwing 10 months ago
parent 5f709fddd0
commit 4877103ed9
Signed by: etu
GPG Key ID: D57EFA625C9A925F
  1. 2
      Makefile
  2. 6
      day02/example.txt
  3. 1000
      day02/input.txt
  4. 66
      day02/solve.php

@ -6,6 +6,8 @@ day01:
php day01/solve.php input
day02:
php day02/solve.php input
day03:
day04:
day05:

@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

File diff suppressed because it is too large Load Diff

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
require_once(__DIR__.'/../lib/functions.php');
$entries = getLines(__DIR__.'/'.$argv[1].'.txt');
timedPrintf(
'[2021-12-02.1] Resulting position: %d [Time: %ss]',
(function (array $entries) : int {
$forward = 0;
$depth = 0;
foreach ($entries as $entry) {
[ $cmd, $arg ] = explode(' ', $entry, 2);
switch ($cmd) {
case 'forward':
$forward += $arg;
break;
case 'up':
$depth -= $arg;
break;
case 'down':
$depth += $arg;
break;
}
}
return $forward * $depth;
}),
$entries
);
timedPrintf(
'[2021-12-02.2] Resulting position: %d [Time: %ss]',
(function (array $entries) : int {
$forward = 0;
$depth = 0;
$aim = 0;
foreach ($entries as $entry) {
[ $cmd, $arg ] = explode(' ', $entry, 2);
switch ($cmd) {
case 'forward':
$forward += $arg;
$depth += $aim * $arg;
break;
case 'up':
$aim -= $arg;
break;
case 'down':
$aim += $arg;
break;
}
}
return $forward * $depth;
}),
$entries
);
Loading…
Cancel
Save