Advent of Code 2022 day 10
12/10/2024, 5:00 AMbj0
12/10/2024, 5:23 AMbj0
12/10/2024, 5:23 AMJakub Gwóźdź
12/10/2024, 5:24 AMPetr Sýkora
12/10/2024, 5:38 AMMarcin Wisniowski
12/10/2024, 5:38 AMJakub Gwóźdź
12/10/2024, 5:38 AMMarcin Wisniowski
12/10/2024, 5:39 AMJonathan Kolberg
12/10/2024, 5:41 AMbj0
12/10/2024, 5:54 AMbj0
12/10/2024, 5:56 AMreduce
at the end can be sumOf { it.size }
, didn't think about the fact that different starts will never produce the same path...Renette Ros
12/10/2024, 6:01 AMMichael de Kaste
12/10/2024, 6:08 AMbj0
12/10/2024, 6:10 AMMichael Böiers
12/10/2024, 6:16 AMMichael Böiers
12/10/2024, 6:17 AMwow an easy one on day 10Only if you know the algorithm 🙂
bj0
12/10/2024, 6:20 AMMichael de Kaste
12/10/2024, 6:23 AMMichael de Kaste
12/10/2024, 6:55 AMOzioma Ogbe
12/10/2024, 6:59 AMimport utils.Point
import java.io.File
fun main() {
val input =
File("/Users/oogbe/IdeaProjects/AdventOfCodeSolutions/solutions/src/2024/input.txt").readText().split("\n")
val points = input.mapIndexed { x, s ->
s.mapIndexed { y, c ->
Trail(c.digitToInt(), Point(x, y))
}
}
println(
"Part1: ${
points.flatten().filter { it.value == 0 }.sumOf {
calculateScore(
it, points, mutableSetOf(), true
)
}
}"
)
println(
"Part2: ${
points.flatten().filter { it.value == 0 }.sumOf {
calculateScore(
it, points, mutableSetOf(), false
)
}
}"
)
}
fun calculateScore(trail: Trail, trails: List<List<Trail>>, visited: Set<Point>, part1: Boolean): Int {
if (trail.value == 9) return 1
return listOf(0 to 1, 1 to 0, -1 to 0, 0 to -1).map {
Point(x = trail.position.x + it.first, y = trail.position.y + it.second)
}.mapNotNull {
if (it.x in trails.indices && it.y in trails.first().indices
&& !visited.contains(trails[it.x][it.y].position) && trails[it.x][it.y].value - trail.value == 1
) {
(visited as MutableSet).add(it)
calculateScore(trails[it.x][it.y], trails, if (part1) visited else visited + it, part1)
} else null
}.sum()
}
data class Trail(val value: Int, val position: Point)
El Anthony
12/10/2024, 7:08 AMPaul Woitaschek
12/10/2024, 7:14 AMNeil Banman
12/10/2024, 7:24 AMNeil Banman
12/10/2024, 7:26 AMAnirudh
12/10/2024, 7:34 AMMichael Böiers
12/10/2024, 7:34 AMtrue, but if you've done aoc in the past you've likely seen it many times, it gets used a lotSure, but I remember how I struggled in my first season of AoC. 😆
Michael Böiers
12/10/2024, 7:34 AMephemient
12/10/2024, 7:52 AMAnirudh
12/10/2024, 7:54 AMPoint.neighbours()
function also returns diagonal points so that cost a bit more time)Endre Deak
12/10/2024, 7:55 AMsolve("Hoof It") {
val input = lines
.flatMapIndexed { y, l -> l.mapIndexed { x, c -> Coord(x,y) to c} }
.filter { it.second != '.' }
.map { it.first to it.second.digitToInt() }
.associate { it }
fun Coord.neighbours() =
input
.filterKeys { k -> k in this.NEWS().map { n -> n + this} }
.filterValues { it == input[this]!! + 1 }
.keys
fun bfs(start: Coord): Pair<Int, Int> {
val discovered = mutableSetOf(start)
val q = ArrayDeque<Pair<Coord, List<Coord>>>()
q.add(start to listOf(start))
var count = 0
while (q.isNotEmpty()) {
val (curr, path) = q.removeFirst()
if (input[curr] == 9) { count++ }
curr.neighbours()
.forEach {
if (path.none { d -> d == it }) { q.add(it to path.plus(listOf(it))) }
if (discovered.none { d -> d == it }) { discovered.add(it) }
}
}
return discovered.mapNotNull { input[it] }.count { it == 9 } to count
}
val bfsResult = input.filterValues { it == 0 }.keys.map { bfs(it) }
part1() { bfsResult.sumOf { it.first } }
part2() { bfsResult.sumOf { it.second } }
}
Jakub Gwóźdź
12/10/2024, 7:57 AMephemient
12/10/2024, 7:59 AMvisited
set disappeared after I saw that we were only making 0-1, 1-2, 2-3, etc. steps in both partsephemient
12/10/2024, 8:01 AMJaap Beetstra
12/10/2024, 8:19 AMval route = "0123456789".toList()
fun findPaths(cur: Position, routeIndex: Int): Sequence<List<Position>> = when {
grid[cur] != route[routeIndex] -> emptySequence()
routeIndex >= route.indices.last -> sequenceOf(listOf(cur))
else -> Direction.entries.map { cur + it }.asSequence().flatMap { newPos ->
findPaths(newPos, routeIndex + 1)
}.map { listOf(cur) + it }
}
Dan Fingal-Surma
12/10/2024, 8:34 AMDan Fingal-Surma
12/10/2024, 8:52 AMDan Fingal-Surma
12/10/2024, 8:55 AMDan Fingal-Surma
12/10/2024, 8:55 AMDan Fingal-Surma
12/10/2024, 8:56 AMephemient
12/10/2024, 8:57 AMephemient
12/10/2024, 8:57 AMJakub Gwóźdź
12/10/2024, 8:58 AMJakub Gwóźdź
12/10/2024, 8:59 AMJakub Gwóźdź
12/10/2024, 9:00 AMDan Fingal-Surma
12/10/2024, 9:00 AMDan Fingal-Surma
12/10/2024, 9:00 AMDan Fingal-Surma
12/10/2024, 9:00 AMephemient
12/10/2024, 9:00 AMimport Data.Monoid
import Data.Set qualified as Set
part1 input = sum $ Set.size <$> bfs Set.singleton (parse input)
part2 input = getSum $ mconcat $ Map.elems $ bfs (const $ Sum 1) (parse input)
Haskell has very generic built-in tools for things like functors, semigroups, and monoids, so e.g. if I <>
two `Set`s together, I get their union, and if I <>
two `Sum`s together, I get their sum, while
val values = // parse
fun part1() = bfs(::setOf, Set<IntPair>::plus).values.sumOf { it.size }
fun part2() = bfs({ 1 }, Int::plus).values.sum()
while Kotlin has no such thing and I have to explicitly refer to each type's ::plus
, but the syntax prioritizes having things like setOf
available without additional importsDan Fingal-Surma
12/10/2024, 9:02 AMDan Fingal-Surma
12/10/2024, 9:04 AMzsmb
12/10/2024, 9:46 AMI accidentally implemented part 2 as an error while implementing part 1 🙂Same here, and in the end my solution goes between part1 or part2 depending on whether the class used for storing points is a
data
class or not 😄Michael Böiers
12/10/2024, 9:50 AMritesh
12/10/2024, 9:52 AMkingsley
12/10/2024, 11:03 AMphldavies
12/10/2024, 1:45 PMWarming up 1 puzzles for 10s each for year 2024 day 10...
Default warmed up with 35143 iterations
year 2024 day 10 part 1
Default took 149.695us 👑: 538
year 2024 day 10 part 2
Default took 135.283us 👑: 1110
was happy enough with this solution this morning - but I originally wasted a good 10-15mins fighting the compiler with an FIR lowering exception.Charles Flynn
12/10/2024, 5:55 PMFredrik Rødland
12/10/2024, 11:29 PM