Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package day07
- import java.io.File
- /**
- * author: vishnu
- * date: 5/23/17
- * purpose:
- */
- val splitter = Regex("(?=\\b[\\[\\]])")
- fun main(args: Array<String>) {
- val ipList = File("src/day07/ips.txt").readLines()
- println("TLS supported IPs: ${ipList.count { supportsTLS(it) }}")
- println("SSL supported IPs: ${ipList.count { supportsSSL(it) }}")
- }
- /* Part 1 */
- fun supportsTLS(ip: String): Boolean {
- var bracketedHasMirrored = false
- var sectionHasMirrored = false
- ip.split(splitter).forEach {
- if (hasAbba(it)) {
- if (it.startsWith('[')) bracketedHasMirrored = true
- else sectionHasMirrored = true
- }
- }
- return !bracketedHasMirrored and sectionHasMirrored
- }
- fun hasAbba(section: String): Boolean {
- return (3..section.length - 1)
- .any {
- (section[it - 3] == section[it]) and (section[it - 2] == section[it - 1]) and
- (section[it - 3] != section[it - 2])
- }
- }
- /* Part 2 */
- fun supportsSSL(ip: String): Boolean {
- val sections = ip.split(splitter)
- sections.filter { !it.startsWith('[') }
- .forEach {
- val current = it
- (2..current.length - 1).forEach {
- if ((current[it - 2] == current[it]) and (current[it - 2] != current[it - 1])) {
- val bab = current[it - 1].toString() + current[it] + current[it - 1]
- sections.filter { it.startsWith('[') }
- .forEach {
- val currentBracketed = it
- (2..currentBracketed.length - 1).forEach {
- if ((currentBracketed[it - 2] == bab[0]) and
- (currentBracketed[it - 1] == bab[1]) and
- (currentBracketed[it] == bab[2])) {
- return true
- }
- }
- }
- }
- }
- }
- return false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement