Skip to content

Commit ad9d863

Browse files
committed
feat(2024/Day08)
1 parent 31d18b4 commit ad9d863

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

2024/Day08/Day08.hs

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
module Main where
2+
3+
import Data.List
4+
import Data.List.Unique
5+
import System.Environment
6+
7+
type Input = ((Int, Int), [(Char, [(Int, Int)])])
8+
type Output = Int
9+
10+
parseInput :: String -> Input
11+
parseInput s = ((width, height), freqs)
12+
where
13+
ls = lines s
14+
height = length $ ls
15+
width = length . head $ ls
16+
17+
freqs = map (\f -> (f, locations f)) frequencies
18+
frequencies = uniq . sort . filter (/= '.') $ concat ls
19+
locations c = [(x, y) | x<-[0..width-1], y<-[0..height-1], (ls !! y) !! x == c]
20+
21+
isOut :: (Int, Int) -> (Int, Int) -> Bool
22+
isOut (width, height) (x, y) = x < 0 || y < 0 || x >= width || y >= height
23+
24+
diffVect :: (Int, Int) -> (Int, Int) -> (Int, Int)
25+
diffVect (x, y) (x', y') = (x-x', y-y')
26+
27+
addVect :: (Int, Int) -> (Int, Int) -> (Int, Int)
28+
addVect (x, y) (x', y') = (x+x', y+y')
29+
30+
addVectn :: (Int, Int) -> Int -> (Int, Int) -> (Int, Int)
31+
addVectn (x, y) n (x', y') = (x+n*x', y+n*y')
32+
33+
pairs :: [a] -> [(a, a)]
34+
pairs l = [(x,y) | (x:ys) <- tails l, y <- ys]
35+
36+
algo :: [Int] -> Bool -> Input -> [(Int, Int)]
37+
algo range authorizeAntenna (dim, antennaGrid) = nub . concat $ map getAntinodes antennaGrid --
38+
where
39+
getAntinode :: (Int, Int) -> (Int, Int) -> [(Int, Int)]
40+
getAntinode ant1 ant2 = concat [side ant1 $ diffVect ant1 ant2, side ant2 $ diffVect ant2 ant1]
41+
where
42+
side :: (Int, Int) -> (Int, Int) -> [(Int, Int)]
43+
side ant dist = takeWhile (not . isOut dim) $ map (\n -> addVectn ant n dist) range
44+
45+
getAntinodes (_, antennas) = filter ((||) authorizeAntenna . (`notElem` antennas)) . concat . map (uncurry getAntinode) $ pairs antennas
46+
47+
part1 :: Input -> Output
48+
part1 = length . algo [1] False
49+
50+
part2 :: Input -> Output
51+
part2 = length . algo [0..] True
52+
53+
insertAt :: Char -> [String] -> (Int, Int) -> [String]
54+
insertAt c grid (x, y) = linesBefore ++ [charsBefore ++ [c] ++ charsAfter] ++ linesAfter
55+
where
56+
(linesBefore, currLine:linesAfter) = splitAt y grid
57+
(charsBefore, _:charsAfter) = splitAt x currLine
58+
59+
getGrid :: Input -> [(Int, Int)] -> [String]
60+
getGrid ((w, h), freqs) antinodes = antennasGrid
61+
where
62+
blankGrid = [['.' | _<-[1..w]] | _<-[1..h]]
63+
antinodesGrid = foldl (insertAt '#') blankGrid antinodes
64+
antennasGrid = foldl (\g (c, as) -> foldl (insertAt c) g as) antinodesGrid freqs
65+
66+
getGrid1 :: Input -> [String]
67+
getGrid1 input = getGrid input $ algo [1] False input
68+
69+
getGrid2 :: Input -> [String]
70+
getGrid2 input = getGrid input $ algo [0..] True input
71+
72+
main :: IO ()
73+
main = do
74+
args <- getArgs
75+
content <- readFile (last args)
76+
let input = parseInput content
77+
78+
print $ part1 input
79+
print $ part2 input
80+
81+
-- putStrLn $ unlines $ getGrid1 input
82+
-- putStrLn $ unlines $ getGrid2 input

2024/Day08/Makefile

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

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
.......................O......T.....d......M......
2+
..............................F...................
3+
..........V..................R....................
4+
............B..t..........T..........d............
5+
.....................B.........T................M.
6+
..V.................................2.......M.....
7+
.......V........................F.O..........2....
8+
...................................T..............
9+
..................................................
10+
......r..........B......................c.........
11+
.....o3.B.............................2...........
12+
..................1...m..o....d..c.....M..........
13+
......Qr....o............F....0............1......
14+
....Q.......................0....................2
15+
......t..........0................................
16+
.............R.................................mL.
17+
....r..............3.....................c..1.....
18+
.........Q.........................1..............
19+
................x...R.............................
20+
...x........8.R...................................
21+
..................8...............................
22+
........x.u.................Z.....................
23+
...........................X...............d......
24+
....................30.....................f......
25+
......q...............v...................c.......
26+
..........t8.........D.3..........................
27+
.......t.......4.............8....................
28+
...b..................C...........D...............
29+
.........................v..ND4..........K........
30+
.......F.........u...........C..............fZ....
31+
........X..9...........N.........Z..........k.....
32+
.............X.6...q..........................k...
33+
..............................C.Z...........m....k
34+
...................4.v..............N.............
35+
....................u.......D..............m......
36+
............................vl.....UK.............
37+
............................l..6.......f..........
38+
..................q.4............N................
39+
..........b....x..............fu..................
40+
.9..................................U.......l.....
41+
....w......b.........L......6.....z.5.............
42+
..........X..........W6........5............z.....
43+
...........q..........L............z........n...W.
44+
............................5.........n...W..z....
45+
........9........w................7....n..........
46+
............w......................7...K.....n....
47+
.........................U....K......W............
48+
.........w.....L.................k....7...........
49+
...................7.............l.............5..
50+
..............9...................................

2024/Day08/shortinput.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
T.........
2+
...T......
3+
.T........
4+
..........
5+
..........
6+
..........
7+
..........
8+
..........
9+
..........
10+
..........

2024/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,11 @@ At first the bottle neck was the operation `concatNb`, I did it like `read (show
104104
So the solution was just to shift `a` to the left by multiplying it by `((^) 10) . length $ show b`. This does for example with `42` and `24` : `42* 10^2` => `4200` and then only `(+)` => `4224`
105105

106106
At the moment the bottle neck is the operation `(*)` so no really my fault.
107+
108+
### Day 8:
109+
110+
Todays was also fun.
111+
112+
Since solution already is at 0.03s, there is no need to optimize.
113+
114+
Instead I had fun creating a function to preview the actual grid at each step, you just have to uncomment the lines in main to see it.

0 commit comments

Comments
 (0)