-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpart-one.html
97 lines (93 loc) · 2.75 KB
/
part-one.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
input:invalid {
border: 2px dashed red;
}
input:invalid:focus {
background-color: pink;
}
input {
width: 200px;
}
</style>
</head>
<body>
<label>Input: <input id="num" placeholder="Enter 14 digit number" minlength="14" /></label>
<ul>
<li id="w">w: <span></span></li>
<li id="x">x: <span></span></li>
<li id="y">y: <span></span></li>
<li id="z">z: <span></span></li>
</ul>
<ol id="steps" style="font-family: monospace" start="0"></ol>
<hr />
<pre id="winners-ele"></pre>
<script>
const input_ele = document.getElementById('num');
const winners_ele = document.getElementById('winners-ele');
const steps_ele = document.getElementById('steps');
const regsMax = (steps) =>
steps.reduce(
(acc, cur) => {
for (let r in cur) {
if (String(cur[r]).length > acc[r]) {
acc[r] = String(cur[r]).length;
}
}
return acc;
},
{ w: 0, x: 0, y: 0, z: 0 }
);
const regsToString = (regs, maxes) =>
`w: ${String(regs.w).padStart(maxes.w)}, x: ${String(regs.x).padStart(
maxes.x
)}, y: ${String(regs.y).padStart(maxes.y)}, <b>z: ${String(regs.z).padStart(
maxes.z
)}</b>`;
(async () => {
const instructions_arr = await fetch('/input.txt')
.then((response) => response.text())
.then((raw_input) => raw_input.trim().split('\n'));
const { ALU } = await import('./alu.js');
const winners = new Set();
input_ele.addEventListener('input', () => {
const input_str = input_ele.value;
if (input_str.length === 14) {
steps_ele.innerHTML = '';
const alu = new ALU(instructions_arr, input_str);
alu.run();
const maxes = regsMax(alu.steps);
for (let li of alu.steps.map((regs) => {
const li_ele = document.createElement('li');
li_ele.innerHTML = regsToString(regs, maxes);
li_ele.style.whiteSpace = 'pre-wrap';
return li_ele;
})) {
steps_ele.appendChild(li);
}
for (let register of ['w', 'x', 'y', 'z']) {
let output = document.querySelector(`#${register} > span`);
output.innerText = alu.registers[register];
if (register === 'z') {
if (alu.registers.z === 0) {
winners.add(input_str);
// Can sort by string since they are all the same length
winners_ele.value = [...winners].sort().join('\n');
output.innerText += ' ✅';
} else {
output.innerText += ' ‼️';
}
}
}
}
});
})();
</script>
</body>
</html>