-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.php
214 lines (179 loc) · 6.23 KB
/
day24.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
$filename = 'inputs/day24.txt';
$lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$results = [];
$expressions = [];
$variables = [];
// More accurate to compare these instead of the expanded string versions of the expressions
class AST
{
public string $operator;
public AST|string $left;
public AST|string $right;
public function __construct(string $operator, AST|string $left, AST|string $right)
{
$this->operator = $operator;
$this->left = $left;
$this->right = $right;
}
}
function evaluate(string $variable): int
{
global $results, $expressions;
if (!array_key_exists($variable, $results)) {
list($arg1, $bitwiseOperator, $arg2) = explode(' ', $expressions[$variable]);
$results[$variable] = match ($bitwiseOperator) {
'AND' => evaluate($arg1) & evaluate($arg2),
'XOR' => evaluate($arg1) ^ evaluate($arg2),
'OR' => evaluate($arg1) | evaluate($arg2),
};
}
return $results[$variable];
}
// If the ASTs for the expected z[i] and actual z[i] match, we've fixed it (or it was never wrong originally)
function sameAST(AST|string $a, AST|string $b): bool
{
if (gettype($a) === gettype($b)) {
if (gettype($a) === 'string') {
return $a === $b;
} else {
return $a->operator === $b->operator and ((sameAST($a->left, $b->left) and sameAST($a->right, $b->right)) or (sameAST($a->right, $b->left) and sameAST($a->right, $b->left)));
}
}
return false;
}
function baseCaseVariable(string $variable): bool
{
return str_starts_with($variable, 'x') or str_starts_with($variable, 'y');
}
/**
* @link https://en.wikipedia.org/wiki/Adder_(electronics)#Ripple-carry_adder
*
* Carry digit for ripple carry adder: carry_out[i] = ((x[i] ^ y[i]) & carry_out[i - 1]) | (x[i] & y[i]).
* ! This does not rely on the actual contents of $expressions
*/
function expectedCarryOutFormula(int $index): AST
{
if ($index === 0) {
return new AST('AND', 'x00', 'y00');
}
$i = str_pad(strval($index), 2, '0', STR_PAD_LEFT);
return new AST(
'OR',
new AST('AND', new AST('XOR', "x$i", "y$i"), expectedCarryOutFormula($index - 1)),
new AST('AND', "x$i", "y$i"),
);
}
/**
* @link https://en.wikipedia.org/wiki/Adder_(electronics)#Ripple-carry_adder
*
* Output digit for ripple carry adder: z[i] = (x[i] ^ y[i]) ^ carry_out[i - 1].
* ! This does not rely on the actual contents of $expressions
*/
function expectedFormulaForZ(int $index): AST
{
$i = str_pad(strval($index), 2, '0', STR_PAD_LEFT);
if ($index === 0) {
return new AST('XOR', "x$i", "y$i");
}
return new AST(
'XOR',
new AST('XOR', "x$i", "y$i"),
expectedCarryOutFormula($index - 1),
);
}
// Uses contents of $expressions to recursively construct the AST
function actualFormula(string $variable, array $seen): AST|string
{
global $expressions;
if (in_array($variable, $seen)) {
return "(*cycle=$variable)";
}
if (baseCaseVariable($variable)) {
return $variable;
}
list($lhs, $operator, $rhs) = explode(' ', $expressions[$variable]);
return new AST($operator, actualFormula($lhs, [...$seen, $variable]), actualFormula($rhs, [...$seen, $variable]));
}
// Evaluate wires with z prefix
function solvePart1(): int
{
global $variables;
$values = [];
foreach ($variables as $variable) {
if (str_starts_with($variable, 'z')) {
$index = intval(substr($variable, 1));
$values[$index] = evaluate($variable);
}
}
$bitString = '';
for ($i = 0; $i < count($values); $i++) {
$bitString = $values[$i] . $bitString;
}
return bindec($bitString);
}
function solvePart2(): string
{
global $variables, $expressions;
$numZ = 0;
foreach ($variables as $variable) {
if (str_starts_with($variable, 'z')) {
$numZ++;
}
}
$changed = [];
for ($zi = 0; $zi < $numZ - 1; $zi++) {
$i = str_pad(strval($zi), 2, '0', STR_PAD_LEFT);
if (!sameAST(expectedFormulaForZ($zi), actualFormula("z$i", []))) {
echo "Broke at z$i...";
$originalExpressions = $expressions;
$swaps = [];
for ($first = 0; $first < count($variables) - 1; $first++) {
for ($second = $first + 1; $second < count($variables); $second++) {
if (!baseCaseVariable($variables[$first]) and !baseCaseVariable($variables[$second])) {
$swaps[] = [$variables[$first], $variables[$second]];
}
}
}
foreach ($swaps as list($a, $b)) {
$temp = $expressions[$a];
$expressions[$a] = $expressions[$b];
$expressions[$b] = $temp;
foreach (array_keys($expressions) as $key) {
$expressions[$key] = str_replace([$a, $b], [$b, $a], $expressions[$key]);
}
$fixed = true;
for ($indexUpToI = 0; $indexUpToI <= $i; $indexUpToI++) {
$j = str_pad(strval($indexUpToI), 2, '0', STR_PAD_LEFT);
if (!sameAST(expectedFormulaForZ($j), actualFormula("z$j", []))) {
$fixed = false;
break;
}
}
// We don't revert the state if it was fixed
if ($fixed) {
array_push($changed, $a, $b);
echo "fixed by swapping \e[1;37;42m$a\e[0m with \e[1;37;42m$b\e[0m!\n";
break;
}
$expressions = $originalExpressions;
}
}
}
sort($changed);
return implode(',', $changed);
}
foreach ($lines as $line) {
if (str_contains($line, ': ')) {
list($lhs, $rhs) = explode(': ', $line);
$results[$lhs] = intval($rhs);
$variables[] = $lhs;
} else {
list($lhs, $rhs) = explode(' -> ', $line);
$expressions[$rhs] = $lhs;
array_push($variables, $rhs, explode(' ', $lhs)[0], explode(' ', $lhs)[2]);
}
}
$variables = [...array_unique($variables)];
echo solvePart1() . "\n";
echo solvePart2() . "\n";