Skip to content

Commit

Permalink
Day 25 2020
Browse files Browse the repository at this point in the history
  • Loading branch information
peckb1 committed Nov 10, 2023
1 parent 0a1b0f8 commit db626f7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/kotlin/me/peckb/aoc/_2020/calendar/day25/Day25.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.peckb.aoc._2020.calendar.day25

import javax.inject.Inject

import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory

class Day25 @Inject constructor(
private val generatorFactory: InputGeneratorFactory,
) {
fun partOne(filename: String) = generatorFactory.forFile(filename).read {
val (cardPublicKey, doorPublicKey) = it.toList().map { it.toLong() }
val cardLoopSize = findLoopSize(cardPublicKey)

findEncryptionKey(doorPublicKey, cardLoopSize)
}

private fun findLoopSize(publicKey: Long): Int {
var value = 1L
var loopSize = 0

while(value != publicKey) {
loopSize++
value = (value * 7L) % 2020_12_27L
}

return loopSize
}

private fun findEncryptionKey(publicKey: Long, loopSize: Int): Long {
var value = 1L
repeat(loopSize) { value = (value * publicKey) % 2020_12_27L }
return value
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## [Day 25: Combo Breaker](https://adventofcode.com/2020/day/25)
2 changes: 2 additions & 0 deletions src/test/kotlin/me/peckb/aoc/_2020/TestDayComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import me.peckb.aoc._2020.calendar.day21.Day21Test
import me.peckb.aoc._2020.calendar.day22.Day22Test
import me.peckb.aoc._2020.calendar.day23.Day23Test
import me.peckb.aoc._2020.calendar.day24.Day24Test
import me.peckb.aoc._2020.calendar.day25.Day25Test
import javax.inject.Singleton

import me.peckb.aoc.DayComponent
Expand Down Expand Up @@ -57,4 +58,5 @@ internal interface TestDayComponent : DayComponent {
fun inject(day22Test: Day22Test)
fun inject(day23Test: Day23Test)
fun inject(day24Test: Day24Test)
fun inject(day25Test: Day25Test)
}
28 changes: 28 additions & 0 deletions src/test/kotlin/me/peckb/aoc/_2020/calendar/day25/Day25Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package me.peckb.aoc._2020.calendar.day25

import javax.inject.Inject


import me.peckb.aoc._2020.DaggerTestDayComponent
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

internal class Day25Test {
@Inject
lateinit var day25: Day25

@BeforeEach
fun setup() {
DaggerTestDayComponent.create().inject(this)
}

@Test
fun testDay25PartOne() {
assertEquals(290487, day25.partOne(DAY_25))
}

companion object {
private const val DAY_25: String = "advent-of-code-input/2020/day25.input"
}
}

0 comments on commit db626f7

Please sign in to comment.