-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.go
56 lines (50 loc) · 1.2 KB
/
day02.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"os"
"strings"
"github.com/dergeberl/aoc/utils"
)
func main() {
input, err := os.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
fmt.Printf("Part 1: %v\n", SolveDay02Part1(string(input)))
fmt.Printf("Part 2: %v\n", SolveDay02Part2(string(input)))
}
// SolveDay02Part1
func SolveDay02Part1(input string) int {
return runWithReplace(input, 12, 2)
}
// SolveDay02Part2
func SolveDay02Part2(input string) int {
_, _ = utils.InputToIntSlice(input)
var solution int
for a := 0; a <= 99; a++ {
for b := 0; b <= 99; b++ {
if runWithReplace(input, a, b) == 19690720 {
solution = a*100 + b
}
}
}
return solution
}
func runWithReplace(input string, a, b int) int {
inputInt, _ := utils.InputToIntSlice(strings.ReplaceAll(strings.ReplaceAll(input, "\n", ""), ",", "\n"))
position := 0
inputInt[1] = a
inputInt[2] = b
for inputInt[position] != 99 {
switch inputInt[position] {
case 1:
// add
inputInt[inputInt[position+3]] = inputInt[inputInt[position+1]] + inputInt[inputInt[position+2]]
case 2:
// multiply
inputInt[inputInt[position+3]] = inputInt[inputInt[position+1]] * inputInt[inputInt[position+2]]
}
position += 4
}
return inputInt[0]
}