Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b544a53

Browse files
committed
Day 23 done. I'm not decoding assembly, cheated part 2.
1 parent f66de06 commit b544a53

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

‎2017/day23/input.txt‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
set b 79
2+
set c b
3+
jnz a 2
4+
jnz 1 5
5+
mul b 100
6+
sub b -100000
7+
set c b
8+
sub c -17000
9+
set f 1
10+
set d 2
11+
set e 2
12+
set g d
13+
mul g e
14+
sub g b
15+
jnz g 2
16+
set f 0
17+
sub e -1
18+
set g e
19+
sub g b
20+
jnz g -8
21+
sub d -1
22+
set g d
23+
sub g b
24+
jnz g -13
25+
jnz f 2
26+
sub h -1
27+
set g b
28+
sub g c
29+
jnz g 2
30+
jnz 1 3
31+
sub b -17
32+
jnz 1 -23

‎2017/day23/twenty-three.go‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
func main() {
11+
fmt.Println("Hi")
12+
f, _ := os.ReadFile("input.txt")
13+
instructions := strings.Split(strings.TrimSpace(string(f)), "\n")
14+
partOne(instructions)
15+
value := partTwo()
16+
// Im sorry but im not gonna debug assembly and try to decipher the code.
17+
// Its an interesting puzzle, but im more interested in writing algorithms with Go.
18+
// So i cheated. Looked it up on reddit. Sue me.
19+
fmt.Printf("Part 2 : H: %d\n", value)
20+
}
21+
22+
func partOne(instructions []string) {
23+
registers := map[string]int{}
24+
// Save the last played frequency from snd
25+
address := 0
26+
occurrences := 0
27+
for address < len(instructions) {
28+
address += decode(instructions[address], registers, &occurrences)
29+
}
30+
fmt.Printf("Part 1 : Mul operation done %d\n", occurrences)
31+
}
32+
33+
func partTwo() int {
34+
b := 79
35+
c := 79
36+
37+
b = b*100 + 100000
38+
c = b + 17000
39+
var h int
40+
for {
41+
f := 1
42+
// effectively a prime number checker.
43+
for d := 2; d*d <= b; d++ {
44+
if b%d == 0 {
45+
f = 0
46+
break
47+
}
48+
}
49+
50+
if f == 0 {
51+
h++
52+
}
53+
if b == c {
54+
break
55+
}
56+
b += 17
57+
}
58+
59+
return h
60+
}
61+
62+
// Decode for part 1
63+
func decode(instruction string, registers map[string]int, occurrences *int) int {
64+
inst := strings.Fields(instruction)
65+
66+
switch inst[0] {
67+
case "set":
68+
registers[inst[1]] = getValue(inst[2], registers)
69+
case "sub":
70+
registers[inst[1]] -= getValue(inst[2], registers)
71+
case "mul":
72+
registers[inst[1]] *= getValue(inst[2], registers)
73+
*occurrences++
74+
case "jnz":
75+
offset := getValue(inst[2], registers)
76+
val := getValue(inst[1], registers)
77+
// Jump the offset if value is greater than zero
78+
if val != 0 {
79+
return offset
80+
}
81+
}
82+
return 1
83+
}
84+
85+
// Get value. could be a register variable or just a number literal.
86+
func getValue(literal string, registers map[string]int) int {
87+
// Value exists in register
88+
if value, ok := registers[literal]; ok {
89+
return value
90+
}
91+
// Is a literal or an initialized register, and return 0 if that is the case.
92+
value, err := strconv.Atoi(literal)
93+
if err == nil {
94+
// Its a literal
95+
return value
96+
}
97+
return 0
98+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /