Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object day7 extends App {
- def hypernetParts(xs: String): List[List[Char]] = {
- val res = xs.split("\\[").toList filter
- (_.contains("]")) map (_.split("\\]") take 1)
- res.flatten map (_.toList)
- }
- def addrParts(xs: String): List[List[Char]] = {
- val res = xs.split("\\[").toList map (_.split("\\]") takeRight 1)
- res.flatten map (_.toList)
- }
- def hasTls(xs: List[Char]): Boolean = xs match {
- case a :: b :: c :: d :: t =>
- if (a == d && b == c && a != b) true else hasTls(b :: c :: d :: t)
- case _ => false
- }
- def listAbas(xs: List[Char]) = {
- for {
- i <- 0 to xs.length - 3
- t = xs.slice(i, i + 3)
- if (t(0) == t(2) && t(1) != t(0))
- } yield t
- }
- def getBab(xs: List[Char]): String = xs match {
- case a :: b :: c :: d if (a == c && b != c) => List(b, a, b).mkString
- case _ => throw new NoSuchElementException
- }
- def isSsl (as: List[List[Char]], hs: List[List[Char]]) = {
- val abas = (as map listAbas).flatten
- abas.exists(aba => hs.exists(h => h.mkString.contains(getBab(aba))))
- }
- val ips = Source.fromFile("day7.txt").getLines.toList
- val ipsWithTls = ips filter (ip => addrParts(ip).exists(hasTls) && !hypernetParts(ip).exists(hasTls))
- println(ipsWithTls.length)
- // Part 2
- val ipsWithSsl = ips filter (ip => isSsl(addrParts(ip), hypernetParts(ip)))
- println(ipsWithSsl.length)
- }
Advertisement
Add Comment
Please, Sign In to add comment