Advent of Code 2021 day 12
12/12/2022, 5:00 AMKroppeb
12/12/2022, 5:16 AMDan Fingal-Surma
12/12/2022, 5:16 AMRafal Bednarczuk
12/12/2022, 5:36 AMMichael de Kaste
12/12/2022, 5:38 AMMarcin Wisniowski
12/12/2022, 5:49 AMNeil Banman
12/12/2022, 5:55 AMJonathan Kolberg
12/12/2022, 5:58 AMRafal Bednarczuk
12/12/2022, 5:59 AMJonathan Kolberg
12/12/2022, 6:11 AMJonathan Kolberg
12/12/2022, 6:32 AMJonathan Kolberg
12/12/2022, 6:36 AMMichael de Kaste
12/12/2022, 6:37 AMtypealias Point = Pair<Int, Int>
fun Point.neighbours() = sequenceOf(
first - 1 to second,
first to second + 1,
first + 1 to second,
first to second - 1
)
object Day12 : Challenge() {
private lateinit var startPoint: Point
private lateinit var endPoint: Point
val parsed = input.lines().withIndex().flatMap { (y, line) ->
line.withIndex().map { (x, char) ->
val point = y to x
when (char) {
'S' -> point to 'a'.also { startPoint = point }
'E' -> point to 'z'.also { endPoint = point }
else -> point to char
}
}
}.toMap()
private val countMap = buildMap {
var count = 0
var currentCandidates = setOf(endPoint)
while (currentCandidates.isNotEmpty()) {
currentCandidates = buildSet {
for (candidate in currentCandidates) {
if (putIfAbsent(candidate, count) != null) continue
val value = parsed.getValue(candidate)
for (neighbour in candidate.neighbours()) {
parsed[neighbour]?.also {
if (value - it <= 1) {
add(neighbour)
}
}
}
}
}
count++
}
}
override fun part1() = countMap[startPoint]
override fun part2() = parsed.filterValues('a'::equals)
.filterKeys(countMap::containsKey)
.keys.minOf(countMap::getValue)
}
I built a countMap so I can just get all the points of interest in hindsight :)Dan Fingal-Surma
12/12/2022, 6:41 AMrkechols
12/12/2022, 6:42 AMDan Fingal-Surma
12/12/2022, 6:42 AMDan Fingal-Surma
12/12/2022, 6:42 AMDan Fingal-Surma
12/12/2022, 6:42 AMJakub Gwóźdź
12/12/2022, 6:54 AMMichael de Kaste
12/12/2022, 6:59 AMDan Fingal-Surma
12/12/2022, 7:00 AMDan Fingal-Surma
12/12/2022, 7:01 AMJakub Gwóźdź
12/12/2022, 7:01 AMDan Fingal-Surma
12/12/2022, 7:01 AMDan Fingal-Surma
12/12/2022, 7:01 AMDan Fingal-Surma
12/12/2022, 7:01 AMCognitive Gear
12/12/2022, 7:01 AMMichael de Kaste
12/12/2022, 7:03 AMDan Fingal-Surma
12/12/2022, 7:03 AMDan Fingal-Surma
12/12/2022, 7:04 AMMichael de Kaste
12/12/2022, 7:04 AMJakub Gwóźdź
12/12/2022, 7:04 AMCognitive Gear
12/12/2022, 7:05 AMDan Fingal-Surma
12/12/2022, 7:05 AMDan Fingal-Surma
12/12/2022, 7:06 AMMichael de Kaste
12/12/2022, 7:07 AMNeil Banman
12/12/2022, 7:07 AMMichael de Kaste
12/12/2022, 7:07 AMDan Fingal-Surma
12/12/2022, 7:08 AMDan Fingal-Surma
12/12/2022, 7:09 AMMichael de Kaste
12/12/2022, 7:10 AMMichael de Kaste
12/12/2022, 7:10 AMNeil Banman
12/12/2022, 7:11 AMDan Fingal-Surma
12/12/2022, 7:12 AMJakub Gwóźdź
12/12/2022, 7:18 AMpossibleUp
output by the distance to “E”, but it does not seem to be worth the effort.Sergei Petunin
12/12/2022, 7:22 AMDan Fingal-Surma
12/12/2022, 7:23 AMProbably I could gain something by sorting the possibleUp output by the distance to "E", but it does not seem to be worth the effort.
— then you have A* hahaDan Fingal-Surma
12/12/2022, 7:24 AMephemient
12/12/2022, 7:25 AMephemient
12/12/2022, 7:26 AMb - a <= 1
(where a: Char
, b: Char
) as my condition, but that allows for stepping from 'x' -> 'E'
. took me waaay too long to track down…ephemient
12/12/2022, 7:27 AM'a'
. it worked but I realized soon after that I could just run the search in reverseephemient
12/12/2022, 7:28 AMJakub Gwóźdź
12/12/2022, 7:33 AM.isLowerCase
🙂Lidonis Calhau
12/12/2022, 7:37 AMDan Fingal-Surma
12/12/2022, 7:42 AMDan Fingal-Surma
12/12/2022, 7:42 AMfun findShortedPath(target: Char): Int? {
val end = vertexes.flatten().single { it.code == 'E' }
val frontier = ArrayDeque<Pair<Vertex, Int>>(listOf(end to 0))
val seen = mutableSetOf<Vertex>(end)
while (!frontier.isEmpty()) {
val (vertex, distance) = frontier.removeFirst()
if (vertex.code == target) return distance
vertex.neighbors().filter { it.height + 1 >= vertex.height }.forEach {
if (seen.add(it)) frontier.add(it to distance + 1)
}
}
return null
}
Neil Banman
12/12/2022, 7:52 AMI don't think there was ever a year A* was needed I think, BFS hath always been enoughThere have definitely been weighted graphs where regular BFS won't work. 2018 Day 22 being a good example. But you're right that A* has never been needed. I only used it twice (2018 Day 22 and 2021 Day 15) and it only made a big change for the 2018 one.
zsmb
12/12/2022, 7:52 AMDan Fingal-Surma
12/12/2022, 7:54 AMNeil Banman
12/12/2022, 7:56 AMOzioma Ogbe
12/12/2022, 8:00 AMDavio
12/12/2022, 8:25 AMDavio
12/12/2022, 8:25 AMhttps://i.kym-cdn.com/photos/images/original/002/472/808/623.png▾
Davio
12/12/2022, 8:31 AMDavio
12/12/2022, 8:35 AMSergei Petunin
12/12/2022, 9:13 AMPaul Woitaschek
12/12/2022, 9:23 AMDavio
12/12/2022, 9:34 AMDavio
12/12/2022, 9:35 AMDavio
12/12/2022, 9:36 AMDavio
12/12/2022, 9:42 AMFredrik Rødland
12/12/2022, 10:16 AMDavio
12/12/2022, 10:29 AMDavio
12/12/2022, 10:29 AMOzioma Ogbe
12/12/2022, 10:40 AMDavio
12/12/2022, 10:40 AM0.8
to make it look more like an actual square, your choice 🙂Fredrik Rødland
12/12/2022, 10:46 AMFredrik Rødland
12/12/2022, 10:48 AMDavio
12/12/2022, 10:50 AMDavio
12/12/2022, 10:52 AMKroppeb
12/12/2022, 10:55 AMDavio
12/12/2022, 10:57 AMDavio
12/12/2022, 10:58 AMCharles Flynn
12/12/2022, 11:06 AMMarcin Wisniowski
12/12/2022, 11:10 AMFredrik Rødland
12/12/2022, 11:12 AMDavio
12/12/2022, 11:14 AMritesh
12/12/2022, 11:18 AMBFS
https://github.com/ritesh-singh/aoc-2022-kotlin/blob/main/src/day12/Day12.kt
But - i had missed this part initially and only read the statement in bold
This also means that the elevation of the destination square can be much lower than the elevation of your current square.
Alex J.
12/12/2022, 12:15 PMritesh
12/12/2022, 12:16 PMthe elevation of the destination square can be at most one higher
Alex J.
12/12/2022, 12:18 PMritesh
12/12/2022, 12:19 PMAlex J.
12/12/2022, 12:22 PMAlex J.
12/12/2022, 12:22 PMritesh
12/12/2022, 12:24 PMzsmb
12/12/2022, 12:24 PME
) has elevation z
."
This got me too, made 2 extra steps because of it...Alex J.
12/12/2022, 12:26 PMAlex J.
12/12/2022, 12:26 PMDavio
12/12/2022, 12:28 PMFredrik Rødland
12/12/2022, 12:33 PMgrid.printRoute(
{ p -> (grid[p] - 'a'.code).toDouble() / ('z'.code - 'a'.code) },
{ p: Pos -> if (p in shortestPath) Char(grid[p]) else ' ' }
)
Davio
12/12/2022, 12:35 PMphldavies
12/12/2022, 1:41 PMKarloti
12/12/2022, 2:55 PMNeil Banman
12/12/2022, 3:59 PMephemient
12/12/2022, 4:20 PMFredrik Rødland
12/12/2022, 4:25 PMephemient
12/12/2022, 4:30 PME
and finding the shortest path to S
or a
. if we just keep going until we find both, the total time taken is less than searching from S
to E
- at least on my inputNeil Banman
12/12/2022, 4:55 PMephemient
12/12/2022, 5:06 PMEzxy...cbbaS
........a...
the first a
along the path to S
is not the nearest a
ephemient
12/12/2022, 5:07 PME
, you do visit the first a
first, and then the S
. since I'm always looking for S
anyway, finding the a
during the search means I don't have to do an additional search for itFredrik Rødland
12/12/2022, 5:08 PMephemient
12/12/2022, 5:09 PMNeil Banman
12/12/2022, 5:14 PMtodd.ginsberg
12/12/2022, 5:19 PMNeil Banman
12/12/2022, 5:21 PMAbdul Khaliq
12/18/2022, 2:25 PMMarcin Wisniowski
12/19/2022, 8:19 AM