generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay06.kt
153 lines (147 loc) · 5.9 KB
/
Day06.kt
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
fun main() {
val board = readInput("Day06").map { it.toCharArray() }.toTypedArray()
var startx = 0
var starty = 0
for(i in board.indices) {
for(j in board[0].indices) {
if (board[i][j] == '^') {
startx = i
starty = j
break
}
}
}
val n = board.size
val m = board[0].size
println("Found starting position ($startx, $starty)")
var i = startx
var j = starty
var direction = 0 // 0 - up, 1 - down, 2 - left, 3 - right
var visited = mutableSetOf<Pair<Int,Int>>()
while (i in 0..<n && j in 0..<m) {
when {
direction == 0 && i > 0 && board[i - 1][j] == '#' -> direction = 3
direction == 1 && i < n - 1 && board[i + 1][j] == '#' -> direction = 2
direction == 2 && j > 0 && board[i][j - 1] == '#' -> direction = 0
direction == 3 && j < m - 1 && board[i][j + 1] == '#' -> direction = 1
direction == 0 -> visited.add(Pair(i,j)).also{i--}
direction == 1 -> visited.add(Pair(i,j)).also{i++}
direction == 2 -> visited.add(Pair(i,j)).also{j--}
direction == 3 -> visited.add(Pair(i,j)).also{j++}
}
}
println(visited.size)
// part 2
i = startx
j = starty
direction = 0 // 0 - up, 1 - down, 2 - left, 3 - right
val visitedWithDirection = mutableSetOf<Triple<Int,Int,Int>>()
val loopObstacles = mutableSetOf<Pair<Int,Int>>()
val noLoops = mutableSetOf<Pair<Int, Int>>()
while (i in 0..<n && j in 0..<m) {
when {
direction == 0 && i > 0 && board[i - 1][j] != '#' -> {
val obstacle = Pair(i - 1, j)
board[i - 1][j] = '#'
if (obstacle !in noLoops && verifyLoop(i, j, direction, board, visitedWithDirection)) {
loopObstacles.add(obstacle)
} else {
noLoops.add(obstacle)
}
board[i - 1][j] = '.'
}
direction == 1 && i < n - 1 && board[i + 1][j] != '#' -> {
board[i + 1][j] = '#'
val obstacle = Pair(i + 1, j)
if (obstacle !in noLoops && verifyLoop(i, j, direction, board, visitedWithDirection)) {
loopObstacles.add(obstacle)
} else {
noLoops.add(obstacle)
}
board[i + 1][j] = '.'
}
direction == 2 && j > 0 && board[i][j - 1] != '#' -> {
board[i][j - 1] = '#'
val obstacle = Pair(i, j - 1)
if (obstacle !in noLoops && verifyLoop(i, j, direction, board, visitedWithDirection)) {
loopObstacles.add(obstacle)
} else {
noLoops.add(obstacle)
}
board[i][j - 1] = '.'
}
direction == 3 && j < m - 1 && board[i][j + 1] != '#' -> {
board[i][j + 1] = '#'
val obstacle = Pair(i, j + 1)
if (obstacle !in noLoops && verifyLoop(i, j, direction, board, visitedWithDirection)) {
loopObstacles.add(obstacle)
} else {
noLoops.add(obstacle)
}
board[i][j + 1] = '.'
}
}
when {
direction == 0 && i > 0 && board[i - 1][j] == '#' -> {
direction = 3
visitedWithDirection.add(Triple(i,j,0))
}
direction == 1 && i < n - 1 && board[i + 1][j] == '#' -> {
direction = 2
visitedWithDirection.add(Triple(i,j,1))
}
direction == 2 && j > 0 && board[i][j - 1] == '#' -> {
direction = 0
visitedWithDirection.add(Triple(i,j,2))
}
direction == 3 && j < m - 1 && board[i][j + 1] == '#' -> {
direction = 1
visitedWithDirection.add(Triple(i,j,3))
}
direction == 0 -> visitedWithDirection.add(Triple(i,j,0)).also{i--}
direction == 1 -> visitedWithDirection.add(Triple(i,j,1)).also{i++}
direction == 2 -> visitedWithDirection.add(Triple(i,j,2)).also{j--}
direction == 3 -> visitedWithDirection.add(Triple(i,j,3)).also{j++}
}
}
println(loopObstacles.size)
}
fun verifyLoop(startX: Int, startY: Int, startDirection: Int, board: Array<CharArray>, visited: Set<Triple<Int, Int, Int>>): Boolean {
val visitedWithDirection = mutableSetOf<Triple<Int,Int,Int>>()
visitedWithDirection.addAll(visited)
val n = board.size
val m = board[0].size
var i = startX
var j = startY
var direction = startDirection
var iter = 0
while (i in 0..<n && j in 0..<m) {
iter++
if (visitedWithDirection.contains(Triple(i,j,direction))) {
return true
}
when {
direction == 0 && i > 0 && board[i - 1][j] == '#' -> {
direction = 3
visitedWithDirection.add(Triple(i,j,0))
}
direction == 1 && i < n - 1 && board[i + 1][j] == '#' -> {
direction = 2
visitedWithDirection.add(Triple(i,j,1))
}
direction == 2 && j > 0 && board[i][j - 1] == '#' -> {
direction = 0
visitedWithDirection.add(Triple(i,j,2))
}
direction == 3 && j < m - 1 && board[i][j + 1] == '#' -> {
direction = 1
visitedWithDirection.add(Triple(i,j,3))
}
direction == 0 -> visitedWithDirection.add(Triple(i,j,0)).also{i--}
direction == 1 -> visitedWithDirection.add(Triple(i,j,1)).also{i++}
direction == 2 -> visitedWithDirection.add(Triple(i,j,2)).also{j--}
direction == 3 -> visitedWithDirection.add(Triple(i,j,3)).also{j++}
}
}
return false
}