package aoc2015

import println
import readInput

/*
--- Day 5: Doesn't He Have Intern-Elves For This? ---
https://adventofcode.com/2015/day/5
*/
fun main() {

    val inlineTestInput = """
ugknbfddgicrmopn
aaa
jchzalrnumimnmhp
haegwjzuvuyypxyu
dvszwmarrgswjxmb
"""

    val inlineTestInput2 = """
qjhvhtzxzqqjkmpb
xxyxx
uurcxstgmygtbstg
ieodomkazucvgmuy
"""

    fun part1(input: List<String>): Int {
        var nice = 0
        for (i in input) {
            val vowels = i.filter { "aeiou".contains(it) }.take(3).count() == 3
            val double = i.zipWithNext { a, b -> a == b }.any { it }
            val sane = listOf("ab", "cd", "pq", "xy").none { i.contains(it) }
            if (vowels && double && sane) nice++
        }
        return nice
    }

    fun part2(input: List<String>): Int {
        var nice = 0
        for (i in input) {
            val double = i.zipWithNext { a, b -> "$a$b" }.any { i.replace(it, "").length < i.length - 3 }
            val spaced = i.zipWithNext().zipWithNext().any { it.first.first == it.second.second }
            if (double && spaced) nice++
        }
        return nice
    }

    // test if implementation meets criteria from the description, like:
    val testInput = inlineTestInput.trim().reader().readLines()
    val testInput2 = inlineTestInput2.trim().reader().readLines()
    //val testInput = readInput("aoc2015/Day05_test")
    val testInputPart1Result = part1(testInput)
    println("Part 1 Test: $testInputPart1Result")
    val testInputPart2Result = part2(testInput2)
    println("Part 2 Test: $testInputPart2Result")
    check(testInputPart1Result == 2)
    check(testInputPart2Result == 2)

    val input = readInput("aoc2015/Day05")
    part1(input).println()
    part2(input).println()
}
