Skip to content

Commit abdab42

Browse files
committed
feat(2024/Day22)
1 parent 888a83d commit abdab42

File tree

5 files changed

+2230
-0
lines changed

5 files changed

+2230
-0
lines changed

2024/Day22/Day22.hs

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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) $

2024/Day22/Makefile

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
CONTAINER_NAME=haskell-aoc
2+
SRC=./Day22.hs
3+
TARGET=Day22
4+
all: $(TARGET)
5+
6+
$(TARGET): $(SRC)
7+
ghc -O3 $(SRC)
8+
9+
profile: $(SRC)
10+
11+
12+
clean:
13+
$(RM) ./Day22 ./Day22.o ./Day22.hi
14+
15+
setup: $(SRC)
16+
docker cp $(SRC) $(CONTAINER_NAME):/home/haskell/Main.hs
17+
docker cp input.txt $(CONTAINER_NAME):/home/haskell/input.txt
18+
docker cp input2.txt $(CONTAINER_NAME):/home/haskell/input2.txt
19+
docker cp shortinput.txt $(CONTAINER_NAME):/home/haskell/shortinput.txt
20+
21+
profiling: setup
22+
docker exec -it $(CONTAINER_NAME) cabal build --enable-profiling
23+
docker exec -it $(CONTAINER_NAME) ./profile input.txt
24+
25+
run: setup
26+
docker exec -it $(CONTAINER_NAME) cabal build
27+
docker exec -it $(CONTAINER_NAME) ./run input.txt
28+
29+
.PHONY: all $(TARGET) clean setup profiling run

0 commit comments

Comments
 (0)