Skip to content

Commit 58e7737

Browse files
committed
feat(2024/Day10)
1 parent 8f11036 commit 58e7737

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

2024/Day10/Day10.hs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module Main where
2+
3+
import Data.Bits
4+
import Data.List
5+
import Data.List.Split
6+
import Data.List.Unique
7+
import Data.Matrix (Matrix, (!))
8+
import Data.Matrix qualified as Mat
9+
import Data.Set (Set)
10+
import Data.Set qualified as S
11+
import System.Environment
12+
-- TODO: Cleanup imports after day done
13+
14+
type Input = (Matrix Int, [(Int, Int)])
15+
type Output = Int
16+
17+
data Dir = UP | LEFT | RIGHT | DOWN deriving (Show, Eq)
18+
19+
parseInput :: String -> Input
20+
parseInput s = (mat, starts)
21+
where
22+
mat = Mat.fromLists . map (map (read . (:[]))) $ lines s
23+
starts = [(y, x) | y<-[1..Mat.nrows mat], x<-[1..Mat.ncols mat], mat ! (y,x) == 0]
24+
25+
move :: (Int, Int) -> Dir -> (Int, Int)
26+
move (y, x) UP = (y-1, x)
27+
move (y, x) DOWN = (y+1, x)
28+
move (y, x) LEFT = (y, x-1)
29+
move (y, x) RIGHT = (y, x+1)
30+
31+
isOut :: (Int, Int) -> (Int, Int) -> Bool
32+
isOut (width, height) (y, x) = x <= 0 || y <= 0 || x > width || y > height
33+
34+
validEndTrails :: Matrix Int -> Int -> (Int, Int) -> [(Int, Int)]
35+
validEndTrails grid curr pos
36+
| isOut (Mat.ncols grid, Mat.nrows grid) pos = []
37+
| grid ! pos /= curr + 1 = []
38+
| grid ! pos == 9 = [pos]
39+
| otherwise = concat $ map (validEndTrails grid (grid ! pos) . move pos) [UP, DOWN, LEFT, RIGHT]
40+
41+
part1 :: Input -> Output
42+
part1 (grid, pos) = sum $ map (length . sortUniq . validEndTrails grid (-1)) pos
43+
44+
part2 :: Input -> Output
45+
part2 (grid, pos) = sum $ map (length . validEndTrails grid (-1)) pos
46+
47+
main :: IO ()
48+
main = do
49+
args <- getArgs
50+
content <- readFile (last args)
51+
let input = parseInput content
52+
53+
print $ part1 input
54+
print $ part2 input

2024/Day10/Makefile

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
CONTAINER_NAME=haskell-aoc
2+
SRC=./Day10.hs
3+
TARGET=Day10
4+
all: $(TARGET)
5+
6+
$(TARGET): $(SRC)
7+
ghc -O3 $(SRC)
8+
9+
profile: $(SRC)
10+
11+
12+
clean:
13+
$(RM) ./Day10 ./Day10.o ./Day10.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 input.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/Day10/input.txt

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
6541001098012789610347890107654656710323
2+
7832102127643898701256521218323465891410
3+
8996543034556789650987434309012534892565
4+
3887689678965876501874345892105621763676
5+
4305678563456903416765676756898760654980
6+
5214107852107812321254382347872108901221
7+
6543236943056921010341291078963457654338
8+
7896545987045430010980012569454968983549
9+
3217830656189899121676101430356879892678
10+
2106921043210778234585232321267898761432
11+
3478854430345665056798743410456901050501
12+
4569763521012552143895654501345012347670
13+
3654012678903443212104309690432167898981
14+
2783656987654874908765218781201254012567
15+
1092347897893965889034765670387063013498
16+
1001298756102456776121874989496122110901
17+
2310891043201307655430923876565434325892
18+
3456780103011218967649810189410145456743
19+
2561078212320989858236702107320236787654
20+
1232569343423874749145893678741199899873
21+
0343454358514565632098704569632087684562
22+
0456789969609034501347612189323456893001
23+
1499876878798123101256543079012548762110
24+
2387905462687678871212344568187659450223
25+
3456012301056549960305650127691098321054
26+
3456732102345832154454781034540107650169
27+
2369847898738981023763692321121256743278
28+
1078456654567670119832103400012349894361
29+
0012387763456543208041076510123412765010
30+
7650196892565454589107889623296503854321
31+
8943256781074303673236908774387654983432
32+
8912965890985210984365219985345015676541
33+
7607834187866789875434308776236723498650
34+
6506543045679012766923105698109894567743
35+
5410432134988703457810014567056210754892
36+
0322345028767845893456723459847349889701
37+
1201276719454936712679801210738256776545
38+
2450989805103221604589752345629145480230
39+
2347823456012120113298943238710076591121
40+
1056910147893012320107654109656789432012

2024/Day10/shortinput.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
89010123
2+
78121874
3+
87430965
4+
96549874
5+
45678903
6+
32019012
7+
01329801
8+
10456732

2024/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,9 @@ So I indeed took time to do some optimizations, now I run in 2s.
125125
The main reason for this time improvement is because I now play using a tuple (id, size) and not just the id, hence less items to loop through
126126

127127
Tried more optimizations such as saving the first valid free spot but this was only slowing things down
128+
129+
### Day 10:
130+
131+
Today was so short that I didn't even take time to enjoy it...
132+
133+
It was like asking 2+2...

0 commit comments

Comments
 (0)