Skip to content

Commit

Permalink
day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
norganos committed Dec 3, 2023
1 parent 6a2e5b1 commit 0e15245
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
implementation("io.micronaut.serde:micronaut-serde-jackson")
implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
implementation("de.linkel.aoc:aoc-utils:1.0.0")
implementation("de.linkel.aoc:aoc-utils:1.0.1")
runtimeOnly("ch.qos.logback:logback-classic")
runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
testImplementation("org.assertj:assertj-core:3.24.2")
Expand Down
41 changes: 40 additions & 1 deletion src/main/kotlin/de/linkel/aoc/Day03.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,52 @@ package de.linkel.aoc

import de.linkel.aoc.base.AbstractLinesAdventDay
import de.linkel.aoc.base.QuizPart
import de.linkel.aoc.utils.append
import de.linkel.aoc.utils.extend
import de.linkel.aoc.utils.intersects
import de.linkel.aoc.utils.prepend
import jakarta.inject.Singleton

@Singleton
class Day03: AbstractLinesAdventDay<Int>() {
override val day = 3

override fun process(part: QuizPart, lines: Sequence<String>): Int {
return 0
val digitPattern = Regex("[0-9]+")
val symbolPattern = Regex("[^0-9.]")
val asteriskPattern = Regex("\\*")
val result = lines
.prepend("")
.append("")
.windowed(3)
.map { win ->
val allNumbersPerLine = win.map { digitPattern.findAll(it) }.map { it.toList() }
val allSymbols = win.flatMap { symbolPattern.findAll(it) }.toList()
val partNumbers = allNumbersPerLine[1]
.filter { match ->
val checkrange = match.range.extend(front = 1, back = 1)
allSymbols
.any { it.range.intersects(checkrange) }
}
.sumOf { it.value.toInt() }
val allNumbers = allNumbersPerLine.flatten()
val gears = asteriskPattern.findAll(win[1])
.map { match ->
val checkrange = match.range.extend(front = 1, back = 1)
allNumbers
.filter { it.range.intersects(checkrange) }
.map { it.value.toInt() }
}
.filter { it.size == 2 }
.map { it[0] * it[1] }
.sum()
Pair(partNumbers, gears)
}
.fold(Pair(0,0)) {
sum, line ->
Pair(sum.first + line.first, sum.second + line.second)
}
return if (part == QuizPart.A) result.first
else result.second
}
}
18 changes: 14 additions & 4 deletions src/test/kotlin/de/linkel/aoc/Day03Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ package de.linkel.aoc

class Day03Test: AbstractDayTest<Int>() {
override val exampleA = """
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...${'$'}.*....
.664.598..
""".trimIndent()
override val exampleSolutionA = 0
override val solutionA = 0
override val exampleSolutionA = 4361
override val solutionA = 559667

override val exampleSolutionB = 0
override val solutionB = 0
override val exampleSolutionB = 467835
override val solutionB = 86841457

override val implementation = Day03()
}

0 comments on commit 0e15245

Please sign in to comment.