Advent of Code 2021 day 5
12/05/2021, 5:00 AMDavid Whittaker
12/05/2021, 5:48 AMSimon Nyström
12/05/2021, 6:57 AMelizarov
12/05/2021, 8:26 AMDavid Whittaker
12/05/2021, 8:33 AMelizarov
12/05/2021, 8:40 AMelizarov
12/05/2021, 8:44 AMMarcin Wisniowski
12/05/2021, 8:44 AMMichael Böiers
12/05/2021, 9:28 AMMichael Böiers
12/05/2021, 9:47 AMfun main() = generateSequence(::readLine).toList().day05().let { (p1, p2) -> println(p1); println(p2) }
fun List<String>.day05() = map { row -> row.replace(" -> ", ",").split(",").map { it.toInt() } }
.let { listOf(it.filter { (x1, y1, x2, y2) -> x1 == x2 || y1 == y2 }, it) }
.map { lines -> lines.points().groupingBy { it }.eachCount().count { it.value > 1 } }
private fun List<List<Int>>.points() = flatMap { (x1, y1, x2, y2) ->
sequence {
var pos = x1 to y1
while (true) {
yield(pos)
if (pos == x2 to y2) break
pos = pos.let { (x1, y1) -> closerTo(x1, x2) to closerTo(y1, y2) }
}
}
}
private fun closerTo(a: Int, b: Int) = when {
a > b -> a - 1
a < b -> a + 1
else -> a
}
ephemient
12/05/2021, 10:40 AMPoisonedYouth
12/05/2021, 10:55 AMPaul Woitaschek
12/05/2021, 10:58 AMEmil Kantis
12/05/2021, 11:21 AMEdgars
12/05/2021, 11:37 AMMichael de Kaste
12/05/2021, 12:16 PMMichael de Kaste
12/05/2021, 12:33 PMtypealias Point = Pair<Int, Int>
operator fun Point.rangeTo(other: Point): List<Point>{
val yDiv = other.first - first
val xDiv = other.second - second
return List(maxOf(abs(yDiv), abs(xDiv)) + 1) { index -> first + yDiv.sign * index to second + xDiv.sign * index }
}
object Day5 : Challenge("--- Day 5: Hydrothermal Venture ---") {
val parsed = input.split("""[^(\d+)]+""".toRegex()).map(String::toInt).chunked(4)
override fun part1() = solve(diagonals = false)
override fun part2() = solve(diagonals = true)
private fun solve(diagonals: Boolean) = parsed
.filter { (x1, y1, x2, y2) -> diagonals || x1 == x2 || y1 == y2 }
.flatMap { (x1, y1, x2, y2) -> (y1 to x1)..(y2 to x2) }
.groupingBy { it }.eachCount().values.count { it > 1 }
}
kqr
12/05/2021, 1:12 PMTies
12/05/2021, 2:18 PMPoisonedYouth
12/05/2021, 3:31 PMMarcin Wisniowski
12/05/2021, 4:28 PM.map { .. }.flatten()
could just be .flatMap { .. }
todd.ginsberg
12/05/2021, 4:29 PMmapNotNull
before and it didn’t have a flatMapNotNull
and when I changed my impl to not have a null list (refactoring!) I just spaced on that. Thanks Marcin. Racing to get out the door but I’ll correct it later! 🙂PoisonedYouth
12/05/2021, 4:30 PMephemient
12/05/2021, 5:39 PMflatMapNotNull
, just return empty from the lambda block. or make a wrapper if you want to,
inline fun <T, R> Iterable<T>.flatMapNotNull(transform: (T) -> Iterable<R>?): List<R> = flatMap { transform(it) ?: emptyList() }
phldavies
12/05/2021, 6:30 PMMichael Böiers
12/05/2021, 7:05 PMMichael Böiers
12/05/2021, 7:26 PMfun main() = generateSequence(::readLine).toList().day05().let { (p1, p2) -> println(p1); println(p2) }
fun List<String>.day05() = map { row -> row.replace(" -> ", ",").split(",").map { it.toInt() } }
.let { listOf(it.filter { (x1, y1, x2, y2) -> x1 == x2 || y1 == y2 }, it) }
.map { lines -> lines.points().groupBy { it }.count { it.value.size > 1 } }
private fun List<List<Int>>.points() = flatMap { (x1, y1, x2, y2) ->
sequence {
var pos = x1 to y1
while (true) {
yield(pos)
if (pos == x2 to y2) break
pos = pos.let { (x1, y1) -> x1 + x2.compareTo(x1) to y1 + y2.compareTo(y1) }
}
}
}
ephemient
12/05/2021, 7:31 PM(x1 - x0).sign
I used. but I'm not sure if compareTo
guarantees that it return -1, 0, 1? I thought it was only required to return <0, 0, >0Michael Böiers
12/05/2021, 7:37 PM.sign
for additional assurance 🙂Michael de Kaste
12/05/2021, 7:48 PMMichael Böiers
12/05/2021, 8:09 PMfun main() = generateSequence(::readLine).toList().day05().let { (p1, p2) -> println(p1); println(p2) }
fun List<String>.day05() = map { row -> row.replace(" -> ", ",").split(",").map { it.toInt() } }
.let { listOf(it.filter { (x1, y1, x2, y2) -> x1 == x2 || y1 == y2 }, it) }
.map { lines -> lines.points().groupBy { it }.count { it.value.size > 1 } }
private fun List<List<Int>>.points() = flatMap { (x1, y1, x2, y2) ->
generateSequence(x1 to y1) { (x, y) -> if (x to y == x2 to y2) null else x + x2.compareTo(x) to y + y2.compareTo(y) }
}
todd.ginsberg
12/05/2021, 11:22 PMsign
function. Cool!Dan Fingal-Surma
12/05/2021, 11:44 PMephemient
12/06/2021, 8:07 AM