-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/main/kotlin/me/peckb/aoc/_2023/calendar/day01/Day01.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package me.peckb.aoc._2023.calendar.day01 | ||
|
||
import javax.inject.Inject | ||
|
||
import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory | ||
|
||
class Day01 @Inject constructor( | ||
private val generatorFactory: InputGeneratorFactory, | ||
) { | ||
fun partOne(filename: String) = generatorFactory.forFile(filename).read { input -> | ||
input.map { lineFromFile -> | ||
val digits = lineFromFile.mapNotNull { character -> | ||
if (character.isDigit()) { | ||
character.digitToInt() | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
val first = digits.first() | ||
val last = digits.last() | ||
|
||
"$first$last".toInt() | ||
}.sum() | ||
} | ||
|
||
fun partTwo(filename: String) = generatorFactory.forFile(filename).read { input -> | ||
input.map { lineFromFile -> | ||
val first = listOf( | ||
lineFromFile.indexOf("one") to 1, | ||
lineFromFile.indexOf("two") to 2, | ||
lineFromFile.indexOf("three") to 3, | ||
lineFromFile.indexOf("four") to 4, | ||
lineFromFile.indexOf("five") to 5, | ||
lineFromFile.indexOf("six") to 6, | ||
lineFromFile.indexOf("seven") to 7, | ||
lineFromFile.indexOf("eight") to 8, | ||
lineFromFile.indexOf("nine") to 9, | ||
|
||
lineFromFile.indexOf('1') to 1, | ||
lineFromFile.indexOf('2') to 2, | ||
lineFromFile.indexOf('3') to 3, | ||
lineFromFile.indexOf('4') to 4, | ||
lineFromFile.indexOf('5') to 5, | ||
lineFromFile.indexOf('6') to 6, | ||
lineFromFile.indexOf('7') to 7, | ||
lineFromFile.indexOf('8') to 8, | ||
lineFromFile.indexOf('9') to 9, | ||
).sortedBy { (index, _) -> index } | ||
.first { (index, _) -> index >= 0 } | ||
.second | ||
|
||
val last = listOf( | ||
lineFromFile.lastIndexOf("one") to 1, | ||
lineFromFile.lastIndexOf("two") to 2, | ||
lineFromFile.lastIndexOf("three") to 3, | ||
lineFromFile.lastIndexOf("four") to 4, | ||
lineFromFile.lastIndexOf("five") to 5, | ||
lineFromFile.lastIndexOf("six") to 6, | ||
lineFromFile.lastIndexOf("seven") to 7, | ||
lineFromFile.lastIndexOf("eight") to 8, | ||
lineFromFile.lastIndexOf("nine") to 9, | ||
|
||
lineFromFile.lastIndexOf('1') to 1, | ||
lineFromFile.lastIndexOf('2') to 2, | ||
lineFromFile.lastIndexOf('3') to 3, | ||
lineFromFile.lastIndexOf('4') to 4, | ||
lineFromFile.lastIndexOf('5') to 5, | ||
lineFromFile.lastIndexOf('6') to 6, | ||
lineFromFile.lastIndexOf('7') to 7, | ||
lineFromFile.lastIndexOf('8') to 8, | ||
lineFromFile.lastIndexOf('9') to 9, | ||
).sortedBy { (index, _) -> index } | ||
.last { (index, _) -> index >= 0 } | ||
.second | ||
|
||
"$first$last".toInt() | ||
}.sum() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
## [Day 1: Trebuchet?!](https://adventofcode.com/2023/day/1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package me.peckb.aoc._2023 | ||
|
||
import dagger.Component | ||
import javax.inject.Singleton | ||
|
||
import me.peckb.aoc.DayComponent | ||
import me.peckb.aoc.InputModule | ||
import me.peckb.aoc._2023.calendar.day01.Day01Test | ||
|
||
@Singleton | ||
@Component(modules = [InputModule::class]) | ||
internal interface TestDayComponent : DayComponent { | ||
fun inject(day01Test: Day01Test) | ||
} |
33 changes: 33 additions & 0 deletions
33
src/test/kotlin/me/peckb/aoc/_2023/calendar/day01/Day01Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package me.peckb.aoc._2023.calendar.day01 | ||
|
||
import javax.inject.Inject | ||
|
||
|
||
import me.peckb.aoc._2023.DaggerTestDayComponent | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class Day01Test { | ||
@Inject | ||
lateinit var day01: Day01 | ||
|
||
@BeforeEach | ||
fun setup() { | ||
DaggerTestDayComponent.create().inject(this) | ||
} | ||
|
||
@Test | ||
fun testDay01PartOne() { | ||
assertEquals(54667, day01.partOne(DAY_01)) | ||
} | ||
|
||
@Test | ||
fun testDay01PartTwo() { | ||
assertEquals(54203, day01.partTwo(DAY_01)) | ||
} | ||
|
||
companion object { | ||
private const val DAY_01: String = "advent-of-code-input/2023/day01.input" | ||
} | ||
} |