Skip to content

Commit be96e83

Browse files
committed
feat(2024/Day11): part1, part2 too slow
1 parent 322bff8 commit be96e83

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

2024/Day11/Day11.hs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module Main where
2+
3+
import Control.Parallel.Strategies
4+
import Data.List.Split
5+
import Debug.Trace
6+
import System.Environment
7+
8+
-- TODO: Cleanup imports after day done
9+
10+
type Input = [Int]
11+
12+
type Output = Int
13+
14+
parseInput :: String -> Input
15+
parseInput = map read . splitOn " " . head . lines
16+
17+
digitCount :: Int -> Int
18+
digitCount 0 = 1
19+
digitCount n = countThem n
20+
where
21+
countThem i
22+
| i < 10 = 1
23+
| otherwise = 1 + countThem (div i 10)
24+
25+
splitNumber :: Int -> Int -> [Int]
26+
splitNumber n lengthn = [firstHalf, secondHalf]
27+
where
28+
-- lengthn = digitCount n
29+
powered10 = {-# SCC powered10 #-} (10 ^ (div lengthn 2))
30+
firstHalf = {-# SCC firstHalf #-} div n powered10
31+
secondHalf = {-# SCC secondHalf #-} n - (firstHalf * powered10)
32+
33+
iterateStones :: [Int] -> [Int]
34+
iterateStones stones = concat $ map iterateStone stones
35+
where
36+
iterateStone 0 = [1]
37+
iterateStone stone
38+
| isSplitable = splitNumber stone stoneLength
39+
| otherwise = [stone * 2024]
40+
where
41+
isSplitable = {-# SCC isSplitable #-} (== 0) $ (`mod` 2) stoneLength
42+
stoneLength = {-# SCC stoneLength #-} digitCount stone
43+
44+
solve :: Int -> Input -> Output
45+
solve n arr = length . fst . head . drop n $ iterate (\(l, i) -> trace (show (i, length l)) (iterateStones l, i + 1)) (arr, 0)
46+
47+
part1 :: Input -> Output
48+
part1 = solve 25
49+
50+
part2 :: Input -> Output
51+
part2 = solve 35
52+
53+
main :: IO ()
54+
main = do
55+
args <- getArgs
56+
content <- readFile (last args)
57+
let input = parseInput content
58+
59+
print input
60+
61+
print $ part1 input
62+
print $ part2 input

2024/Day11/Makefile

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
CONTAINER_NAME=haskell-aoc
2+
SRC=./Day11.hs
3+
TARGET=Day11
4+
all: $(TARGET)
5+
6+
$(TARGET): $(SRC)
7+
ghc -O3 $(SRC)
8+
9+
profile: $(SRC)
10+
11+
12+
clean:
13+
$(RM) ./Day11 ./Day11.o ./Day11.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 shortinput.txt $(CONTAINER_NAME):/home/haskell/shortinput.txt
19+
20+
profiling: setup
21+
docker exec -it $(CONTAINER_NAME) cabal build --enable-profiling
22+
docker exec -it $(CONTAINER_NAME) ./profile shortinput.txt
23+
24+
run: setup
25+
docker exec -it $(CONTAINER_NAME) cabal build
26+
docker exec -it $(CONTAINER_NAME) ./run input.txt
27+
28+
.PHONY: all $(TARGET) clean setup profiling run

2024/Day11/input.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8069 87014 98 809367 525 0 9494914 5

2024/Day11/shortinput.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
125 17

2024/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,17 @@ Tried more optimizations such as saving the first valid free spot but this was o
131131
Today was so short that I didn't even take time to enjoy it...
132132

133133
It was like asking 2+2...
134+
135+
### Day 11:
136+
137+
First part was interesting, managed to realize that `++` is the worst operator ever and that you should always use `concat` instead
138+
139+
For the second part, I managed to optimize and go up to `38` quite far from the required `75`
140+
141+
Tried to multithread it (in the `iterateStones` change `map` to `parMap rseq`) but it only slowed things down...
142+
(for target 35 had 4s in normal map and 12 using rseq...)
143+
144+
- rseq : 12
145+
- r0 : 11.7
146+
- rdeepseq: 9.6
147+
- rpar : 12.7

0 commit comments

Comments
 (0)