Created
December 6, 2023 10:40
-
-
Save maartenpaauw/bb9eceff170b3bc77eaedfe7d66dcc2b to your computer and use it in GitHub Desktop.
Advent of Code 2023 - Part 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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