-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay5.php
138 lines (117 loc) · 3.5 KB
/
Day5.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
namespace AoC;
use AoC\Solution;
use Illuminate\Support\Collection;
class Day5 implements Solution
{
private readonly Collection $seedsToPlant;
private readonly Collection $maps;
public function __construct(string $input)
{
$parts = explode(PHP_EOL . PHP_EOL, trim($input));
$this->seedsToPlant = collect(explode(' ', explode(': ', array_shift($parts))[1]));
$this->maps = collect($parts)
->map(fn(string $part) => explode('map:' . PHP_EOL, $part)[1])
->map(fn(string $lines) => Util::splitByLines($lines))
->map(fn(array $ranges) => new MappedRanges(array_map(
function(string $range) {
$nums = explode(' ', $range);
return new MappedRange(
(int) $nums[1],
(int) $nums[0],
(int) $nums[2]
);
},
$ranges
)))
;
}
public function part1(string $input): string
{
$closestSeed = $this->seedsToPlant
->map(function(string $seed) {
foreach ($this->maps as $map) {
$seed = $map->get((int) $seed);
}
return $seed;
})
->min()
;
return (string) $closestSeed;
}
public function part2(string $input): string
{
$isInSeeds = function(int $seed): bool {
$ranges = $this->seedsToPlant
->chunk(2)
->map(fn(Collection $collection) => $collection->values())
;
foreach ($ranges as $range) {
if ($seed >= $range[0] && $seed < $range[0] + $range[1]) {
return true;
}
}
return false;
};
$loopWithStep = function(int $i, int $step = 1) use ($isInSeeds): int {
while (true) {
$seed = $i;
foreach ($this->maps->reverse() as $map) {
$seed = $map->get($seed, true);
}
if ($isInSeeds($seed)) { return $i; }
$i += $step;
}
};
$i = $loopWithStep(0, 10000);
$i = $loopWithStep($i - 10000, 1);
return (string) $i;
}
}
class MappedRanges
{
/**
* @param array<MappedRange> $ranges
*/
public function __construct(
private readonly array $ranges
) { }
public function get(int $num, bool $reverse = false): int
{
foreach ($this->ranges as $range) {
$mapped = $reverse ? $range->getReverse($num) : $range->get($num);
if (null !== $mapped) {
return $mapped;
}
}
return $num;
}
}
class MappedRange
{
private readonly int $srcTo;
private readonly int $destTo;
public function __construct(
private readonly int $srcFrom,
private readonly int $destFrom,
int $len
) {
$this->srcTo = $srcFrom + $len;
$this->destTo = $destFrom + $len;
}
function get(int $num): ?int
{
if ($num >= $this->srcFrom && $num < $this->srcTo) {
return $this->destFrom + ($num - $this->srcFrom);
}
return null;
}
function getReverse(int $num): ?int
{
if ($num >= $this->destFrom && $num < $this->destTo) {
return $this->srcFrom + ($num - $this->destFrom);
}
return null;
}
}