Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Advent of Frege code, day 3
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