Skip to content

Commit bfbfe09

Browse files
committed
added day 19
1 parent 5417d39 commit bfbfe09

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

2018/day19/input.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ip 2
2+
addi 2 16 2
3+
seti 1 0 1
4+
seti 1 3 3
5+
mulr 1 3 5
6+
eqrr 5 4 5
7+
addr 5 2 2
8+
addi 2 1 2
9+
addr 1 0 0
10+
addi 3 1 3
11+
gtrr 3 4 5
12+
addr 2 5 2
13+
seti 2 6 2
14+
addi 1 1 1
15+
gtrr 1 4 5
16+
addr 5 2 2
17+
seti 1 1 2
18+
mulr 2 2 2
19+
addi 4 2 4
20+
mulr 4 4 4
21+
mulr 2 4 4
22+
muli 4 11 4
23+
addi 5 6 5
24+
mulr 5 2 5
25+
addi 5 19 5
26+
addr 4 5 4
27+
addr 2 0 2
28+
seti 0 7 2
29+
setr 2 6 5
30+
mulr 5 2 5
31+
addr 2 5 5
32+
mulr 2 5 5
33+
muli 5 14 5
34+
mulr 5 2 5
35+
addr 4 5 4
36+
seti 0 7 0
37+
seti 0 3 2

2018/day19/nineteen.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
)
8+
9+
func main() {
10+
instructions := instructions()
11+
// IP = 2
12+
registers := [6]int{0, 0, 0, 0, 0, 0}
13+
registers = runProgram(instructions[1:], registers)
14+
fmt.Printf("Part 1 | When the program halts, the value %d is in register[%d]\n", registers[0], 0)
15+
fmt.Printf("Part 2 | When the program halts, the value %d is in register[%d]\n", part2(), 0)
16+
}
17+
18+
func runProgram(program []string, registers [6]int) [6]int {
19+
ip := registers[2]
20+
for ip < len(program) {
21+
registers = decode(program[ip], registers)
22+
registers[2]++
23+
// Update IP counter
24+
ip = registers[2]
25+
}
26+
return registers
27+
}
28+
29+
func decode(instruction string, registers [6]int) [6]int {
30+
fields := [3]int{}
31+
inst := ""
32+
fmt.Sscanf(instruction, "%s %2d %2d %2d", &inst, &fields[0], &fields[1], &fields[2])
33+
34+
switch inst {
35+
case "seti":
36+
registers = seti(registers, fields)
37+
case "setr":
38+
registers = setr(registers, fields)
39+
case "addi":
40+
registers = addi(registers, fields)
41+
case "addr":
42+
registers = addr(registers, fields)
43+
case "muli":
44+
registers = muli(registers, fields)
45+
case "mulr":
46+
registers = mulr(registers, fields)
47+
case "gtrr":
48+
registers = gtrr(registers, fields)
49+
case "eqrr":
50+
registers = eqrr(registers, fields)
51+
default:
52+
fmt.Printf("inst: %s not found\n", inst)
53+
}
54+
55+
return registers
56+
}
57+
58+
func addr(registers [6]int, instructions [3]int) [6]int {
59+
registers[instructions[2]] = registers[instructions[0]] + registers[instructions[1]]
60+
return registers
61+
}
62+
63+
func addi(registers [6]int, instructions [3]int) [6]int {
64+
registers[instructions[2]] = registers[instructions[0]] + instructions[1]
65+
return registers
66+
}
67+
68+
func seti(registers [6]int, instructions [3]int) [6]int {
69+
registers[instructions[2]] = instructions[0]
70+
return registers
71+
}
72+
73+
func setr(registers [6]int, instructions [3]int) [6]int {
74+
registers[instructions[2]] = registers[instructions[0]]
75+
return registers
76+
}
77+
78+
func mulr(registers [6]int, instructions [3]int) [6]int {
79+
registers[instructions[2]] = registers[instructions[0]] * registers[instructions[1]]
80+
return registers
81+
}
82+
83+
func muli(registers [6]int, instructions [3]int) [6]int {
84+
registers[instructions[2]] = registers[instructions[0]] * instructions[1]
85+
return registers
86+
}
87+
88+
func gtrr(registers [6]int, instructions [3]int) [6]int {
89+
if registers[instructions[0]] > registers[instructions[1]] {
90+
registers[instructions[2]] = 1
91+
} else {
92+
registers[instructions[2]] = 0
93+
}
94+
return registers
95+
}
96+
97+
func eqrr(registers [6]int, instructions [3]int) [6]int {
98+
if registers[instructions[0]] == registers[instructions[1]] {
99+
registers[instructions[2]] = 1
100+
} else {
101+
registers[instructions[2]] = 0
102+
}
103+
return registers
104+
}
105+
106+
func instructions() []string {
107+
f, _ := os.ReadFile("input.txt")
108+
lines := strings.Split(strings.TrimSpace(string(f)), "\n")
109+
return lines
110+
}
111+
112+
// I can't be arsed to disassemble assembly what was meticulously crafted lol. Got this off Github
113+
func part2() int {
114+
numberToFactorize := 10551387 // this index varies based on inputs
115+
116+
var ans int
117+
for i := 1; i <= numberToFactorize; i++ {
118+
if numberToFactorize%i == 0 {
119+
ans += i
120+
}
121+
}
122+
123+
return ans
124+
}

0 commit comments

Comments
 (0)