Advent of Code 2023 day 11
12/11/2023, 5:00 AMDan Fingal-Surma
12/11/2023, 5:30 AMDan Fingal-Surma
12/11/2023, 5:36 AMDan Fingal-Surma
12/11/2023, 5:38 AMe - 1
part was the off by one error, e
= expansion factor)Tomasz Linkowski
12/11/2023, 5:38 AMDan Fingal-Surma
12/11/2023, 5:39 AMMarcin Wisniowski
12/11/2023, 5:55 AMbj0
12/11/2023, 5:58 AMNorbert Kiesel
12/11/2023, 6:01 AMLidonis Calhau
12/11/2023, 6:07 AMJonathan Kolberg
12/11/2023, 6:07 AMNeil Banman
12/11/2023, 6:07 AMephemient
12/11/2023, 6:08 AMLidonis Calhau
12/11/2023, 6:10 AMDan Fingal-Surma
12/11/2023, 6:14 AMfun makePoint(x: Int, y: Int) = Point(
x + colsToExpand.count { x > it },
y + rowsToExpand.count { y > it }
)
so then I did count times 1m which is wrongMarcin Wisniowski
12/11/2023, 6:16 AMNorbert Kiesel
12/11/2023, 6:17 AMbj0
12/11/2023, 6:21 AMval ys = lines.indices.filter { lines[it].all { c -> c == '.' } }
val xs = lines.first().indices.filter { lines.all { line -> line[it] == '.' } }
fun expandedX(x: Long) = x + xs.count { it < x } * (n - 1)
fun expandedY(y: Long) = y + ys.count { it < y } * (n - 1)
buildList {
lines.forEachIndexed { row, line ->
line.forEachIndexed { col, c ->
if (c == '#')
add(PointL(expandedX(col.toLong()), expandedY(row.toLong())))
}
}
}
sequence {
val seen = mutableSetOf<PointL>()
galaxies.forEach { p ->
seen += p
yieldAll((galaxies - seen).map { p to it })
}
}.sumOf { (a, b) -> (a mdist b) }
Riccardo Lippolis
12/11/2023, 6:49 AMephemient
12/11/2023, 6:54 AMNorbert Kiesel
12/11/2023, 7:13 AMephemient
12/11/2023, 7:14 AMMichael de Kaste
12/11/2023, 7:31 AMobject Day11 : Challenge() {
private val parsed = input.lines()
.flatMapIndexed { y: Int, s: String -> s.mapIndexedNotNull { x, c -> (y to x).takeIf { c == '#' } } }
private fun holes(selector: (Pair<Int, Int>) -> Int) = parsed.asSequence()
.map(selector)
.sorted()
.distinct()
.zipWithNext { a, b -> b to b - a - 1 }
.toMap(TreeMap())
override fun part1() = solve(2)
override fun part2() = solve(1000000)
private fun solve(expansionAmount: Int): Long {
val holesY = holes { it.first }
val holesX = holes { it.second }
val newPlaces = parsed.map { (y, x) ->
val holesBeforeY = holesY.headMap(y, true).values.sum()
val holesBeforeX = holesX.headMap(x, true).values.sum()
y + holesBeforeY * (expansionAmount - 1) to x + holesBeforeX * (expansionAmount - 1)
}
return newPlaces.flatMap { (y1, x1) -> newPlaces.map { (y2, x2) -> abs(y2 - y1) + abs(x2 - x1) } }
.sumOf { it.toLong() } / 2
}
}
elizarov
12/11/2023, 7:35 AMCheckedInt
as a library is an option, but it would be, inevitable, somewhat cumbersome to use, as it is not a default.elizarov
12/11/2023, 7:36 AMDan Fingal-Surma
12/11/2023, 7:36 AMelizarov
12/11/2023, 7:42 AMDan Fingal-Surma
12/11/2023, 7:48 AMJaap Beetstra
12/11/2023, 8:08 AMpart2(input, 999_999).println()
Jakub Gwóźdź
12/11/2023, 8:24 AMLong
was not much of an issue (I expected that as soon as I looked at input data), but the ugliness came with indexing operator []
, which enforces Int
so I had to cast back like rows[r.toInt()] to cols[c.toInt()]
ephemient
12/11/2023, 8:37 AMcheckedAdd
isn't an issue, it exists in standard Java as Math.addExact
. that doesn't solve how to make that ergonomic (so you can write code using the normal +
etc. operators)ephemient
12/11/2023, 8:41 AM@CheckedArithmetic(mode = CheckedArithmetic.Mode.CHECKED)
fun thisWillThrow(): Int {
for (x in listOf(1)) return x + Int.MAX_VALUE
}
@CheckedArithmetic(mode = CheckedArithmetic.Mode.UNCHECKED)
fun thisWillWrap(): Int {
for (x in listOf(1)) return x + Int.MAX_VALUE
}
but because of the layer it's working at, it can't apply to specific blocks or expressions within a function, it potentially breaks things that are inlined (like unsigned integers), and it can't apply to Kotlin lambdas. that's something the compiler could do properly, thoughKroppeb
12/11/2023, 8:50 AMvalue == 0
, I need to use an extension property to get it to work value == 0.s
Marcin Wisniowski
12/11/2023, 8:51 AMequals
on your Sint
that works with normal integers?ephemient
12/11/2023, 8:52 AMKroppeb
12/11/2023, 8:52 AMvalue == 0
but not with 0 == value
, but I also made them inline classes, so I can't override equalsephemient
12/11/2023, 8:56 AMKroppeb
12/11/2023, 8:57 AMephemient
12/11/2023, 8:59 AMandriyo
12/11/2023, 9:12 AMreduce
instead of sum
Also used TreeMap for faster lookup, so there is a binary search somewhereephemient
12/11/2023, 9:18 AMsum
works fine on Long
andriyo
12/11/2023, 9:20 AMandriyo
12/11/2023, 9:22 AMLong
.. count()
?Kroppeb
12/11/2023, 9:23 AMcount
always returns an int. But you also can't have lists that are longer than the max integerandriyo
12/11/2023, 9:24 AMPaul Woitaschek
12/11/2023, 9:24 AMephemient
12/11/2023, 9:33 AMInt.MAX_VALUE
it would take a long time to count()
that high…Anirudh
12/11/2023, 9:46 AMpart2(input, 999_999).println()
@Jaap Beetstra I did exactly the same 😂
just to test part1 with part2's math, I did Day11(testInput).partTwo(1)
and it's not an off-by-one when the multiplier is 1 🤔
I suppose "double" means factor = 2, or add = 1.
so if we made the corresponding change to both parts, then 1_000_000 would be correctandriyo
12/11/2023, 9:50 AMephemient
12/11/2023, 9:55 AM.count().minus(1).toLong()
would have worked fineandriyo
12/11/2023, 9:59 AMPoisonedYouth
12/11/2023, 11:54 AMJakub Gwóźdź
12/11/2023, 12:19 PMKash Kabeya
12/11/2023, 12:29 PMelizarov
12/11/2023, 12:45 PMMichael de Kaste
12/11/2023, 12:46 PMKroppeb
12/11/2023, 12:46 PMCharles Flynn
12/11/2023, 12:51 PMelizarov
12/11/2023, 12:52 PMMichael Böiers
12/11/2023, 1:38 PMMichael Böiers
12/11/2023, 2:06 PMtodd.ginsberg
12/11/2023, 5:58 PMephemient
12/11/2023, 6:07 PMNorbert Kiesel
12/11/2023, 6:07 PMephemient
12/11/2023, 6:07 PMMatthias T
12/11/2023, 6:09 PMDan Fingal-Surma
12/11/2023, 6:16 PMDan Fingal-Surma
12/11/2023, 6:17 PMbj0
12/11/2023, 6:19 PMDan Fingal-Surma
12/11/2023, 6:20 PMNorbert Kiesel
12/11/2023, 6:20 PMDan Fingal-Surma
12/11/2023, 6:20 PMephemient
12/11/2023, 6:20 PMDan Fingal-Surma
12/11/2023, 6:21 PMtodd.ginsberg
12/11/2023, 6:21 PMephemient
12/11/2023, 6:22 PMephemient
12/11/2023, 6:22 PMephemient
12/11/2023, 6:23 PMDan Fingal-Surma
12/11/2023, 6:23 PMephemient
12/11/2023, 6:23 PMritesh
12/11/2023, 6:24 PMephemient
12/11/2023, 6:24 PMa * b
multiplication in my current solution (subject to change when I optimize later though)todd.ginsberg
12/11/2023, 7:04 PMNeil Banman
12/11/2023, 11:58 PMxxfast
12/14/2023, 1:44 PMNeil Banman
12/14/2023, 5:27 PM