Advent of Code 2023 day 3
12/03/2023, 5:00 AMKroppeb
12/03/2023, 5:39 AMaddyi
12/03/2023, 5:41 AMJakub Gwóźdź
12/03/2023, 5:52 AMJonathan Kolberg
12/03/2023, 6:07 AMMarcin Wisniowski
12/03/2023, 6:16 AMMarcin Wisniowski
12/03/2023, 6:17 AMJonathan Kolberg
12/03/2023, 6:18 AMNorbert Kiesel
12/03/2023, 6:28 AMNorbert Kiesel
12/03/2023, 6:29 AMJakub Gwóźdź
12/03/2023, 6:31 AMNorbert Kiesel
12/03/2023, 6:34 AMNeil Banman
12/03/2023, 6:48 AMephemient
12/03/2023, 6:49 AMJonathan Kolberg
12/03/2023, 6:53 AMJonathan Kolberg
12/03/2023, 6:54 AMapply
after mutableMapOf
is basically an implementation of buildMap
Neil Banman
12/03/2023, 6:55 AMJonathan Kolberg
12/03/2023, 6:57 AMflatMap
? As far as I know there were no garantees the same numbers could not be in the top and let's say the last lineDan Fingal-Surma
12/03/2023, 6:58 AMNeil Banman
12/03/2023, 7:00 AMdistinct
in both the inner and outer `flatmap`s, but at any rate, the outer one is necessary and cleans up the lack of the inner. The outer loop goes through each digit in the number, so there will be plenty of repeats, which the outer distinct
will catch.Norbert Kiesel
12/03/2023, 7:07 AM*
symbols for part 2?ephemient
12/03/2023, 7:17 AMbuildMap
, but I wanted to specify the types directly in order to not expose MutableList
beyond the initializer, and if I wasn't gonna get the benefit of builder inference then I didn't feel the need to botherephemient
12/03/2023, 7:19 AMPoisonedYouth
12/03/2023, 7:23 AMPeter Duong
12/03/2023, 7:24 AMJonathan Kolberg
12/03/2023, 7:25 AMAnirudh
12/03/2023, 7:50 AMList<List<Char>>
instead of List<MutableList<Char>>
Johannes Gätjen
12/03/2023, 8:12 AMDavio
12/03/2023, 8:31 AMAnirudh
12/03/2023, 8:36 AM*
or any symbol - all symbols have at least one neighbour
• no symbols share neighbours at all (gears or otherwise0
maybe he took it easy on us for Day3.
result: I wasted time re-writing the "find & replace numbers" for part 2, I could have just re-used my part 1 logic it with a *
check and result.size == 2
double 🤦Dan Fingal-Surma
12/03/2023, 8:56 AMAnirudh
12/03/2023, 8:57 AMDan Fingal-Surma
12/03/2023, 8:58 AMephemient
12/03/2023, 9:14 AMI believe otherwise @ephemient’s solution for part 1 would not have worked.yeah, I'd have to build the map in part 2 only if that were the case. but sometimes the inputs are nice :)
y9san9
12/03/2023, 9:43 AMNeil Banman
12/03/2023, 9:45 AMNikita Merinov
12/03/2023, 10:52 AMy9san9
12/03/2023, 11:03 AMy9san9
12/03/2023, 11:03 AMjonas.app
12/03/2023, 11:13 AMJaap Beetstra
12/03/2023, 11:29 AMJaap Beetstra
12/03/2023, 11:30 AMy9san9
12/03/2023, 11:30 AMNikita Merinov
12/03/2023, 11:31 AM.********.
.11111111.
You will do 8 times substring to build the same number.y9san9
12/03/2023, 11:32 AMy9san9
12/03/2023, 11:33 AMy9san9
12/03/2023, 11:36 AMJaap Beetstra
12/03/2023, 11:37 AMNikita Merinov
12/03/2023, 11:45 AMritesh
12/03/2023, 12:04 PMKroppeb
12/03/2023, 12:40 PMxxfast
12/03/2023, 12:40 PMPaul Woitaschek
12/03/2023, 1:03 PMJacek Rondio
12/03/2023, 1:08 PMMichael Böiers
12/03/2023, 1:21 PMTolly Kulczycki
12/03/2023, 2:02 PMCharles Flynn
12/03/2023, 2:04 PMPaul Woitaschek
12/03/2023, 2:04 PMCharles Flynn
12/03/2023, 2:05 PMCharles Flynn
12/03/2023, 2:05 PMCharles Flynn
12/03/2023, 2:05 PMCharles Flynn
12/03/2023, 2:06 PMPaul Woitaschek
12/03/2023, 2:06 PMCharles Flynn
12/03/2023, 2:08 PMCharles Flynn
12/03/2023, 2:30 PMCharles Flynn
12/03/2023, 2:32 PMMichael Böiers
12/03/2023, 2:34 PMphldavies
12/03/2023, 3:28 PM@AoKSolution
object Day03 : PuzDSL({
part1 {
lines.mapIndexed { y, line ->
"\\d+".toRegex().findAll(line).filter { m ->
val sr = max(m.range.first - 1, 0)..min(m.range.last + 1, line.lastIndex)
(max(y-1, 0)..min(y+1, lines.lastIndex))
.map { lines[it].substring(sr) }
.any { it.any { c -> c != '.' && !c.isDigit() } }
}.sumOf { it.value.toInt() }
}.sum()
}
part2 {
lines.mapIndexed { y, line ->
line.mapIndexedNotNull { idx, c -> idx.takeIf{ c == '*' } }.sumOf { x ->
(max(y - 1, 0)..min(y + 1, lines.lastIndex)).flatMap {
"\\d+".toRegex().findAll(lines[it]).filter { m ->
x in m.range || (x - 1) in m.range || (x + 1) in m.range
}.map { it.value.toInt() }
}.takeIf { it.size == 2 }?.let { (a, b) -> a * b } ?: 0
}
}.sum()
}
})
todd.ginsberg
12/03/2023, 6:31 PMraphadlts
12/03/2023, 10:00 PMstephen
12/03/2023, 10:29 PMjoney
12/03/2023, 11:44 PMMichael de Kaste
12/06/2023, 8:27 AMobject Day3 : Challenge() {
private var parts: Set<Part>
private var symbols: Set<Symbol>
init {
val graph = buildMap<Point, Segment> {
input.lines().forEachIndexed { y, line ->
line.forEachIndexed { x, char ->
when {
char.isDigit() -> put(
key = y to x,
value = get(y to x - 1).let { it as? Part ?: Part() }.apply {
positions += y to x
value = value * 10 + char.digitToInt()
},
)
char != '.' -> put(
key = y to x,
value = Symbol(
positions = listOf(y to x),
char = char,
),
)
}
}
}
}.apply {
values.forEach { segment ->
listOf(-1 to -1, -1 to 0, -1 to 1, 0 to -1, 0 to 1, 1 to -1, 1 to 0, 1 to 1).forEach { (y, x) ->
segment.positions.forEach { (baseY, baseX) ->
this[baseY + y to baseX + x]?.also {
segment.neighbours.add(it)
}
}
}
}
}
parts = graph.values.filterIsInstance<Part>().toSet()
symbols = graph.values.filterIsInstance<Symbol>().toSet()
}
sealed interface Segment {
val neighbours: MutableSet<Segment>
val positions: List<Pair<Int, Int>>
}
data class Part(
override val positions: MutableList<Pair<Int, Int>> = mutableListOf(),
var value: Int = 0,
) : Segment {
override val neighbours: MutableSet<Segment> = mutableSetOf()
}
data class Symbol(
override val positions: List<Pair<Int, Int>>,
val char: Char,
) : Segment {
override val neighbours: MutableSet<Segment> = mutableSetOf()
}
override fun part1() = parts
.filter { it.neighbours.any { it is Symbol } }
.sumOf { it.value }
override fun part2() = symbols
.filter { it.char == '*' }
.filter { it.neighbours.size == 2 && it.neighbours.all { it is Part } }
.sumOf { it.neighbours.filterIsInstance<Part>().let { (a, b) -> a.value * b.value } }
}