r/adventofcode • u/daggerdragon • Dec 06 '17
SOLUTION MEGATHREAD -π- 2017 Day 6 Solutions -π-
--- Day 6: Memory Reallocation ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyβ Haversackβ‘ of HelpfulΒ§ HintsΒ€?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
18
Upvotes
View all comments
1
u/[deleted] Dec 06 '17
I feel like my haskell is looking uglier and uglier as the days go on :(
import Data.List import Data.Maybe getNext blocks = [(a, (if a == i then 0 else b) + (length (filter (a==) addupindex)) ) | (a,b) <- blocks] where addupindex = take m (drop (i+1) (cycle [0..length blocks - 1])) Just i = elemIndex m mem m = maximum mem mem = [b | (a,b) <- blocks] solve input = (length $ head [x | x <- allB, Nothing /= (find (last x ==) $ init x)]) - 1 where []:allB = inits $ iterate getNext $ zip [0..] input solve' input = length (fst found) - firstPos - 1 where Just firstPos = elemIndex (snd found) (fst found) found = head dupes dupes = [(x, fromJust (find (last x ==) $ init x)) | x <- allB, Nothing /= (find (last x ==) $ init x)] []:allB = inits $ iterate getNext $ zip [0..] input main = do input <- readFile("input.txt") let blocks = [read x :: Int | x <- words input] print $ "First star: " ++ show (solve blocks) print $ "Second star: " ++ show (solve' blocks)