-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd07.ts
More file actions
177 lines (139 loc) Β· 4.19 KB
/
d07.ts
File metadata and controls
177 lines (139 loc) Β· 4.19 KB
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
import { Day, RegisterDay } from "../Day.ts";
@RegisterDay(2015, 7)
export class Day07 extends Day {
override partOne(): string {
const input = this.readInput();
const wiresMap = new Map<string, number>();
const operations: Map<string, boolean> = new Map();
input.split("\n").forEach((line) => operations.set(line, false));
const result = execute(operations, wiresMap, "a");
return String(result);
}
override partTwo(): string {
const input = this.readInput();
const wiresMap = new Map<string, number>();
const operations: Map<string, boolean> = new Map();
input.split("\n").forEach((line) => operations.set(line, false));
let result = execute(operations, wiresMap, "a");
// Rewire 'b' and reset all other wires
wiresMap.clear();
wiresMap.set("b", result);
for (const key of operations.keys()) {
operations.set(key, false);
}
result = execute(operations, wiresMap, "a");
return String(result);
}
}
const REG = {
INPUT: /^(\d+|\w+) -> (\w+)$/,
AND: /^(.+) AND (.+) -> (\w+)$/,
OR: /^(.+) OR (.+) -> (\w+)$/,
LSHIFT: /^(.+) LSHIFT (.+) -> (\w+)$/,
RSHIFT: /^(.+) RSHIFT (.+) -> (\w+)$/,
NOT: /^NOT (.+) -> (\w+)$/,
};
function execute(
operations: Map<string, boolean>,
wiresMap: Map<string, number>,
output: string,
): number {
let result = undefined;
while (!result) {
for (const [op, executed] of operations.entries()) {
if (wiresMap.get(output)) {
result = wiresMap.get(output);
break;
}
if (executed) {
continue;
}
let matches;
if (matches = op.match(REG.INPUT)) {
if (wiresMap.get(matches[2])) {
// Value is already set, so skip (important to not "override an override" like on day 2)
operations.set(op, true);
continue;
}
const in1 = evaluateInput(matches[1], wiresMap);
if (in1 == undefined) {
continue;
}
wiresMap.set(matches[2], conv16Bit(in1));
operations.set(op, true);
continue;
}
if (matches = op.match(REG.AND)) {
const in1 = evaluateInput(matches[1], wiresMap);
const in2 = evaluateInput(matches[2], wiresMap);
const out = matches[3];
if (!in1 || !in2) {
continue;
}
wiresMap.set(out, conv16Bit(in1 & in2));
operations.set(op, true);
continue;
}
if (matches = op.match(REG.OR)) {
const in1 = evaluateInput(matches[1], wiresMap);
const in2 = evaluateInput(matches[2], wiresMap);
const out = matches[3];
if (in1 == undefined || in2 == undefined) {
continue;
}
wiresMap.set(out, conv16Bit(in1 | in2));
operations.set(op, true);
continue;
}
if (matches = op.match(REG.LSHIFT)) {
const in1 = evaluateInput(matches[1], wiresMap);
const in2 = evaluateInput(matches[2], wiresMap);
const out = matches[3];
if (in1 == undefined || in2 == undefined) {
continue;
}
wiresMap.set(out, conv16Bit(in1 << in2));
operations.set(op, true);
continue;
}
if (matches = op.match(REG.RSHIFT)) {
const in1 = evaluateInput(matches[1], wiresMap);
const in2 = evaluateInput(matches[2], wiresMap);
const out = matches[3];
if (in1 == undefined || in2 == undefined) {
continue;
}
wiresMap.set(out, conv16Bit(in1 >>> in2));
operations.set(op, true);
continue;
}
if (matches = op.match(REG.NOT)) {
const in1 = evaluateInput(matches[1], wiresMap);
const out = matches[2];
if (in1 == undefined) {
continue;
}
wiresMap.set(out, conv16Bit(~in1));
operations.set(op, true);
continue;
}
}
}
return result;
}
function evaluateInput(
input: string,
wiresMap: Map<string, number>,
): number | undefined {
const numeric = Number(input);
if (!isNaN(numeric)) {
return numeric;
}
return wiresMap.get(input);
}
function conv16Bit(num: number): number {
return (num << 16) >>> 16;
}
function dec2bin(dec: number) {
return (dec >>> 0).toString(2);
}