-
Notifications
You must be signed in to change notification settings - Fork 0
/
day21_1.groovy
156 lines (149 loc) · 4.11 KB
/
day21_1.groovy
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
def sampleInput = '''029A
980A
179A
456A
379A'''
static def neighbours(state) {
def movesNumeric = [
'7': [
'v': '4',
'>': '8'
],
'8': [
'<': '7',
'v': '5',
'>': '9'
],
'9': [
'v': '6',
'<': '8'
],
'4': [
'^': '7',
'v': '1',
'>': '5'
],
'5': [
'^': '8',
'<': '4',
'v': '2',
'>': '6',
],
'6': [
'^': '9',
'v': '3',
'<': '5'
],
'1': [
'^': '4',
'>': '2'
],
'2': [
'^': '5',
'<': '1',
'v': '0',
'>': '3',
],
'3': [
'^': '6',
'v': 'A',
'<': '2'
],
'0': [
'^': '2',
'>': 'A'
],
'A': [
'^': '3',
'<': '0'
]
]
def movesArrows = [
'A': [
'v': '>',
'<': '^'
],
'^': [
'>': 'A',
'v': 'v'
],
'>': [
'<': 'v',
'^': 'A'
],
'v': [
'>': '>',
'^': '^',
'<': '<'
],
'<': [
'>': 'v',
]
]
// case 'A':
def n = [:]
['A', '<', '>', '^', 'v'].each {
if (it == 'A') {
if (state[0] == 'A') {
if (state[1] == 'A') {
// do nothing, the button is pressed and nothing changes
} else {
def nextNumber = movesNumeric[state[2]][state[1]]
if (nextNumber) {
n << [(it): [state[0], state[1], nextNumber]]
}
}
} else {
def next2 = movesArrows[state[1]][state[0]]
if (next2) {
n << [(it): [state[0], next2, state[2]]]
}
}
} else {
def next1 = movesArrows[state[0]][it]
if (next1) {
n << [(it): [next1, state[1], state[2]]]
}
}
}
n
}
//state has 1 numeric and 2 arrows
static def shortestNumberOfPushes(def state, def targetState) {
// there's always an option to press A, or to press any of the buttons.
def queue = [state] as Queue
def dist = [(state): 0L]
def prev = [:]
while (!queue.isEmpty()) {
def node = queue.poll()
//println "$node"
if (node == targetState) return [prev, dist[node]]
neighbours(node).each {
//println it
if (!dist.containsKey(it.value)) {
prev[it.value] = [it.key, node]
queue.add(it.value)
dist[it.value] = dist[node] + 1
}
}
}
throw new RuntimeException("Path not found between $state and $targetState")
}
static long numericPartOfCode(String code) {
code.replaceAll(~/^0+/, '').replaceAll('A', '').toLong()
}
static long solve(List<String> inputs) {
inputs.collect { code ->
def state = ['A', 'A', 'A']
def shortestPaths = code.inject(0) { sum, sign ->
def (prev, shortestPath) = shortestNumberOfPushes(state, ['A', 'A', sign])
state = ['A', 'A', sign]
sum += shortestPath + 1
sum
}
//println "$shortestPaths: ${numericPartOfCode(code)}"
shortestPaths * numericPartOfCode(code)
}.sum() as long
}
assert 126384L == solve(sampleInput.split('\n') as List)
println solve(new File('input/day21.txt').readLines())