Advent of Code 2024 - Day 6 - Guard Gallivant

  Рет қаралды 232

Daniel Persson (Kalaspuffar)

Daniel Persson (Kalaspuffar)

Күн бұрын

I try to solve Advent of code puzzles during the 2024 event. Please share your solutions and be a part of this great community.
Repository:
github.com/kal...
Leverage AI in your software development workflow. Enhance your skills as a software developer, grow your career, and stay competitive in this fast-paced industry.
imp.i384100.ne...
Kubernetes at scale managed for you!
Use DigitalOcean Cluster AutoScaler to scale your clusters to seamlessly ensure performance during peak times.
digitalocean.p...
Support the channel and donate a coffee:
ko-fi.com/dani...
Or visit my blog at:
danielpersson.dev
Outro music: Sanaas Scylla
#java #adventofcode #2024

Пікірлер: 7
@aaronperl
@aaronperl 8 күн бұрын
My solution for Part B is still running so I don't know yet if it works.... it looks like I'm taking a mostly-similar approach to you, but instead of just tracking the visited positions, I'm also tracking the direction that the guard travelled through each position. Then, if the guard moves into a visited position in the same direction as before, the guard should be entering a loop, so I increase the counter and move on to the next attempt. I still feel like there should be a better way to do this, because it always seems like "the right" algorithm to every AoC problem ends up giving the result in milliseconds. ... And now it looks like my code just got stuck, so I guess there is still more for me to figure out....
@DanielPersson
@DanielPersson 7 күн бұрын
Hi Aaron. This one was something that stumped me. And I think there is a really elegant solution if you know more about AI, math or graphics I guess. But I think making the guard run is kinda a good solution. The map is really small; maybe you could load it into a graphics card and use the thousands of cores there to do small, quick calculations. Thank you for being a part of this event every year and the community at large. Best regards Daniel
@aaronperl
@aaronperl 7 күн бұрын
@@DanielPersson As I thought about the problem a bit more, I realized my error: it's possible to cross the same spot in different directions, so my simple loop detection didn't work; I needed to track all the directions at each visited position. Once I did that, I was able to get the correct solution (but it still took a few minutes to run .... but it's good enough for AoC :) )
@WayneBagguley
@WayneBagguley 8 күн бұрын
SPOILER: Don't read this if you haven't figured it out yet but it looks like you are always moving forward after hitting an obstacle and turning, so if you have another obstacle immediately in front of you after turning it will move to that position and you will get an incorrect result. To detect a loop you need to keep a set of visited position-direction pairs, if your current position and direction pair is in the set then it's a loop.
@DanielPersson
@DanielPersson 8 күн бұрын
Hi Wayne. Thank you for the hint, I was totally stumped and the simple thing I missed was that I could turn multiple times and that is in hindsight obvious. Best regards Daniel
@WayneBagguley
@WayneBagguley 8 күн бұрын
Part 1: fun calc(input: List): Int { var current = '^' var direction = 0 to -1 var position = input.flatMapIndexed { y, s -> s.mapIndexed { x, c -> x to c }.filter { it.second == '^' }.map { it.first to y } }.first() val visited: MutableSet = mutableSetOf(position) while (current != 'X') { val next = input.getOrNull(position.second + direction.second)?.getOrNull(position.first + direction.first) ?: 'X' when (next) { '.', '^' -> { position = position.first + direction.first to position.second + direction.second visited.add(position) current = next } '#' -> direction = -direction.second to direction.first else -> current = 'X' } } return visited.size }
@WayneBagguley
@WayneBagguley 8 күн бұрын
Part 2: fun calc(input: List): Int { val start = input.flatMapIndexed { y, s -> s.mapIndexed { x, c -> x to c }.filter { it.second == '^' }.map { it.first to y } }.first() val dots = input.flatMapIndexed { y, s -> s.mapIndexed { x, c -> x to c }.filter { it.second == '.' }.map { it.first to y } } return dots.map { d -> input.toMutableList().apply { this[d.second] = StringBuilder(this[d.second]) .also { it.setCharAt(d.first, '#') }.toString() } }.count { !doesExit(it, start) } } private fun doesExit(input: List, start: Pair): Boolean { var position = start var current = '^' var direction = 0 to -1 val visited: MutableSet = mutableSetOf((0 to -1) to start) while (current != 'X') { val next = input.getOrNull(position.second + direction.second)?.getOrNull(position.first + direction.first) ?: 'X' when (next) { '.', '^' -> { position = position.first + direction.first to position.second + direction.second if (position to direction in visited) return false visited.add(position to direction) current = next } '#' -> direction = -direction.second to direction.first else -> current = 'X' } } return true }
Advent of Code 2024 - Day 7 - Bridge Repair
21:15
Daniel Persson (Kalaspuffar)
Рет қаралды 349
Day 6: Guard Gallivant | Advent of Code 2024
23:39
William Y. Feng
Рет қаралды 2,3 М.
Арыстанның айқасы, Тәуіржанның шайқасы!
25:51
QosLike / ҚосЛайк / Косылайық
Рет қаралды 681 М.
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 38 МЛН
Lamborghini vs Smoke 😱
00:38
Topper Guild
Рет қаралды 69 МЛН
So Cute 🥰 who is better?
00:15
dednahype
Рет қаралды 18 МЛН
Advent of Code 2024 - Day 15 - Warehouse Woes
1:05:36
Daniel Persson (Kalaspuffar)
Рет қаралды 12
I blew it (Advent of Code 2024 Day 1 Pt. 1)
6:56
denvaar
Рет қаралды 2,5 М.
Day 1 - Advent of Code 2024 (Solution)
21:49
James Peralta
Рет қаралды 301
Advent of Code 2024 - Day 9 - Disk Fragmenter
39:31
Daniel Persson (Kalaspuffar)
Рет қаралды 405
Advent of Code 2024 - Day 10 - Hoof It
31:48
Daniel Persson (Kalaspuffar)
Рет қаралды 161
Advent of Code 2024 - Day 8 - Resonant Collinearity
25:35
Daniel Persson (Kalaspuffar)
Рет қаралды 227
Day 2 | Advent of Code 2024 | Better Time Complexity
15:14
Errichto Algorithms
Рет қаралды 10 М.
Advent of Code 2024 - Day 12 - Garden Groups
34:27
Daniel Persson (Kalaspuffar)
Рет қаралды 208
Advent of Code 2024 Day 6: Guard Gallivant in C# ✅✅
34:15
Martin Zikmund
Рет қаралды 475
I Made an Electronic Chessboard Without Turns
14:32
From Scratch
Рет қаралды 952 М.
Арыстанның айқасы, Тәуіржанның шайқасы!
25:51
QosLike / ҚосЛайк / Косылайық
Рет қаралды 681 М.