-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
136 lines (106 loc) · 2.56 KB
/
main.go
File metadata and controls
136 lines (106 loc) · 2.56 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
)
const (
numMoves = 10000000
numTotalCups = 1000000
)
func input() *os.File {
input, err := os.Open(path.Join("2020", "23", "input.txt"))
if err != nil {
panic(err)
}
return input
}
// a linked list of cups
type cup struct {
next *cup
label int
}
func parse(r io.Reader) (*cup, map[int]*cup) {
var currentCup *cup
labelsToCups := make(map[int]*cup)
scanner := bufio.NewScanner(r)
chomp := func() string {
if !scanner.Scan() {
if scanner.Err() != nil {
panic(scanner.Err())
}
}
return scanner.Text()
}
labels := chomp()
var lastCup *cup
var maxLabel int
addCup := func(label int) {
newCup := &cup{
label: label,
}
labelsToCups[label] = newCup
if lastCup != nil {
lastCup.next = newCup
}
lastCup = newCup
}
for i := 0; i < len(labels); i++ {
label, err := strconv.Atoi(labels[i : i+1])
if err != nil {
panic(err)
}
addCup(label)
if currentCup == nil {
currentCup = lastCup
}
if lastCup.label > maxLabel {
maxLabel = lastCup.label
}
}
for i := maxLabel + 1; i <= numTotalCups; i += 1 {
addCup(i)
}
// connect linked list
lastCup.next = currentCup
return currentCup, labelsToCups
}
func move(curr *cup, labelToCup map[int]*cup) *cup {
// first action: pick up cups (by finding the cups after the current cup, and then removing them from the linked list)
first, second, third := curr.next, curr.next.next, curr.next.next.next
curr.next = third.next
third.next = nil
// second action: select destination cup (by finding the right destination value, and then the cup associated with it)
decrement := func(val, total int) int {
if val-1 == 0 {
return total
}
return val - 1
}
destinationValue := decrement(curr.label, len(labelToCup))
for i := 0; i < 3; i++ {
if destinationValue == first.label || destinationValue == second.label || destinationValue == third.label {
destinationValue = decrement(destinationValue, len(labelToCup))
}
}
destination := labelToCup[destinationValue]
// third action: put cups back (by inserting them after the destination cup)
third.next = destination.next
destination.next = first
// fourth action: select new current cup
return curr.next
}
func solve(currentCup *cup, labelsToCups map[int]*cup) int {
for i := 0; i < numMoves; i++ {
currentCup = move(currentCup, labelsToCups)
}
return labelsToCups[1].next.label * labelsToCups[1].next.next.label
}
func main() {
fmt.Println(solve(parse(strings.NewReader("389125467"))))
fmt.Println(solve(parse(input())))
}