/
day16.go
107 lines (98 loc) · 2.27 KB
/
day16.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"flag"
"fmt"
"io/ioutil"
"strconv"
"strings"
)
var inputFile = flag.String("inputFile", "inputs/day16.input", "Relative file path to use as input.")
var iterations = flag.Int("iterations", 1, "Number of iterations to run.")
type Cache map[[16]byte]int
func main() {
flag.Parse()
bytes, err := ioutil.ReadFile(*inputFile)
if err != nil {
fmt.Printf("Could not open file %s because %v.\n", *inputFile, err)
}
contents := string(bytes)
instructions := strings.Split(contents[:len(contents)-1], ",")
lookup := make(map[byte]int)
var order [16]byte
for i := 0; i < 16; i++ {
order[i] = byte('a') + byte(i)
lookup[byte('a')+byte(i)] = i
}
memo := make(Cache)
for i := 0; i < *iterations; i++ {
lookup, order = dance(instructions, lookup, order)
if prev, found := memo[order]; found {
loopLen := i - prev
fmt.Printf("Positions %d and %d repeat (length %d).", prev, i, loopLen)
break
} else {
memo[order] = i
}
}
for _, v := range order {
fmt.Printf("%c", v)
}
fmt.Println()
}
func dance(instructions []string, l map[byte]int, order [16]byte) (map[byte]int, [16]byte) {
lookup := make(map[byte]int)
for k, v := range l {
lookup[k] = v
}
for _, inst := range instructions {
switch inst[0] {
case 's':
count, err := strconv.Atoi(inst[1:])
if err != nil {
fmt.Printf("Failed to parse instruction %s\n", inst)
return lookup, order
}
var neworder [16]byte
for i := 0; i < 16; i++ {
if i < count {
c := order[16-count+i]
neworder[i] = c
lookup[c] = i
} else {
c := order[i-count]
neworder[i] = c
lookup[c] = i
}
}
order = neworder
case 'x':
operands := strings.Split(inst[1:], "/")
a, err := strconv.Atoi(operands[0])
if err != nil {
fmt.Printf("Failed to parse instruction %s: %s\n", inst)
return lookup, order
}
b, err := strconv.Atoi(operands[1])
if err != nil {
fmt.Printf("Failed to parse instruction %s: %s\n", inst)
return lookup, order
}
ca := order[a]
cb := order[b]
order[a] = cb
order[b] = ca
lookup[ca] = b
lookup[cb] = a
case 'p':
ca := inst[1]
cb := inst[3]
a := lookup[ca]
b := lookup[cb]
order[a] = cb
order[b] = ca
lookup[ca] = b
lookup[cb] = a
}
}
return lookup, order
}