Skip to content

Instantly share code, notes, and snippets.

@pwm
Last active December 4, 2017 13:42
Show Gist options
  • Save pwm/29054440fafb877eace58f1312b60b20 to your computer and use it in GitHub Desktop.
Save pwm/29054440fafb877eace58f1312b60b20 to your computer and use it in GitHub Desktop.
AOC 2017
-- read string into list
input = map (\x -> read [x]::Int)
-----------
-- Day 1 --
-----------
-- part 1
f xs = sum $ map fst $ filter (\(x,y) -> x == y) $ zip xs $ tail $ (\xs -> xs ++ [head xs]) xs
-- part 2
f xs = sum $ map fst $ filter (\(x,y) -> x == y) $ zip xs $ drop (length xs `div` 2) xs ++ take (length xs `div` 2) xs
-----------
-- Day 2 --
-----------
-- part 1
f xxs = sum $ map (\xs -> maximum xs - minimum xs) xxs
-- part 2
f1 xs = zip (take (length xs - 1) $ repeat $ head xs) $ tail xs
f2 (x,y) = if x > y then (x,y) else (y,x)
f3 = concat . map (map f2 . f1) . init . init . tails
f4 = head . map (\(x,y) -> x `div` y) . filter (\(x,y) -> x `mod` y == 0) . f3
f5 = sum . map f4
-----------
-- Day 3 --
-----------
-- part 1
f x (a,b)
| x == 1 = (a, b + 1) -- right
| x == 2 = (a + 1, b) -- up
| x == 3 = (a, b - 1) -- left
| x == 4 = (a - 1, b) -- down
g x = (\(x,y) -> abs x + abs y) $ foldr f (0,0) $ take (x - 1) $ concatMap (\(x,y) -> take y $ repeat x) $ zip (cycle [1..4]) $ concatMap (\x -> [x,x]) [1..]
-- part 2
TODO
-----------
-- Day 4 --
-----------
-- part 1 & part 2
p1 = length . nub . words
p2 = length . nub . map sort . words
solve p xxs = length $ filter (== True) $ map (\xs -> p xs == (length . words) xs) xxs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment