Advent of Code 2022 day 4
12/04/2024, 5:00 AMPaul Woitaschek
12/04/2024, 5:24 AMNeil Banman
12/04/2024, 5:28 AMMichael de Kaste
12/04/2024, 5:31 AMtoGrid()
, WINDS
etc.Jonathan Kolberg
12/04/2024, 5:31 AMMarcin Wisniowski
12/04/2024, 5:33 AMMarcin Wisniowski
12/04/2024, 5:33 AMMarcin Wisniowski
12/04/2024, 5:33 AMMichael de Kaste
12/04/2024, 6:05 AMJakub Gwóźdź
12/04/2024, 6:07 AMDan Fingal-Surma
12/04/2024, 6:20 AMDan Fingal-Surma
12/04/2024, 6:20 AMbj0
12/04/2024, 6:22 AMJakub Gwóźdź
12/04/2024, 6:59 AMX
-es on the grid, performs cartesian products with possible directions, and in this list goes step after step, filtering out the cases when the consecutive values aren't M
, A
, and S
Renette Ros
12/04/2024, 7:03 AMfun solveA(text: String, debug: Debug = Debug.Disabled): Int {
val lines = text.lines()
val rRange = 0..lines.lastIndex
val cRange = 0..lines[0].lastIndex
return rRange.sumOf { r ->
cRange.sumOf { c ->
val h = if (c + 3 in cRange) "${lines[r][c]}${lines[r][c + 1]}${lines[r][c + 2]}${lines[r][c + 3]}" else ""
val v = if (r + 3 in rRange) "${lines[r][c]}${lines[r + 1][c]}${lines[r + 2][c]}${lines[r + 3][c]}" else ""
val tlbr =
if (c + 3 in cRange && r + 3 in rRange) "${lines[r][c]}${lines[r + 1][c + 1]}${lines[r + 2][c + 2]}${lines[r + 3][c + 3]}" else ""
val bltr =
if (c + 3 in cRange && r + 3 in rRange) "${lines[r + 3][c]}${lines[r + 2][c + 1]}${lines[r + 1][c + 2]}${lines[r][c + 3]}" else ""
listOf(h, v, tlbr, bltr).count { it == "XMAS" || it == "SAMX" }
}
}
}
fun solveB(text: String, debug: Debug = Debug.Disabled): Int {
val lines = text.lines()
return (1..<lines.lastIndex).sumOf { r ->
(1..<lines[0].lastIndex).count { c ->
val tlbr = "${lines[r - 1][c - 1]}${lines[r][c]}${lines[r + 1][c + 1]}"
val bltr = "${lines[r - 1][c + 1]}${lines[r][c]}${lines[r + 1][c - 1]}"
(tlbr == "MAS" || tlbr == "SAM") && (bltr == "MAS" || bltr == "SAM")
}
}
}
Norbert Kiesel
12/04/2024, 7:32 AMbj0
12/04/2024, 7:35 AMdict
for grids from python, maybe i should just write an actual grid class...Michael de Kaste
12/04/2024, 7:56 AMMap<Pair<Int, Int>, T> -> Map<Point, T> -> Grid<T>
Neil Banman
12/04/2024, 8:28 AMindexOf('\n') + 1
. Then going up is -width, down is width, left is -1, right is 1. If you use getOrNull
then that handles anything above and below the area. The sides return \n, so it's easy to filter them out as well.Pavel Egorov
12/04/2024, 9:29 AMAnirudh
12/04/2024, 10:07 AMaLetters.size == 2 && aLetters.all { it in setOf("MAS", "SAM") }
but if I make it just
aLetters.all { it in setOf("MAS", "SAM") }
it still works - ie there are no X's with just one MAS/SAM 🤔Anirudh
12/04/2024, 10:11 AMisSubset
/ operator in Kotlin ?
I mean, it's a simple extension function with ⬇️ but still
Set<T>.isSubset(other: Set<T>) = all { it in other }
(edit: just subset, not proper subset)Jakub Gwóźdź
12/04/2024, 10:13 AMother.containsAll(this)
?Jakub Gwóźdź
12/04/2024, 10:14 AMall { it in other }
is cleanerephemient
12/04/2024, 10:19 AMephemient
12/04/2024, 10:22 AMAnirudh
12/04/2024, 10:22 AMsetOf("SAM", "SAM").containsAll(setOf("SAM", "MAS")) // false but we want true
setOf("SAM", "MAS", "XYZ").containsAll(setOf("SAM", "MAS")) // true but we want false
Jakub Gwóźdź
12/04/2024, 10:23 AMJakub Gwóźdź
12/04/2024, 10:25 AMSet<T>.isProperSubset(other: Set<T>) = other.containsAll(this)
Anirudh
12/04/2024, 10:25 AMMichael de Kaste
12/04/2024, 10:25 AMAnirudh
12/04/2024, 10:27 AMcontainsAll
confused meCharles Flynn
12/04/2024, 10:27 AMPetr Sýkora
12/04/2024, 11:34 AMphldavies
12/04/2024, 1:25 PMmaiatoday
12/04/2024, 2:12 PM// check diagonals
if ((i < this[0].length - 3) && (j < this.size - 3)) {
val diagonal1 = "${this[j][i]}${this[j + 1][i + 1]}${this[j + 2][i + 2]}${this[j + 3][i + 3]}"
val diagonal2 = "${this[j + 3][i]}${this[j + 2][i + 1]}${this[j + 1][i + 2]}${this[j][i + 3]}"
if (diagonal1.isXmas()) count++
if (diagonal2.isXmas()) count++
}
Max Thiele
12/04/2024, 4:41 PMDominik
12/08/2024, 8:18 PMSebastian Aigner
12/08/2024, 8:21 PM