Skip to content

Instantly share code, notes, and snippets.

@Dierk
Created December 4, 2017 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dierk/48f28478cd7946bc6181152aea7dadd4 to your computer and use it in GitHub Desktop.
Save Dierk/48f28478cd7946bc6181152aea7dadd4 to your computer and use it in GitHub Desktop.
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