-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
116 lines (90 loc) · 2.88 KB
/
index.ts
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
enum OperationTypes {
UPDATE_MASK = "UPDATE_MASK",
WRITE_VALUE = "WRITE_VALUE",
}
type FluxAction = { type: string; payload: { [k: string]: any } };
type DockingStore = {
mask: string;
mem: { [k: string]: number };
};
const ASSIGNMENT_REGEXP = /^mem\[(\d+)\] = (\d+)$/;
const parseInput = (input: string): FluxAction[] => {
const parsedInput = input.split("\n");
return parsedInput.map((mem) => {
const matches = mem.match(ASSIGNMENT_REGEXP);
if (!matches) {
const newMask = mem.split(" = ")[1];
return { type: OperationTypes.UPDATE_MASK, payload: { value: newMask } };
}
const [_, position, value] = matches;
return {
type: OperationTypes.WRITE_VALUE,
payload: { position: parseInt(position, 10), value: parseInt(value, 10) },
};
});
};
const decimalToBin = (decimal: number) => decimal.toString(2).padStart(36, "0");
const binToDecimal = (bin: string) => parseInt(bin, 2);
const createFerryDockingProgram = (input: string, reducer: Function) => {
const actions = parseInput(input);
let store: DockingStore = {
mask: "",
mem: {},
};
for (let action of actions) {
reducer(store, action);
}
return Object.values(store.mem).reduce((sum, value) => sum + value, 0);
};
const dockingDataReducer = (
store: DockingStore,
{ type, payload }: FluxAction
) => {
if (type === OperationTypes.UPDATE_MASK) {
store.mask = payload.value;
} else if (type === OperationTypes.WRITE_VALUE) {
const bin = decimalToBin(payload.value);
let newValue = "";
for (let i = 0; i < store.mask.length; i++) {
const maskValue = store.mask[i];
if (maskValue !== "X") {
newValue += maskValue;
} else {
newValue += bin[i];
}
}
store.mem[payload.position] = binToDecimal(newValue);
}
};
export const part1 = (input: string) =>
createFerryDockingProgram(input, dockingDataReducer);
const dockingDataReducerV2 = (
store: DockingStore,
{ type, payload }: FluxAction
) => {
if (type === OperationTypes.UPDATE_MASK) {
store.mask = payload.value;
} else if (type === OperationTypes.WRITE_VALUE) {
const bin = decimalToBin(payload.position);
let positions = [""];
for (let i = 0; i < store.mask.length; i++) {
const maskValue = store.mask[i];
if (maskValue === "0") {
positions = positions.map((position) => position + bin[i]);
} else if (maskValue === "1") {
positions = positions.map((position) => position + "1");
} else if (maskValue === "X") {
positions = positions.flatMap((position) => [
position + "0",
position + "1",
]);
}
}
for (let position of positions) {
const decimalPosition = binToDecimal(position);
store.mem[decimalPosition] = payload.value;
}
}
};
export const part2 = (input: string) =>
createFerryDockingProgram(input, dockingDataReducerV2);