Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.19 KB | None | 0 0
  1. package day07
  2.  
  3. import java.io.File
  4.  
  5. /**
  6.  * author:  vishnu
  7.  * date:    5/23/17
  8.  * purpose:
  9.  */
  10.  
  11. val splitter = Regex("(?=\\b[\\[\\]])")
  12.  
  13. fun main(args: Array<String>) {
  14.     val ipList = File("src/day07/ips.txt").readLines()
  15.     println("TLS supported IPs: ${ipList.count { supportsTLS(it) }}")
  16.     println("SSL supported IPs: ${ipList.count { supportsSSL(it) }}")
  17. }
  18.  
  19. /* Part 1 */
  20.  
  21. fun supportsTLS(ip: String): Boolean {
  22.     var bracketedHasMirrored = false
  23.     var sectionHasMirrored = false
  24.     ip.split(splitter).forEach {
  25.         if (hasAbba(it)) {
  26.             if (it.startsWith('[')) bracketedHasMirrored = true
  27.             else sectionHasMirrored = true
  28.         }
  29.     }
  30.     return !bracketedHasMirrored and sectionHasMirrored
  31. }
  32.  
  33. fun hasAbba(section: String): Boolean {
  34.     return (3..section.length - 1)
  35.             .any {
  36.                 (section[it - 3] == section[it]) and (section[it - 2] == section[it - 1]) and
  37.                         (section[it - 3] != section[it - 2])
  38.             }
  39. }
  40.  
  41. /* Part 2 */
  42.  
  43. fun supportsSSL(ip: String): Boolean {
  44.     val sections = ip.split(splitter)
  45.     sections.filter { !it.startsWith('[') }
  46.             .forEach {
  47.                 val current = it
  48.                 (2..current.length - 1).forEach {
  49.                     if ((current[it - 2] == current[it]) and (current[it - 2] != current[it - 1])) {
  50.                         val bab = current[it - 1].toString() + current[it] + current[it - 1]
  51.                         sections.filter { it.startsWith('[') }
  52.                                 .forEach {
  53.                                     val currentBracketed = it
  54.                                     (2..currentBracketed.length - 1).forEach {
  55.                                         if ((currentBracketed[it - 2] == bab[0]) and
  56.                                                 (currentBracketed[it - 1] == bab[1]) and
  57.                                                 (currentBracketed[it] == bab[2])) {
  58.                                             return true
  59.                                         }
  60.                                     }
  61.                                 }
  62.                     }
  63.                 }
  64.             }
  65.     return false
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement