Created
December 4, 2017 11:17
-
-
Save Dierk/48f28478cd7946bc6181152aea7dadd4 to your computer and use it in GitHub Desktop.
Advent of Frege code, day 3
This file contains 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
module Advent3 where | |
{- | |
http://adventofcode.com/2017/day/3 | |
-} | |
import Data.List | |
data Direction = Right | Up | Down | Left | |
derive Show Direction | |
derive Eq Direction | |
count x xs = length $ filter (== x) xs | |
input = 1024 -- your number here | |
spiral = | |
[ n | | |
x <- [2, 4..], | |
n <- Right : | |
replicate (x-1) Up ++ | |
replicate x Left ++ | |
replicate x Down ++ | |
replicate x Right | |
] | |
main = do | |
seq = take (input-1) spiral | |
lr = abs $ (count Right seq) - (count Left seq) | |
ud = abs $ (count Up seq) - (count Down seq) | |
println (lr + ud) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment