Skip to content

Commit 692f221

Browse files
committed
day-08
1 parent ce9d58e commit 692f221

File tree

10 files changed

+921
-5
lines changed

10 files changed

+921
-5
lines changed

.deno/gen/file/home/runner/adventOfCode2020/day-08/index.ts.js

+149
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.deno/gen/file/home/runner/adventOfCode2020/day-08/index.ts.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"source_path":"/home/runner/adventOfCode2020/day-08/index.ts","version_hash":"108577a9380b86b7cf6de2d6b848c6361c53384fa5d027c55bdbd3f7ffce9b6c"}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"deps":["file:///home/runner/adventOfCode2020/day-07/index.ts","file:///home/runner/adventOfCode2020/index.ts","file:///home/runner/adventOfCode2020/day-07/index.ts","file:///home/runner/adventOfCode2020/index.ts"],"version_hash":"7fbd210ebec11f65a97190ef900795c4b8da3805af3f5a1b8d1d272556b292ca"}
1+
{"deps":["file:///home/runner/adventOfCode2020/day-08/index.ts","file:///home/runner/adventOfCode2020/day-08/index.ts","file:///home/runner/adventOfCode2020/index.ts","file:///home/runner/adventOfCode2020/index.ts"],"version_hash":"7fbd210ebec11f65a97190ef900795c4b8da3805af3f5a1b8d1d272556b292ca"}

.deno/gen/file/home/runner/adventOfCode2020/index.ts.js

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.deno/gen/file/home/runner/adventOfCode2020/index.ts.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"source_path":"/home/runner/adventOfCode2020/index.ts","version_hash":"8245267391dbd10090eb5d5e49dad20c2c8e2cab880cb7a480109ef8cfee9b62"}
1+
{"source_path":"/home/runner/adventOfCode2020/index.ts","version_hash":"646ea2b3bea045f7f4c67b681820d8fff9a91b69a2d9af23b7929ce0249c307f"}

day-08/index.ts

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
const input = Deno.readTextFileSync("./day-08/input.txt")
2+
const rows = input.split('\n')
3+
4+
type Opertaion = 'nop' | 'acc' | 'jmp'
5+
6+
interface Instruction {
7+
operation: Opertaion
8+
argument: number,
9+
visited: boolean
10+
}
11+
12+
const stage1 = () => {
13+
const instructions: Instruction[] = rows.map(row => {
14+
const [operation, argument] = row.split(" ")
15+
return {
16+
operation: operation as Opertaion, argument: Number(argument), visited: false
17+
}
18+
})
19+
20+
let accumulator = 0
21+
let pointer = 0
22+
23+
while (!instructions[pointer].visited) {
24+
const instruction = instructions[pointer]
25+
const { operation, argument, visited } = instruction
26+
27+
instruction.visited = true
28+
29+
switch (operation) {
30+
case "acc":
31+
accumulator += argument
32+
pointer++
33+
break
34+
case "jmp":
35+
pointer += argument
36+
break
37+
case "nop":
38+
pointer++
39+
break
40+
}
41+
}
42+
43+
return accumulator
44+
}
45+
46+
const run = (instructions: Instruction[]) => {
47+
let accumulator = 0
48+
let prevPointer = 0
49+
let pointers = [0]
50+
51+
for (const pointer of pointers) {
52+
const instruction = instructions[pointer]
53+
54+
if (!instruction) {
55+
return {
56+
accumulator
57+
}
58+
}
59+
60+
const { operation, argument, visited } = instruction
61+
62+
if (visited) {
63+
return {
64+
accumulator,
65+
badInstruction: pointer
66+
}
67+
}
68+
69+
instruction.visited = true
70+
prevPointer = pointer
71+
72+
switch (operation) {
73+
case "acc":
74+
accumulator += argument
75+
pointers.push(pointer + 1)
76+
break
77+
case "jmp":
78+
pointers.push(pointer + argument)
79+
break
80+
case "nop":
81+
pointers.push(pointer + 1)
82+
break
83+
default:
84+
pointers.push(pointer + 1)
85+
break
86+
}
87+
}
88+
89+
return { accumulator }
90+
}
91+
92+
const parseRow = (row: string) => {
93+
const [operation, argument] = row.split(" ")
94+
return {
95+
operation: operation as Opertaion, argument: Number(argument), visited: false
96+
}
97+
}
98+
99+
const stage2 = () => {
100+
const instructions: Instruction[] = rows.map(parseRow)
101+
102+
let accumulator = -1
103+
104+
instructions.forEach((instruction, i) => {
105+
// this is ugly, duplicate, messy and stuff, but whatever 🙂
106+
if (instruction.operation === "jmp") {
107+
const fixedInstructions = rows.map(parseRow)
108+
fixedInstructions[i].operation = 'nop'
109+
const res = run(fixedInstructions)
110+
111+
if (res.badInstruction === undefined) {
112+
accumulator = res.accumulator
113+
}
114+
} else if (instruction.operation === "nop") {
115+
const fixedInstructions = rows.map(parseRow)
116+
fixedInstructions[i].operation = 'jmp'
117+
const res = run(fixedInstructions)
118+
119+
if (res.badInstruction === undefined) {
120+
accumulator = res.accumulator
121+
}
122+
}
123+
})
124+
125+
return accumulator
126+
}
127+
128+
129+
// console.log(stage1())
130+
console.log(stage2())

0 commit comments

Comments
 (0)