Skip to content

Instantly share code, notes, and snippets.

@maartenpaauw
Created December 6, 2023 10:40
Show Gist options
  • Save maartenpaauw/bb9eceff170b3bc77eaedfe7d66dcc2b to your computer and use it in GitHub Desktop.
Save maartenpaauw/bb9eceff170b3bc77eaedfe7d66dcc2b to your computer and use it in GitHub Desktop.
Advent of Code 2023 - Part 6
<?php
final readonly class Boat
{
public function __construct(
private int $time,
private int $record,
) {
}
public function options(): int
{
$ways = 0;
for ($hold = 0; $hold <= $this->time; $hold++) {
$race = $this->time - $hold;
$distance = $hold * $race;
if ($distance > $this->record) {
$ways++;
}
}
return $ways;
}
}
$boats = [
new Boat(7, 9),
new Boat(15, 40),
new Boat(30, 200),
];
$single = new Boat(71530, 940200);
$options = array_map(static fn (Boat $boat): int => $boat->options(), $boats);
$part1 = array_reduce($options, static fn (int $multiplied, int $options) => $multiplied * $options, 1);
$part2 = $single->options();
$result = [$part1, $part2];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment