-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18-1.groovy
127 lines (120 loc) · 4.01 KB
/
day18-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
def sampleInput = '''R 6 (#70c710)
D 5 (#0dc571)
L 2 (#5713f0)
D 2 (#d2c081)
R 2 (#59c680)
D 2 (#411b91)
L 5 (#8ceee2)
U 2 (#caa173)
L 1 (#1b58a2)
U 2 (#caa171)
R 2 (#7807d2)
U 3 (#a77fa3)
L 2 (#015232)
U 2 (#7a21e3)'''.split('\n') as List<String>
def input = new File('inputs/day18-1.txt').readLines()
PATTERN = ~/([RDLU]) (\d+).*/
def areThereMoreRightsThanLefts(steps) {
def lefts = 0
def rights = 0
def prev = steps.get(0)
steps.drop(1).each { step ->
if(prev == 'R' && step[0] == 'D'
|| prev == 'D' && step[0] == 'L'
|| prev == 'L' && step[0] == 'U'
|| prev == 'U' && step[0] == 'R') {
rights++
} else {
lefts++
}
prev = step[0]
}
println "L: $lefts R: $rights"
rights > lefts
}
def bfs(map, interior) {
def dirs = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0]
]
def queue = new LinkedList(interior)
while(!queue.isEmpty()) {
def top = queue.pop()
if(map[top[0]][top[1]] != '#') {
for(def dir in dirs) {
def next = [top[0]+dir[0], top[1]+dir[1]]
if (!interior.contains(next) && map[next[0]][next[1]] == '.') {
queue.add(next)
interior.add(next)
}
}
}
}
interior.size()
}
def createMap(steps) {
def map = [:].withDefault { [:].withDefault { '.' } }
def right = areThereMoreRightsThanLefts(steps)
def interior = new HashSet()
def topValues = steps.inject([[0, 0], [0, 0], [0, 0]]) { result, step ->
def pos = result[0]
if(right) {
switch(step[0]) {
case 'R' -> (1..(step[1])).each { map[pos[0]][pos[1]+it] = '#'; interior << [pos[0]+1, pos[1]+it] ; println "$pos $it" }
case 'D' -> (1..(step[1])).each { map[pos[0]+it][pos[1]] = '#'; interior << [pos[0]+it, pos[1]-1] }
case 'L' -> (1..(step[1])).each { map[pos[0]][pos[1]-it] = '#'; interior << [pos[0]-1, pos[1]-it] }
case 'U' -> (1..(step[1])).each { map[pos[0]-it][pos[1]] = '#'; interior << [pos[0]-it, pos[1]+1] }
}
} else {
switch(step[0]) {
case 'R' -> (1..(step[1])).each { map[pos[0]][pos[1]+it] = '#'; interior << [pos[0]-1, pos[1]+it] }
case 'D' -> (1..(step[1])).each { map[pos[0]+it][pos[1]] = '#'; interior << [pos[0]+it, pos[1]+1] }
case 'L' -> (1..(step[1])).each { map[pos[0]][pos[1]-it] = '#'; interior << [pos[0]+1, pos[1]-it] }
case 'U' -> (1..(step[1])).each { map[pos[0]-it][pos[1]] = '#'; interior << [pos[0]-it, pos[1]-1] }
}
}
switch(step[0]) {
case 'R' -> pos[1] += step[1]
case 'D' -> pos[0] += step[1]
case 'L' -> pos[1] -= step[1]
case 'U' -> pos[0] -= step[1]
}
result[1][0] = Math.min(result[1][0], pos[0])
result[1][1] = Math.min(result[1][1], pos[1])
result[2][0] = Math.max(result[2][0], pos[0])
result[2][1] = Math.max(result[2][1], pos[1])
map.keySet().each { println "${it.getClass()}" }
result
}
interior = interior.findAll { map[it[0]][it[1]] != '#' }
(topValues[1][0]..topValues[2][0]).each { i ->
(topValues[1][1]..topValues[2][1]).each { j ->
print map[i][j]
}
println()
}
[map, interior, topValues]
}
def solve(List<String> lines) {
def steps = lines.collect { line ->
def match = (line =~ PATTERN)
assert match.matches()
[match.group(1), match.group(2) as int]
}
def (map, interior, topValues) = createMap(steps)
def area = bfs(map, interior) + steps.collect { it[1] }.sum()
interior.each {
map[it[0]][it[1]] = '#'
}
(topValues[1][0]..topValues[2][0]).each { i ->
(topValues[1][1]..topValues[2][1]).each { j ->
print map[i][j]
}
println()
}
area
}
assert 62 == solve(sampleInput)
println solve(input)