-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.ts
114 lines (99 loc) · 2.44 KB
/
10.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
import { getFileContent, getDataAsArray } from './utils.js';
interface CPU {
cycle: number
X: number
}
type Command = "noop" | "addx"
interface Instruction {
command: Command
arg?: number
}
const isExpectedCycle = (cpu: CPU) => {
if(cpu.cycle === 20) return true;
if((cpu.cycle - 20) % 40 === 0) return true;
return false;
}
const getSignalStrength = (cpu: CPU) => {
if(isExpectedCycle(cpu)) {
return cpu.cycle * cpu.X;
}
return 0;
}
const process = (arr: Instruction[]): number => {
let cpu: CPU = {
cycle: 0,
X: 1
}
let totalStrength = 0;
arr.forEach(ins => {
if(ins.command === 'noop') {
cpu.cycle += 1;
totalStrength += getSignalStrength(cpu);
return;
}
for(let i = 0; i < 2; i++) {
cpu.cycle += 1;
totalStrength += getSignalStrength(cpu);
if(i === 1) cpu.X += ins.arg!;
}
});
return totalStrength;
}
const processPart2 = (arr: Instruction[]): string[][] => {
let values = [...Array(241)].map(_ => 1);
let curTime = 0
let curXValue = 1
arr.forEach(ins => {
if(ins.command === 'noop') {
curTime += 1;
values[curTime] = curXValue;
} else if(ins.command === 'addx') {
values[curTime + 1] = curXValue;
curXValue += ins.arg!;
curTime += 2;
values[curTime] = curXValue;
}
})
let result = [...Array(6)].map(_ => [...Array(40)].map(_ => ""))
for(let row = 0; row < 6; row++) {
for(let col = 0; col < 40; col++) {
const index = row * 40 + col + 1;
if(Math.abs(values[index - 1] - col) <= 1) {
result[row][col] = 'X'
} else {
result[row][col] = ' '
}
}
}
return result;
}
const first = (arr: Instruction[]) => {
console.log(process(arr));
const result = 0;
console.log(result);
return result;
};
const second = (arr: Instruction[]) => {
const result = processPart2(arr);
result.forEach(row => {
console.log(row.join(''));
})
return 0;
};
const processInput = (arr: string[]): Instruction[] => {
return arr.map(v => {
if(v.startsWith('noop')) {
return {
command: "noop"
} as Instruction;
}
const [cmd, arg] = v.split(' ');
return {
command: cmd,
arg: parseInt(arg)
} as Instruction;
});
}
const data = getDataAsArray(getFileContent('input.txt'));
console.assert(first(processInput(data)) === 0, 'Not matching first part');
console.assert(second(processInput(data)) === 0, 'Not matching second part');