Advent of Code 2023 day 13
12/13/2023, 5:00 AMJonathan Kolberg
12/13/2023, 5:31 AMbj0
12/13/2023, 5:47 AMbj0
12/13/2023, 5:47 AMJonathan Kolberg
12/13/2023, 5:53 AMbj0
12/13/2023, 5:56 AMJonathan Kolberg
12/13/2023, 5:58 AMbj0
12/13/2023, 5:59 AMMarcin Wisniowski
12/13/2023, 5:59 AMJonathan Kolberg
12/13/2023, 6:00 AMJonathan Kolberg
12/13/2023, 6:01 AMbj0
12/13/2023, 6:03 AMWerner Altewischer
12/13/2023, 6:03 AMManoj
12/13/2023, 6:04 AMbj0
12/13/2023, 6:04 AMbj0
12/13/2023, 6:04 AMbj0
12/13/2023, 6:04 AMManoj
12/13/2023, 6:04 AMWerner Altewischer
12/13/2023, 6:07 AMWerner Altewischer
12/13/2023, 6:08 AMbj0
12/13/2023, 6:08 AMephemient
12/13/2023, 6:21 AMEl Anthony
12/13/2023, 6:26 AMMichael de Kaste
12/13/2023, 6:41 AMJonathan Kolberg
12/13/2023, 6:56 AMJakub Gwóźdź
12/13/2023, 7:02 AMJohnny Wulgaru
12/13/2023, 7:24 AMandriyo
12/13/2023, 7:35 AMphldavies
12/13/2023, 7:54 AMandriyo
12/13/2023, 8:03 AMMichael de Kaste
12/13/2023, 8:04 AMJaap Beetstra
12/13/2023, 9:15 AMDave
12/13/2023, 9:33 AMJaap Beetstra
12/13/2023, 9:35 AMJohnny Wulgaru
12/13/2023, 9:36 AMphldavies
12/13/2023, 9:36 AMphldavies
12/13/2023, 9:37 AMMichael de Kaste
12/13/2023, 9:37 AMEl Anthony
12/13/2023, 9:42 AMphldavies
12/13/2023, 9:51 AMnvbahool
12/13/2023, 9:56 AMJaap Beetstra
12/13/2023, 9:57 AMJohnny Wulgaru
12/13/2023, 9:57 AMDave
12/13/2023, 10:02 AM....#....##....
.##..##.####.##
#..#.#...##...#
.##.####....###
#..#.##.####.##
#..###.#.##.#.#
.##.##.##..####
El Anthony
12/13/2023, 10:04 AM....##....
##.####.##
....##...#
###....###
##.####.##
#.#.##.#.#
#.##..####
10-11 is not a reflection, isn't it?Dave
12/13/2023, 10:06 AM.##.
####
.##.
....
####
.##.
#..#
Jakub Gwóźdź
12/13/2023, 10:08 AMEl Anthony
12/13/2023, 10:08 AMDave
12/13/2023, 10:11 AMDave
12/13/2023, 10:12 AMMichael de Kaste
12/13/2023, 10:13 AMMichael de Kaste
12/13/2023, 10:14 AMDave
12/13/2023, 10:14 AMMichael de Kaste
12/13/2023, 10:17 AMMichael de Kaste
12/13/2023, 10:18 AM....#....##....
.##..##.####.##
#..#.#...##...#
.##.####....###
#..#.##.####.##
#..###.#.##.#.#
.##.##.##..####
is equal to
....#....##....????????
.##..##.####.##????????
#..#.#...##...#????????
.##.####....###????????
#..#.##.####.##????????
#..###.#.##.#.#????????
.##.##.##..####????????
you can pretend the grid extends both ways with a potential reflectionMax Thiele
12/13/2023, 10:19 AMval patterns = input.fold(mutableListOf(mutableListOf<String>())) { groups, line ->
groups.apply {
when {
line.isBlank() -> add(mutableListOf())
else -> last().add(line)
}
}
}.map { Pattern(it) }
Michael de Kaste
12/13/2023, 10:19 AM....#....#|#....????????
.##..##.##|##.##????????
#..#.#...#|#...#????????
.##.####..|..###????????
#..#.##.##|##.##????????
#..###.#.#|#.#.#????????
.##.##.##.|.####????????
cannot occur because the 4th value on the bottom row differs if you walk outwards from the reflection centreMichael de Kaste
12/13/2023, 10:19 AMDave
12/13/2023, 10:20 AMMichael de Kaste
12/13/2023, 10:21 AMDave
12/13/2023, 10:23 AM...##....
.#.##.#..
.#....#..
.##..##..
...##....
..####.##
#..##..##
using the obvious vertical reflect line does it not hit the edge ?Michael de Kaste
12/13/2023, 10:24 AMMichael de Kaste
12/13/2023, 10:25 AMMichael de Kaste
12/13/2023, 10:25 AMephemient
12/13/2023, 10:25 AMMichael de Kaste
12/13/2023, 10:25 AM...##...|.
.#.##.#.|.
.#....#.|.
.##..##.|.
...##...|.
..####.#|#
#..##..#|#
equals
...##...|.???????
.#.##.#.|.???????
.#....#.|.???????
.##..##.|.???????
...##...|.???????
..####.#|#???????
#..##..#|#???????
Dave
12/13/2023, 10:28 AMMichael de Kaste
12/13/2023, 10:29 AMMax Thiele
12/13/2023, 10:33 AMEl Anthony
12/13/2023, 10:41 AMfun readPatterns2(lines: Sequence<String>) = sequence {
val lastOne = lines.fold( listOf<String>()) { acc, line ->
if (line.isEmpty()) {
yield( Pattern(acc))
listOf<String>()
}
else acc + line
}
yield (Pattern(lastOne))
}
phldavies
12/13/2023, 10:43 AMfun <T> List<T>.cutAt(idx: Int) = subList(0, idx) to subList(idx + 1, size)
fun <T> List<T>.cutAt(p: (T) -> Boolean) = indexOfFirst(p).let { if (it >= 0) cutAt(it) else this to null }
fun <T> List<T>.splitOn(p: (T) -> Boolean) = Iterable {
iterator {
var remaining = this@splitOn
while (remaining.isNotEmpty()) {
val (a, b) = remaining.cutAt(p)
yield(a)
remaining = b ?: break
}
}
}
I tend to just do input.split("\n\n").map(String::lines)
(assumes input
doesn't have any trailing new lines)ephemient
12/13/2023, 10:47 AMEl Anthony
12/13/2023, 10:52 AMinput.split("\n\n").map(String::lines)
is probably the shortest and highest performing solution.Max Thiele
12/13/2023, 10:53 AMval patterns = buildList {
input.plus("").runningFold(mutableListOf<String>()) { group, line ->
when {
line.isBlank() -> mutableListOf<String>().also { add(Pattern(group)) }
else -> group.also { it.add(line) }
}
}
}
phldavies
12/13/2023, 10:55 AMrunningFold
, a fold
would do?Max Thiele
12/13/2023, 10:57 AMkingsley
12/13/2023, 11:07 AMMax Thiele
12/13/2023, 11:15 AMrunningFold
^^
val patterns = input
.runningFold(mutableListOf<String>()) { group, line ->
if (line.isBlank()) mutableListOf() else group.apply { add(line) }
}
.distinct()
.map { Pattern(it) }
Michael Böiers
12/13/2023, 11:23 AMJonathan Kolberg
12/13/2023, 11:31 AMMax Thiele
12/13/2023, 11:32 AMCharles Flynn
12/13/2023, 1:16 PMprivate val input = readFile("13.txt").split("\n\n").map { s ->
val matrix = s.lines()
val transposed = matrix.transposed()
matrix to transposed
}
Max Thiele
12/13/2023, 1:25 PMAndrei Kovalevsky
12/13/2023, 1:25 PMSystem.lineSeparator()
Michael de Kaste
12/13/2023, 1:27 PMSystem.lineSeparator()
myself too, but I wrote on extension as String.splitOnEmptyLine()
Michael de Kaste
12/13/2023, 1:28 PM.lines
or splitOnEmptyLine
isn't a hassle 😄Charles Flynn
12/13/2023, 1:36 PMreadFileAsLines
is the most common usage but readFile
when I want to go manual.Marcin Wisniowski
12/13/2023, 1:36 PMAndrei Kovalevsky
12/13/2023, 1:36 PMMax Thiele
12/13/2023, 1:43 PMJaap Beetstra
12/13/2023, 1:47 PMfun split(input: List<String>): List<List<String>> =
(listOf(-1) + input.indices.filter { input[it].isBlank() } + input.size).zipWithNext { prev, cur ->
input.subList(prev + 1, cur)
}
Max Thiele
12/13/2023, 2:32 PMNorbert Kiesel
12/13/2023, 3:59 PMCollection.chunkedBy
helper which allows to use input.chunkedBy { it.isEmpty() }
. Not the most efficient, but easy on the eye.Jaap Beetstra
12/13/2023, 4:23 PMCollection.chunkedBy { it.country }
will turn it into a list of lists with the same country.
For this situation I have a helper named ‘split’ (similar to String.split) fun <T> List<T>.split(predicate: (T) -> Boolean)
. So e.g. input.split { it.isEmpty() }
Norbert Kiesel
12/13/2023, 4:25 PMNorbert Kiesel
12/13/2023, 6:56 PMPaul Woitaschek
12/13/2023, 8:12 PMEl Anthony
12/13/2023, 8:37 PMPaul Woitaschek
12/13/2023, 8:38 PMphldavies
12/13/2023, 8:42 PMphldavies
12/13/2023, 8:44 PM== 1
Tomasz Linkowski
12/13/2023, 8:45 PMphldavies
12/13/2023, 8:47 PMGrzegorz Aniol
12/13/2023, 9:04 PM