-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (58 loc) · 1.35 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"bytes"
"fmt"
"io"
"os"
"path"
"sort"
)
func input() *os.File {
input, err := os.Open(path.Join("2021", "24", "input.txt"))
if err != nil {
panic(err)
}
return input
}
// the solution for this day is split up into two files:
// - solver.go: contains a solver to solve the puzzle
// - alu.go: an alu implementation to check the solver's correctness
// the bulk of the code is found in those two files
func solve(r io.Reader) {
buf := &bytes.Buffer{}
tee := io.TeeReader(r, buf)
operations, blocks := parseOperations(tee), parseBlocks(buf)
validInput := findValidInput(blocks)
checkValidInput(operations, validInput)
fmt.Println(combineIntSlice(validInput))
}
func findValidInput(blocks []block) []int {
fmt.Println("Finding valid input ...")
validInputs := findValidInputs(blocks)
sort.Slice(validInputs, func(i, j int) bool {
return less(validInputs[i], validInputs[j])
})
return validInputs[len(validInputs)-1]
}
func checkValidInput(operations []operation, validInput []int) {
fmt.Println("Checking input ...")
a := &alu{
inputs: validInput,
}
a.run(operations)
if a.z != 0 {
panic("incorrect solution")
}
}
func less(a, b []int) bool {
for index := 0; index < len(a); index++ {
if a[index] == b[index] {
continue
}
return a[index] < b[index]
}
return false
}
func main() {
solve(input())
}