lluque

AoC day 7

Dec 7th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.41 KB | None | 0 0
  1. object day7 extends App {
  2.  
  3.   def hypernetParts(xs: String): List[List[Char]] = {
  4.     val res = xs.split("\\[").toList filter
  5.       (_.contains("]")) map (_.split("\\]") take 1)
  6.     res.flatten map (_.toList)
  7.   }
  8.  
  9.   def addrParts(xs: String): List[List[Char]] = {
  10.     val res = xs.split("\\[").toList map (_.split("\\]") takeRight 1)
  11.     res.flatten map (_.toList)
  12.   }
  13.  
  14.   def hasTls(xs: List[Char]): Boolean = xs match {
  15.     case a :: b :: c :: d :: t =>
  16.       if (a == d && b == c && a != b) true else hasTls(b :: c :: d :: t)
  17.     case _ => false
  18.   }
  19.  
  20.   def listAbas(xs: List[Char]) = {
  21.     for {
  22.         i <- 0 to xs.length - 3
  23.         t = xs.slice(i, i + 3)
  24.         if (t(0) == t(2) && t(1) != t(0))
  25.       }  yield t
  26.   }
  27.  
  28.   def getBab(xs: List[Char]): String = xs match {
  29.       case a :: b :: c :: d if (a == c && b != c) => List(b, a, b).mkString
  30.       case _ => throw new NoSuchElementException
  31.   }
  32.  
  33.   def isSsl (as: List[List[Char]], hs: List[List[Char]]) = {
  34.     val abas = (as map listAbas).flatten
  35.     abas.exists(aba => hs.exists(h => h.mkString.contains(getBab(aba))))
  36.   }
  37.  
  38.   val ips = Source.fromFile("day7.txt").getLines.toList
  39.   val ipsWithTls = ips filter (ip => addrParts(ip).exists(hasTls) && !hypernetParts(ip).exists(hasTls))
  40.   println(ipsWithTls.length)
  41.   // Part 2
  42.   val ipsWithSsl = ips filter (ip => isSsl(addrParts(ip), hypernetParts(ip)))
  43.   println(ipsWithSsl.length)
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment