-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.php
179 lines (144 loc) · 3.74 KB
/
day12.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
const INPUT_FILE = 'day12-input.txt';
const GENERATIONS = 20;
const BIG_GENERATIONS = 50000000000;
print "Part 1: " . part1() . "\n";
print "Part 2: " . part2() . "\n";
function part1(): int
{
$pots = getInitialState();
$rules = getRules();
printPots($pots);
for ($i = 0; $i < GENERATIONS; $i++) {
$pots = getNextGeneration($pots, $rules);
printPots($pots);
}
return getSumOfPots($pots);
}
function part2(): int
{
$pots = getInitialState();
$rules = getRules();
for ($i = 0; $i < BIG_GENERATIONS; $i++) {
$pots = getNextGeneration($pots, $rules);
if ($i % 100 === 0) {
print "Done $i = " . round($i * 100/ BIG_GENERATIONS, 2) . "%\n";
}
}
return getSumOfPots($pots);
}
function getInitialState(): array
{
$lines = explode("\n", file_get_contents(INPUT_FILE));
$state = str_replace('initial state: ', '', $lines[0]);
$pots = [];
$numPots = strlen($state);
for ($i = 0; $i < $numPots; $i++) {
$pots[$i] = $state[$i] === '#';
}
return $pots;
}
function getRules(): array
{
$rules = [];
$lines = explode("\n", file_get_contents(INPUT_FILE));
$lines = array_slice($lines, 2);
foreach ($lines as $line) {
if (trim($line)) {
$rules[] = new Rule($line);
}
}
return $rules;
}
/**
* @param array $pots
* @param array|Rule[] $rules
* @return array
*/
function getNextGeneration(array $pots, array $rules): array
{
$pots = extendPots($pots);
$nextGenPots = [];
foreach ($pots as $index => $pot) {
$nextGenPots[$index] = false;
foreach ($rules as $rule) {
if ($rule->doesRuleMatch($pots, $index)) {
$nextGenPots[$index] = $rule->outcome;
continue;
}
}
}
return $nextGenPots;
}
function extendPots(array $pots): array
{
$minKey = min(array_keys($pots));
$maxKey = max(array_keys($pots));
$pots[$minKey - 1] = false;
$pots[$minKey - 2] = false;
$pots[$maxKey + 1] = false;
$pots[$maxKey + 2] = false;
return $pots;
}
function printPots(array $pots)
{
$out = '';
for ($i = -3; $i <= 35; $i++) {
$out .= array_key_exists($i, $pots) ? ($pots[$i] ? '#' : '.') : '.';
}
print $out . "\n";
}
function getSumOfPots(array $pots): int
{
$sum = 0;
foreach ($pots as $index => $value) {
if ($value) {
$sum += $index;
}
}
return $sum;
}
class Rule
{
private $left1;
private $left2;
private $current;
private $right1;
private $right2;
public $outcome;
public function __construct(string $line)
{
$this->left2 = $line[0] === '#';
$this->left1 = $line[1] === '#';
$this->current = $line[2] ==='#';
$this->right1 = $line[3] === '#';
$this->right2 = $line[4] === '#';
$this->outcome = $line[9] === '#';
}
public function doesRuleMatch(array $pots, int $targetPot): bool
{
if ($this->getPotValue($pots, $targetPot - 2) !== $this->left2) {
return false;
}
if ($this->getPotValue($pots, $targetPot - 1) !== $this->left1) {
return false;
}
if ($this->getPotValue($pots, $targetPot) !== $this->current) {
return false;
}
if ($this->getPotValue($pots, $targetPot + 1) !== $this->right1) {
return false;
}
if ($this->getPotValue($pots, $targetPot + 2) !== $this->right2) {
return false;
}
return true;
}
private function getPotValue(array $pots, int $position): bool
{
if (array_key_exists($position, $pots)) {
return $pots[$position];
}
return false;
}
}