-
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
65 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/me/peckb/aoc/_2020/calendar/day25/Day25.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,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 | ||
} | ||
} |
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 25: Combo Breaker](https://adventofcode.com/2020/day/25) |
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
28 changes: 28 additions & 0 deletions
28
src/test/kotlin/me/peckb/aoc/_2020/calendar/day25/Day25Test.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,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" | ||
} | ||
} |