Skip to content

Commit 355453d

Browse files
committed
2024/day 06 - part 2
1 parent 696c457 commit 355453d

File tree

2 files changed

+92
-5
lines changed

2 files changed

+92
-5
lines changed

src/main/kotlin/me/underlow/advent2024/Day06.kt

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ object GuardGallivant {
5151
if (list[next.row][next.col] != '#') {
5252
start = next
5353
visited += start
54-
println("${visited.size} Step to $start")
54+
// println("${visited.size} Step to $start")
5555
continue
5656
}
5757

5858
dir = dir.rotateRight()
59-
println("Rotate to $dir")
59+
// println("Rotate to $dir")
6060
}
6161

6262

@@ -65,7 +65,94 @@ object GuardGallivant {
6565

6666
fun part2(list: List<String>): Int {
6767
val directions = parseInput(list)
68-
return 0
68+
69+
var start: Point? = null
70+
var dir: Dir? = null
71+
for (i in list.indices) {
72+
for (j in list[0].indices)
73+
when {
74+
list[i][j] == '^' -> {
75+
start = Point(i, j)
76+
dir = Dir.Up
77+
}
78+
79+
list[i][j] == '>' -> {
80+
start = Point(i, j)
81+
dir = Dir.Right
82+
}
83+
84+
list[i][j] == 'v' -> {
85+
start = Point(i, j)
86+
dir = Dir.Down
87+
}
88+
89+
list[i][j] == '<' -> {
90+
start = Point(i, j)
91+
dir = Dir.Left
92+
}
93+
}
94+
}
95+
96+
var sum = 0
97+
98+
for (i in list.indices) {
99+
for (j in list[0].indices) {
100+
if (i == start!!.col && j == start.row) {
101+
continue
102+
}
103+
if (directions[i][j] == '#') {
104+
continue
105+
}
106+
107+
val saved = directions[i][j]
108+
directions[i][j] = '#'
109+
// println("Try: $i $j")
110+
sum += if (loop(start, directions, dir)) 1 else 0
111+
112+
directions[i][j] = saved
113+
114+
115+
}
116+
}
117+
118+
return sum
119+
}
120+
121+
private fun loop(
122+
start: Point?,
123+
list: Array<Array<Char>>,
124+
dir: Dir?
125+
): Boolean {
126+
var start1 = start
127+
var dir1 = dir
128+
val visited = mutableSetOf<Pair<Point, Dir>>()
129+
visited += (start1!! to dir1!!)
130+
131+
while (start1!!.row in list.indices && start1.col in list[0].indices) {
132+
//make a step
133+
val next = start1.move(dir1!!)
134+
135+
if ((next to dir1) in visited) {
136+
// loop
137+
// println("Loop: $next to $dir of $visited")
138+
return true
139+
}
140+
141+
if (!(next.row in list.indices && next.col in list[0].indices)) {
142+
break
143+
}
144+
145+
if (list[next.row][next.col] != '#') {
146+
start1 = next
147+
visited += start1 to dir1
148+
// println("${visited.size} Step to $start1")
149+
continue
150+
}
151+
152+
dir1 = dir1.rotateRight()
153+
// println("Rotate to $dir1")
154+
}
155+
return false
69156
}
70157

71158
private fun parseInput(list: List<String>): Array<Array<Char>> {
@@ -80,7 +167,7 @@ fun main() {
80167
val res2 = GuardGallivant.part2(input)
81168

82169
checkResult(res1, 4722)
83-
checkResult(res2, 0)
170+
checkResult(res2, 1602)
84171

85172
println(res1)
86173
println(res2)

src/test/kotlin/me/underlow/advent2024/GuardGallivantTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class GuardGallivantTest {
1111
@Test
1212
fun testPart2() {
1313
val result = GuardGallivant.part2(input.split("\n"))
14-
assertEquals(0, result)
14+
assertEquals(6, result)
1515
}
1616
}
1717

0 commit comments

Comments
 (0)