package aoc2015

import RelPos
import println
import readInput

/*
--- Day 3: Perfectly Spherical Houses in a Vacuum ---
https://adventofcode.com/2015/day/3
*/
fun main() {

    val inlineTestInput = """
^>v<
"""

    fun part1(input: List<String>): Int {
        val houses = HashSet<RelPos>()
        var x = 0
        var y = 0
        for (c in input.single()) {
            when (c) {
                '<' -> x--
                '>' -> x++
                '^' -> y--
                'v' -> y++
            }
            houses.add(RelPos(x, y))
        }
        return houses.size
    }

    fun part2(input: List<String>): Int {
        val houses = HashSet<RelPos>()
        var xs = 0
        var ys = 0
        var xr = 0
        var yr = 0
        var parity = false
        for (c in input.single()) {
            if (parity) {
                when (c) {
                    '<' -> xr--
                    '>' -> xr++
                    '^' -> yr--
                    'v' -> yr++
                }
                houses.add(RelPos(xr, yr))
            } else {
                when (c) {
                    '<' -> xs--
                    '>' -> xs++
                    '^' -> ys--
                    'v' -> ys++
                }
                houses.add(RelPos(xs, ys))
            }
            parity = !parity
        }
        return houses.size
    }

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

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