Skip to content

Commit 09db84e

Browse files
committed
day02 p01 and p02
1 parent 51e8830 commit 09db84e

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

day02/day02_p01.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
func main() {
12+
f, err := ioutil.ReadFile("input.txt")
13+
if err != nil {
14+
log.Fatal(err)
15+
}
16+
s := strings.Split(string(f), ",")
17+
opcodes := []int{}
18+
for _, v := range s {
19+
i, err := strconv.Atoi(v)
20+
if err != nil {
21+
continue
22+
}
23+
opcodes = append(opcodes, i)
24+
}
25+
26+
opcodes[1] = 12
27+
opcodes[2] = 2
28+
i := 0
29+
for {
30+
31+
if opcodes[i] == 99 {
32+
fmt.Println("opcodes[0]: ", opcodes[0])
33+
break
34+
}
35+
36+
if opcodes[i] == 1 {
37+
sum := 0
38+
s1 := opcodes[i+1]
39+
s2 := opcodes[i+2]
40+
sum += opcodes[s1] + opcodes[s2]
41+
s3 := opcodes[i+3]
42+
opcodes[s3] = sum
43+
} else if opcodes[i] == 2 {
44+
sum := 0
45+
s1 := opcodes[i+1]
46+
s2 := opcodes[i+2]
47+
sum += opcodes[s1] * opcodes[s2]
48+
s3 := opcodes[i+3]
49+
opcodes[s3] = sum
50+
}
51+
i += 4
52+
}
53+
fmt.Println("opcodes: ", opcodes)
54+
fmt.Println("len(opcodes): ", len(opcodes))
55+
}
56+
57+
// opcodes
58+
// 1: add the numbers in the positions of the next two values
59+
// 2: same thing, but multiply instead of adding
60+
// 99: halt immediately

day02/day02_p02.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"os"
8+
"strconv"
9+
"strings"
10+
)
11+
12+
func main() {
13+
f, err := ioutil.ReadFile("input.txt")
14+
if err != nil {
15+
log.Fatal(err)
16+
}
17+
s := strings.Split(string(f), ",")
18+
opcodes := []int{}
19+
for _, v := range s {
20+
i, err := strconv.Atoi(v)
21+
if err != nil {
22+
continue
23+
}
24+
opcodes = append(opcodes, i)
25+
}
26+
27+
for noun := 0; noun <= 99; noun++ {
28+
for verb := 0; verb <= 99; verb++ {
29+
var tryCodes []int
30+
tryCodes = make([]int, len(opcodes))
31+
copy(tryCodes, opcodes)
32+
tryCodes[1] = noun
33+
tryCodes[2] = verb
34+
35+
i := 0
36+
for {
37+
38+
if tryCodes[i] == 99 {
39+
break
40+
}
41+
42+
if tryCodes[i] == 1 {
43+
sum := 0
44+
s1 := tryCodes[i+1]
45+
s2 := tryCodes[i+2]
46+
sum += tryCodes[s1] + tryCodes[s2]
47+
s3 := tryCodes[i+3]
48+
tryCodes[s3] = sum
49+
} else if tryCodes[i] == 2 {
50+
sum := 0
51+
s1 := tryCodes[i+1]
52+
s2 := tryCodes[i+2]
53+
sum += tryCodes[s1] * tryCodes[s2]
54+
s3 := tryCodes[i+3]
55+
tryCodes[s3] = sum
56+
}
57+
i += 4
58+
if tryCodes[0] == 19690720 {
59+
fmt.Println("noun: ", noun, " verb: ", verb, " 100 * noun + verb: ", 100*noun+verb)
60+
os.Exit(0)
61+
}
62+
}
63+
64+
}
65+
}
66+
}
67+
68+
// opcodes
69+
// 1: add the numbers in the positions of the next two values
70+
// 2: same thing, but multiply instead of adding
71+
// 99: halt immediately

day02/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,6,19,23,2,23,6,27,2,6,27,31,2,13,31,35,1,10,35,39,2,39,13,43,1,43,13,47,1,6,47,51,1,10,51,55,2,55,6,59,1,5,59,63,2,9,63,67,1,6,67,71,2,9,71,75,1,6,75,79,2,79,13,83,1,83,10,87,1,13,87,91,1,91,10,95,2,9,95,99,1,5,99,103,2,10,103,107,1,107,2,111,1,111,5,0,99,2,14,0,0

0 commit comments

Comments
 (0)