{--
--- Day 3: Perfectly Spherical Houses in a Vacuum ---

Santa is delivering presents to an infinite two-dimensional grid of houses.

He begins by delivering a present to the house at his starting location, and then an elf at the North Pole calls him via radio and tells him where to move next. Moves are always exactly one house to the north (^), south (v), east (>), or west (<). After each move, he delivers another present to the house at his new location.

However, the elf back at the north pole has had a little too much eggnog, and so his directions are a little off, and Santa ends up visiting some houses more than once. How many houses receive at least one present?

For example:

    > delivers presents to 2 houses: one at the starting location, and one to the east.
    ^>v< delivers presents to 4 houses in a square, including twice to the house at his starting/ending location.
    ^v^v^v^v^v delivers a bunch of presents to some very lucky children at only 2 houses.

--- Part Two ---

The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him.

Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year.

This year, how many houses receive at least one present?

For example:

    ^v delivers presents to 3 houses, because Santa goes north, and then Robo-Santa goes south.
    ^>v< now delivers presents to 3 houses, and Santa and Robo-Santa end up back where they started.
    ^v^v^v^v^v now delivers presents to 11 houses, with Santa going one direction and Robo-Santa going the other.
-}

module Day3
    ( answers
    ) where

import qualified Data.Set as Set

type Coordinate = (Int, Int)

charToCoord :: Char -> Coordinate -> Coordinate
charToCoord '^' (x, y) = (x, y + 1)
charToCoord 'v' (x, y) = (x, y - 1)
charToCoord '>' (x, y) = (x + 1, y)
charToCoord '<' (x, y) = (x - 1, y)
charToCoord _ coord    = coord

moveUntilFinish :: String -> Set.Set Coordinate -> Coordinate -> Set.Set Coordinate
moveUntilFinish [] location _         = location
moveUntilFinish (m:ms) location coord = let newCoord    = charToCoord m coord
                                            newLocation = Set.insert newCoord location
                                        in moveUntilFinish ms newLocation newCoord

moveUntilFinishWithRobot :: String -> Set.Set Coordinate -> Set.Set Coordinate -> Coordinate -> Coordinate -> Set.Set Coordinate
moveUntilFinishWithRobot [] santaLocation robotLocation _ _ = 
    Set.union santaLocation robotLocation
moveUntilFinishWithRobot (sm:[]) santaLocation robotLocation santaCoord _ = 
    let newSantaCoord = charToCoord sm santaCoord
        newSantaLocation = Set.insert newSantaCoord santaLocation
    in Set.union newSantaLocation robotLocation
moveUntilFinishWithRobot (sm:rm:ms) santaLocation robotLocation santaCoord robotCoord = 
    let newSantaCoord = charToCoord sm santaCoord
        newSantaLocation = Set.insert newSantaCoord santaLocation
        newRobotCoord = charToCoord rm robotCoord
        newRobotLocation = Set.insert newRobotCoord robotLocation
    in moveUntilFinishWithRobot ms newSantaLocation newRobotLocation newSantaCoord newRobotCoord

howManyHousesSantaVisited :: String -> Int
howManyHousesSantaVisited movements = let initialCoord = (0,0)
                                          initialHouse = Set.singleton initialCoord
                                          visits       = moveUntilFinish movements initialHouse initialCoord
                                      in Set.size visits

howManyHousesSantaAndRobotVisited :: String -> Int
howManyHousesSantaAndRobotVisited movements = let initialCoord = (0,0)
                                                  initialHouse = Set.singleton initialCoord
                                                  visits       = moveUntilFinishWithRobot movements initialHouse initialHouse initialCoord initialCoord
                                              in Set.size visits
answers :: String -> IO ()
answers movements = do
    putStrLn $ "day 3 part 1 = " ++ show (howManyHousesSantaVisited movements)
    putStrLn $ "day 3 part 2 = " ++ show (howManyHousesSantaAndRobotVisited movements)
