-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay07InternetProtocolVersion7.kt
30 lines (24 loc) · 1.17 KB
/
Day07InternetProtocolVersion7.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package adventofcode.year2016
import adventofcode.Puzzle
import adventofcode.PuzzleInput
import adventofcode.common.everyNth
class Day07InternetProtocolVersion7(customInput: PuzzleInput? = null) : Puzzle(customInput) {
private val ipv7Addresses by lazy {
input.lines().map { ipAddress -> ipAddress.split("[", "]") }.map { parts -> parts.everyNth(2) to parts.everyNth(2, 1) }
}
override fun partOne() =
ipv7Addresses
.count { (supernets, hypernets) ->
supernets.any { supernet -> supernet.containsAbba() } && hypernets.none { hypernet -> hypernet.containsAbba() }
}
override fun partTwo() =
ipv7Addresses
.count { (supernets, hypernets) ->
val abas = supernets.flatMap { supernet -> supernet.getAbas() }
hypernets.any { hypernet -> abas.any { (a, b) -> hypernet.contains("$b$a$b") } }
}
companion object {
private fun String.containsAbba() = windowed(4).map(String::toList).any { (a, b, c, d) -> (a != b && "$a$b" == "$d$c") }
private fun String.getAbas() = windowed(3).map(String::toList).filter { (a, b, c) -> (a != b && a == c) }
}
}