|
| 1 | +module Main where |
| 2 | + |
| 3 | +import Data.Bits |
| 4 | +import Data.Function |
| 5 | +import Data.List |
| 6 | +import Data.List.Split |
| 7 | +import Data.List.Unique |
| 8 | +import Data.Map (Map) |
| 9 | +import Data.Map qualified as M |
| 10 | +import Data.Matrix (Matrix, (!)) |
| 11 | +import Data.Matrix qualified as Mat |
| 12 | +import Data.Set (Set) |
| 13 | +import Data.Set qualified as S |
| 14 | +import Data.Tuple.Extra |
| 15 | +import Debug.Trace |
| 16 | +import System.Environment |
| 17 | +import Text.Regex.TDFA ((=~)) |
| 18 | + |
| 19 | +-- TODO: Cleanup imports after day done |
| 20 | + |
| 21 | +type Input = [Int] |
| 22 | + |
| 23 | +type Output = Int |
| 24 | + |
| 25 | +parseInput :: String -> Input |
| 26 | +parseInput = map read . lines |
| 27 | + |
| 28 | +simulate :: Int -> Int |
| 29 | +simulate n = f3 . f2 . f1 $ n |
| 30 | + where |
| 31 | + mix = xor |
| 32 | + prune = (`mod` 16777216) |
| 33 | + f1 n' = prune . mix n' $ n' * 64 |
| 34 | + f2 n' = prune . mix n' $ n' `div` 32 |
| 35 | + f3 n' = prune . mix n' $ n' * 2048 |
| 36 | + |
| 37 | +part1 :: Input -> Output |
| 38 | +part1 input = sum . map (!! 2000) $ map (iterate simulate) input |
| 39 | + |
| 40 | +uniqBy :: (Ord b) => (a -> b) -> [a] -> [a] |
| 41 | +uniqBy f [] = [] |
| 42 | +uniqBy f (e : l) = e : sub f (f e) l |
| 43 | + where |
| 44 | + sub :: (Ord b) => (a -> b) -> b -> [a] -> [a] |
| 45 | + sub f _ [] = [] |
| 46 | + sub f lastE (e : l) |
| 47 | + | currVal == lastE = sub f lastE l |
| 48 | + | otherwise = e : sub f currVal l |
| 49 | + where |
| 50 | + currVal = f e |
| 51 | + |
| 52 | +part2 :: Input -> Output |
| 53 | +part2 input = maximum bananas |
| 54 | + where |
| 55 | + prices = {-# SCC prices #-} map (map (`mod` 10)) $ map (iterate simulate) input |
| 56 | + |
| 57 | + pricesZipped :: [[(Int, Int)]] |
| 58 | + pricesZipped = {-# SCC pricesZipped #-} map (\n -> zip n $ drop 1 n) $ prices |
| 59 | + |
| 60 | + pricesDiff :: [[(Int, Int)]] |
| 61 | + pricesDiff = {-# SCC pricesDiff #-} map (map (\n -> second ((-) 0) $ second ((-) $ fst n) n)) $ pricesZipped |
| 62 | + |
| 63 | + withDiffPrefix :: [[(Int, [Int])]] |
| 64 | + withDiffPrefix = {-# SCC withDiffPrefix #-} map (\n -> take 1997 $ map (\l -> (fst (l !! (4)), map snd $ take 4 $ l)) $ tails n) $ pricesDiff |
| 65 | + |
| 66 | + sortedByDiffPref :: [(Int, [Int])] |
| 67 | + sortedByDiffPref = {-# SCC sortedByDiffPref #-} sortOn snd . concat $ map (uniqBy snd . sortOn snd) $ withDiffPrefix |
| 68 | + |
| 69 | + groupedByDiffPref :: [[(Int, [Int])]] |
| 70 | + groupedByDiffPref = {-# SCC groupedByDiffPref #-} groupBy (\e1 e2 -> snd e1 == snd e2) $ sortedByDiffPref |
| 71 | + |
| 72 | + bananas :: [Int] |
| 73 | + bananas = {-# SCC bananas #-} map (sum . map fst) $ groupedByDiffPref |
| 74 | + |
| 75 | +main :: IO () |
| 76 | +main = do |
| 77 | + args <- getArgs |
| 78 | + content <- readFile (last args) |
| 79 | + let input = parseInput content |
| 80 | + |
| 81 | + -- print input |
| 82 | + |
| 83 | + print $ part1 input -- (== 17262627539) $ |
| 84 | + print $ part2 input -- (== 1986) $ |
0 commit comments