Skip to content

Commit fd233f8

Browse files
committed
Solve 2019 Day 2
Signed-off-by: Alexander Kurbatov <sir.alkurbatov@yandex.ru>
1 parent 7d397c4 commit fd233f8

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

2019/2/main.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// --- Day 2: 1202 Program Alarm ---
2+
// https://adventofcode.com/2019/day/2
3+
package main
4+
5+
import (
6+
"bufio"
7+
"log"
8+
"os"
9+
10+
"github.com/alkurbatov/adventofcode/internal/parsers"
11+
)
12+
13+
func readInput(path string) ([]int, error) {
14+
input, err := os.Open(path)
15+
if err != nil {
16+
return nil, err
17+
}
18+
defer input.Close()
19+
20+
scanner := bufio.NewScanner(input)
21+
scanner.Scan()
22+
23+
if err := scanner.Err(); err != nil {
24+
return nil, err
25+
}
26+
27+
return parsers.ReadNumbers(scanner.Text(), ",")
28+
}
29+
30+
func runIntcode(program []int) int {
31+
i := 0
32+
33+
for i < len(program) {
34+
instruction := program[i]
35+
36+
if instruction == 99 {
37+
break
38+
}
39+
40+
p1 := program[i+1]
41+
p2 := program[i+2]
42+
p3 := program[i+3]
43+
44+
switch instruction {
45+
case 1:
46+
program[p3] = program[p1] + program[p2]
47+
case 2:
48+
program[p3] = program[p1] * program[p2]
49+
}
50+
51+
i += 4
52+
}
53+
54+
return program[0]
55+
}
56+
57+
func restoreGravityAssist(program []int) int {
58+
programCopy := append([]int(nil), program...)
59+
60+
// Apply rules
61+
programCopy[1] = 12 // noun
62+
programCopy[2] = 2 // verb
63+
64+
return runIntcode(programCopy)
65+
}
66+
67+
func completeGravityAssist(program []int) (noun, verb int) {
68+
for noun = 0; noun < 100; noun++ {
69+
for verb = 0; verb < 100; verb++ {
70+
programCopy := append([]int(nil), program...)
71+
72+
// Apply rules
73+
programCopy[1] = noun
74+
programCopy[2] = verb
75+
76+
if runIntcode(programCopy) == 19690720 {
77+
return noun, verb
78+
}
79+
}
80+
}
81+
82+
return noun, verb
83+
}
84+
85+
func main() {
86+
program, err := readInput("./2019/2/input.txt")
87+
if err != nil {
88+
log.Printf("Failed to read data from input.txt: %v", err)
89+
return
90+
}
91+
92+
firstPartResult := restoreGravityAssist(program)
93+
log.Printf("Part 1 result: %d", firstPartResult)
94+
95+
noun, verb := completeGravityAssist(program)
96+
secondPartResult := 100*noun + verb
97+
log.Printf("Part 2 result: %d", secondPartResult)
98+
}

2023/9/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func readInput(path string) ([][]int, error) {
2121
result := make([][]int, 0)
2222

2323
for scanner.Scan() {
24-
sequence, err := parsers.ReadNumbers(scanner.Text())
24+
sequence, err := parsers.ReadNumbers(scanner.Text(), " ")
2525
if err != nil {
2626
return nil, err
2727
}

internal/parsers/numbers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
)
77

88
// Read sequence of numbers from string.
9-
func ReadNumbers(src string) ([]int, error) {
9+
func ReadNumbers(src, sep string) ([]int, error) {
1010
result := make([]int, 0)
1111

12-
for _, field := range strings.Fields(src) {
12+
for _, field := range strings.Split(src, sep) {
1313
num, err := strconv.Atoi(field)
1414
if err != nil {
1515
return nil, err

0 commit comments

Comments
 (0)