Skip to content

Commit 9d0f440

Browse files
committed
feat(2024/Day07)
1 parent f9e55c9 commit 9d0f440

File tree

5 files changed

+938
-1
lines changed

5 files changed

+938
-1
lines changed

2024/Day07/Day07.hs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Main where
2+
3+
import System.Environment
4+
5+
type Input = [(Int, [Int])]
6+
type Output = Int
7+
8+
parseInput :: String -> Input
9+
parseInput = map parseLine . lines
10+
where
11+
getTestValue = read . takeWhile (not . (== ':'))
12+
getOperation = map (\c -> if c == ' ' then ',' else c) . drop 2 . dropWhile (not . (== ':'))
13+
parseLine l = (getTestValue l, read ("[" ++ getOperation l ++ "]") :: [Int])
14+
15+
canBeDone :: [(Int -> Int -> Int)] -> Int -> [Int] -> Bool
16+
canBeDone moves val l = sub (head l) (tail l)
17+
where
18+
{-# SCC sub #-}
19+
sub :: Int -> [Int] -> Bool
20+
sub curr [] = val == curr
21+
sub curr (e:l) = any (\op -> sub (op curr e) l) moves
22+
23+
part1 :: Input -> Output
24+
part1 = sum . map fst . filter (uncurry (canBeDone [{-# SCC add #-}(+), {-# SCC time #-}(*)]))
25+
26+
part2 :: Input -> Output
27+
part2 = sum . map fst . filter (uncurry (canBeDone [{-# SCC add #-}(+), {-# SCC times #-} (*), {-# SCC concat #-} concatNb]))
28+
where
29+
concatNb :: Int -> Int -> Int
30+
concatNb a b = a * (((^) 10) $ length $ show b) + b
31+
32+
main :: IO ()
33+
main = do
34+
args <- getArgs
35+
content <- readFile (last args)
36+
let input = parseInput content
37+
38+
print $ (== 1298300076754) $ part1 input
39+
print $ (== 248427118972289) $ part2 input

2024/Day07/Makefile

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

0 commit comments

Comments
 (0)