-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.go
62 lines (50 loc) · 1.67 KB
/
solver.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
57
58
59
60
61
62
package day02_2019
import (
"fmt"
"github.com/andrerfcsantos/Advent-Of-Code/2020/go/intcode"
"strconv"
)
// Solver implements the puzzle.Solver interface for the puzzle for day 2
type Solver struct {
intcode.Memory
}
// NewSolver returns a new solver
func NewSolver() *Solver {
return &Solver{}
}
// ProcessInput processes the input by transforming it into a slice of opcodes (ints) saved in the struct. Required to implement Solver.
func (s *Solver) ProcessInput(fileContent string) error {
mem, err := intcode.ParseMemory(fileContent)
if err != nil {
fmt.Errorf("Error parsing memory: %w", err)
}
s.Memory = mem
return err
}
// Part1 solves part 1 of the puzzle. A copy of the opcodes slice is made before running the intcode program.
// Required to implement Solver.
func (s *Solver) Part1() (string, error) {
// Make copy of intcode program memory before running it
vm := intcode.NewDefaultVM(intcode.CloneMemory(s.Memory))
vm.Memory[1] = 12
vm.Memory[2] = 2
vm.Run()
return strconv.Itoa(vm.Memory[0]), nil
}
// Part2 solves part 2 of the puzzle by brute-forcing every combination of nouns and verbs until finding the one
// that gives the correct answer. Required to implement Solver.
func (s *Solver) Part2() (string, error) {
// Brute force every combination of nouns and verbs
for noun := 0; noun < 100; noun++ {
for verb := 0; verb < 100; verb++ {
vm := intcode.NewDefaultVM(intcode.CloneMemory(s.Memory))
vm.Memory[1] = noun
vm.Memory[2] = verb
vm.Run()
if vm.Memory[0] == 19690720 {
return strconv.Itoa(100*noun + verb), nil
}
}
}
return "", fmt.Errorf("could not find combination of noun < 100 and verb < 100 that solves the problem :(")
}