-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.ts
74 lines (66 loc) · 1.77 KB
/
08.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
import {loadData, max} from '../shared/utils';
export type DataType = [
target: string,
targetValue: number,
check: string,
checkOp: string,
checkValue: number
];
type Registers = Record<string, number>;
export const parseData = (row: string): DataType => {
const parsed = row.split(' ');
return [
parsed[0],
Number(parsed[2]) * (parsed[1] === 'dec' ? -1 : 1),
parsed[4],
parsed[5],
Number(parsed[6]),
];
};
const OPERATORS: Record<string, (x: number, y: number) => boolean> = {
'>': (x, y) => x > y,
'<': (x, y) => x < y,
'<=': (x, y) => x <= y,
'>=': (x, y) => x >= y,
'==': (x, y) => x === y,
'!=': (x, y) => x != y,
};
/* istanbul ignore next */
export const parse = async (): Promise<Array<DataType>> =>
(await loadData(2017, 8)).split('\n').map(parseData);
function executeInstruction(
registers: Registers,
instruction: DataType
): Registers {
const check = registers[instruction[2]] ?? 0;
const operator = OPERATORS[instruction[3]] ?? (() => false);
if (operator(check, instruction[4])) {
return {
...registers,
[instruction[0]]: (registers[instruction[0]] ?? 0) + instruction[1],
};
}
return {...registers};
}
export const part1 = (data: Array<DataType>): number => {
return max(
Object.values(
data.reduce(
(registers, instruction) => executeInstruction(registers, instruction),
{}
)
)
);
};
export const part2 = (data: Array<DataType>): number => {
return data.reduce(
([registers, maxValue], instruction) => {
const newRegisters = executeInstruction(registers, instruction);
return [
newRegisters,
Math.max(maxValue, max(Object.values(newRegisters))),
] as [Registers, number];
},
[{}, 0] as [Registers, number]
)[1];
};