Last active
April 28, 2017 09:46
-
-
Save lorcanmcdonald/5bfc21023ea5ecb1f9f7b0768321c479 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE OverloadedStrings, GeneralizedNewtypeDeriving, RankNTypes #-} | |
| {- | |
| By Lorcan and Joe @ melosity.com | |
| https://github.com/lorcanmcdonald/ | |
| https://github.com/JosephDevaney/ | |
| -} | |
| module Main where | |
| import Text.Parsec | |
| import Data.Functor.Identity | |
| import Debug.Trace | |
| data Instruction = L Int | |
| | R Int | |
| deriving Show | |
| data Direction = North | |
| | East | |
| | South | |
| | West | |
| deriving Show | |
| type X = Int | |
| type Y = Int | |
| data Vector = Vector Direction X Y | |
| deriving Show | |
| move :: Instruction -> Vector -> Vector | |
| move (L dist) (Vector North x y) = Vector West (x - dist) y | |
| move (R dist) (Vector North x y) = Vector East (x + dist) y | |
| move (L dist) (Vector South x y) = Vector East (x + dist) y | |
| move (R dist) (Vector South x y) = Vector West (x - dist) y | |
| move (L dist) (Vector East x y) = Vector North x (y + dist) | |
| move (R dist) (Vector East x y) = Vector South x (y - dist) | |
| move (L dist) (Vector West x y) = Vector South x (y - dist) | |
| move (R dist) (Vector West x y) = Vector North x (y + dist) | |
| newtype Input = Input [ Instruction ] | |
| distance :: Vector -> Int | |
| distance (Vector _ x y) = abs x + abs y | |
| blocks :: Input -> Int | |
| blocks (Input i) = distance . foldr move (Vector North 0 0) $ reverse i | |
| main :: IO () | |
| main = do | |
| print . blocks $ small | |
| print . blocks $ input | |
| small = Input [ R 5 | |
| , L 5 | |
| , R 5 | |
| , R 3 | |
| ] | |
| input = Input [ R 3 | |
| , L 5 | |
| , R 1 | |
| , R 2 | |
| , L 5 | |
| , R 2 | |
| , R 3 | |
| , L 2 | |
| , L 5 | |
| , R 5 | |
| , L 4 | |
| , L 3 | |
| , R 5 | |
| , L 1 | |
| , R 3 | |
| , R 4 | |
| , R 1 | |
| , L 3 | |
| , R 3 | |
| , L 2 | |
| , L 5 | |
| , L 2 | |
| , R 4 | |
| , R 5 | |
| , R 5 | |
| , L 4 | |
| , L 3 | |
| , L 3 | |
| , R 4 | |
| , R 4 | |
| , R 5 | |
| , L 5 | |
| , L 3 | |
| , R 2 | |
| , R 2 | |
| , L 3 | |
| , L 4 | |
| , L 5 | |
| , R 1 | |
| , R 3 | |
| , L 3 | |
| , R 2 | |
| , L 3 | |
| , R 5 | |
| , L 194 | |
| , L 2 | |
| , L 5 | |
| , R 2 | |
| , R 1 | |
| , R 1 | |
| , L 1 | |
| , L 5 | |
| , L 4 | |
| , R 4 | |
| , R 2 | |
| , R 2 | |
| , L 4 | |
| , L 1 | |
| , R 2 | |
| , R 53 | |
| , R 3 | |
| , L 5 | |
| , R 72 | |
| , R 2 | |
| , L 5 | |
| , R 3 | |
| , L 4 | |
| , R 187 | |
| , L 4 | |
| , L 5 | |
| , L 2 | |
| , R 1 | |
| , R 3 | |
| , R 5 | |
| , L 4 | |
| , L 4 | |
| , R 2 | |
| , R 5 | |
| , L 5 | |
| , L 4 | |
| , L 3 | |
| , R 5 | |
| , L 2 | |
| , R 1 | |
| , R 1 | |
| , R 4 | |
| , L 1 | |
| , R 2 | |
| , L 3 | |
| , R 5 | |
| , L 4 | |
| , R 2 | |
| , L 3 | |
| , R 1 | |
| , L 4 | |
| , R 4 | |
| , L 1 | |
| , L 2 | |
| , R 3 | |
| , L 1 | |
| , L 1 | |
| , R 4 | |
| , R 3 | |
| , L 4 | |
| , R 2 | |
| , R 5 | |
| , L 2 | |
| , L 3 | |
| , L 3 | |
| , L 1 | |
| , R 3 | |
| , R 5 | |
| , R 2 | |
| , R 3 | |
| , R 1 | |
| , R 2 | |
| , L 1 | |
| , L 4 | |
| , L 5 | |
| , L 2 | |
| , R 4 | |
| , R 5 | |
| , L 2 | |
| , R 4 | |
| , R 4 | |
| , L 3 | |
| , R 2 | |
| , R 1 | |
| , L 4 | |
| , R 3 | |
| , L 3 | |
| , L 4 | |
| , L 3 | |
| , L 1 | |
| , R 3 | |
| , L 2 | |
| , R 2 | |
| , L 4 | |
| , L 4 | |
| , L 5 | |
| , R 3 | |
| , R 5 | |
| , R 3 | |
| , L 2 | |
| , R 5 | |
| , L 2 | |
| , L 1 | |
| , L 5 | |
| , L 1 | |
| , R 2 | |
| , R 4 | |
| , L 5 | |
| , R 2 | |
| , L 4 | |
| , L 5 | |
| , L 4 | |
| , L 5 | |
| , L 2 | |
| , L 5 | |
| , L 4 | |
| , R 5 | |
| , R 3 | |
| , R 2 | |
| , R 2 | |
| , L 3 | |
| , R 3 | |
| , L 2 | |
| , L 5 | |
| ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment