Skip to content

Commit 34068bf

Browse files
committed
feat(2024/Day06)
1 parent 275b16b commit 34068bf

File tree

5 files changed

+251
-1
lines changed

5 files changed

+251
-1
lines changed

2024/Day06/Day06.hs

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
module Main where
2+
3+
import Data.List
4+
import System.Environment
5+
import Data.Set (Set)
6+
import Data.Set qualified as S
7+
-- TODO: Cleanup imports after day done
8+
9+
10+
data Heading = UP | RIGHT | DOWN | LEFT deriving(Show, Eq, Ord)
11+
type Input = ([String], (Int, Int), (Int, Int), Heading)
12+
type Output = Int
13+
14+
parseInput :: String -> Input
15+
parseInput s = (lines s, (width, height), pos, UP)
16+
where
17+
height = length $ lines s
18+
width = length . head $ lines s
19+
pos = head . filter (\(x, y) -> (== '^') . (!! x) . (!! y) $ lines s) $ [(x, y) | x<-[0..width-1], y<-[0..height-1]]
20+
21+
22+
turn :: Heading -> Heading
23+
turn UP = RIGHT
24+
turn RIGHT = DOWN
25+
turn DOWN = LEFT
26+
turn LEFT = UP
27+
28+
move :: Heading -> (Int, Int) -> (Int, Int)
29+
move UP (x, y) = (x, y-1)
30+
move RIGHT (x, y) = (x+1, y)
31+
move DOWN (x, y) = (x, y+1)
32+
move LEFT (x, y) = (x-1, y)
33+
34+
isOut :: (Int, Int) -> (Int, Int) -> Bool
35+
isOut (width, height) (x, y) = x < 0 || y < 0 || x >= width || y >= height
36+
37+
cellAt :: [String] -> (Int, Int) -> Char
38+
cellAt grid (x, y) = (grid !! y) !! x
39+
40+
visitedCells :: Input -> Set (Int, Int)
41+
visitedCells (grid, dim, pos, dir) = moveOut pos dir S.empty
42+
where
43+
moveOut xy dir positions
44+
| isOut dim (move dir xy) = newPoss
45+
| cellAt grid (move dir xy) == '#' = moveOut (move newDir xy) newDir newPoss
46+
| otherwise = moveOut (move dir xy) dir newPoss
47+
where
48+
newPoss = (S.insert xy positions)
49+
newDir = turn dir
50+
51+
52+
part1 :: Input -> Output
53+
part1 = S.size . visitedCells
54+
55+
part2 :: Input -> Output
56+
part2 (grid, (dim@(width, height)), pos, dir) = length . filter (isLoop pos dir S.empty) . map insertObstacle $ wallsPos
57+
where
58+
visited = visitedCells (grid, dim, pos, dir)
59+
wallsPos = [(x, y) | x<-[0..width-1], y<-[0..height-1], ((x, y) `S.member` visited) && cellAt grid (x, y) /= '#']
60+
61+
insertObstacle :: (Int, Int) -> [String]
62+
insertObstacle (x, y) = linesBefore ++ [elemsBefore ++ "#" ++ elemsAfter] ++ linesAfter
63+
where
64+
(linesBefore, lineAt:linesAfter) = splitAt y grid
65+
(elemsBefore, elemAt:elemsAfter) = splitAt x lineAt
66+
67+
isLoop :: (Int, Int) -> Heading -> Set ((Int, Int), Heading) -> [String] -> Bool
68+
isLoop xy dir positions grid
69+
| (xy, dir) `S.member` positions = True
70+
| isOut dim xy = False
71+
| isOut dim (move dir xy) = False
72+
| cellAt grid (move dir xy) == '#' = isLoop xy newDir newPos grid
73+
| otherwise = isLoop (move dir xy) dir newPos grid
74+
where
75+
newPos = (S.insert (xy, dir) positions)
76+
newDir = turn dir
77+
78+
79+
main :: IO ()
80+
main = do
81+
args <- getArgs
82+
content <- readFile (last args)
83+
let input = parseInput content
84+
85+
print $ (== 5067) $ part1 input
86+
print $ (== 1793) $ part2 input

2024/Day06/Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
SRC=./Day06.hs
3+
TARGET=Day06
4+
all: $(TARGET)
5+
6+
$(TARGET): $(SRC)
7+
ghc -O3 $(SRC)
8+
9+
clean:
10+
$(RM) ./Day06 ./Day06.o ./Day06.hi
11+
12+
.PHONY: all $(TARGET) clean

2024/Day06/input.txt

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
.........#...................................................................................................#.................#..
2+
...........................#.......................................#..............................................................
3+
.............#.................................................................................#............................#.....
4+
...#.....................#.......#....................................#..............#.............................#...........#.#
5+
..........#.....#..#.....#.............#..................#............#.....#........#.#.......#............................#....
6+
...#...................#..........#...................#.........#..#..............##.......................................#......
7+
..................................................................................................#...............................
8+
...............#.........#....#.#..#.........#...........#...#....................#.........#.......#....#........................
9+
..........................................#......#.#...................................................#.............#............
10+
....................#............................#..........................................#.....................#.........#.....
11+
..............#..................................#................#..#...........................................#................
12+
.........#....###.......#........................................................................................#.#.....#...#..#.
13+
.#....................................#.....................................#.................................#..................#
14+
........................#...................................#.#....................#......##.#.........#..........................
15+
............#.......#..................#..........................................................................................
16+
....#.............#.#.....#...#.....#......................#...............#............##...#....................................
17+
...#....#.....................................##........#........#....##.....................................#....................
18+
...........................................................................#........#..........##................................#
19+
..............................#.#......................#..........................#..#........#...............................#...
20+
..............#.........###.......................................#...........#...............#......#.........#........#.....#...
21+
............#............................#....................#........................#............#.............................
22+
.........................................#.....................................................#.....#...............#............
23+
.#...##.......................#.......#..........................................................................#...#...........#
24+
....#..............#............#......#........................#.......................#...#.......................#.............
25+
.......#...............#......#......#..........................................#..................#....................#...#.....
26+
.......................................................................#.........#................................................
27+
.#.......#........................................................##..............................................................
28+
.........................#...................#..#......##..................................#......#......#........................
29+
......................................#..............................................#......#..........#.#.........#..............
30+
.....#..............#..#........#..............................#...##.............................................................
31+
..##............................#.......................................................#..........#..............................
32+
...........................................................##................................................#....................
33+
#...............#.........#.....................................................#......................###........................
34+
#.#.......................#........................#..#.....................#....#........#........................#...#.........#
35+
............#.............#.#............#........................#..........#....................................................
36+
............#...................#.....................................................................................#.......#...
37+
.............................#...#..#...........................#...#.........................................................#...
38+
........#.......................##..........................................................#.........#.....................#.....
39+
............##............#..........................................................#.#.....#......#.#.......#...................
40+
...........#.....#......#.................#.............#......................................#.....#...#........................
41+
#............#.........#.......##.....#......#...........#...........#.......#................#...................................
42+
......#..#...#.....#................................................#............#...#..........^.................................
43+
............................................................................................................#....#................
44+
......................................#.............................#........................#...............#.....#...#..........
45+
...#.........................................................................#....................................................
46+
.....................................#.........................................................#.............#.........#..........
47+
#..............................#...#.................................#............................................................
48+
..........#..................#............#.......#...........................#...................#.....#.........................
49+
............................#...##.............................................................#............#.....................
50+
.................#..................................................................#..#..............#...........................
51+
..................#...............................#................#............#...........#......#............#.................
52+
.....#......................#..............#.......................#......................#.......................................
53+
...............................#.....#.......................#.......#......................#......#............#...........#.....
54+
......#.............##....#.....#...............#.....#............................#....................................#.........
55+
...#.#.......#..#...##.............................................................#.#.#...........#.....#................#.......
56+
...........................................#...#......#...........................................................................
57+
.........#.................................#........................................................................#........#.#..
58+
........................#..........................................#.......#..............#.......................................
59+
....................................................#...............#..........................#..................................
60+
............................#................#......#...............#.....................#.......................................
61+
#.#............#......................................................................................#.....#.....................
62+
................................................................#....#..........................................#........#......#.
63+
.......#........#......................................................................#.................#..................#.#...
64+
...............................#...#......................................................................#.........#.........#...
65+
...........#..#..................##..#.........................................#..............##............................#.....
66+
#........#...............#.....#.....#.................#............#....#.............................#.......#.................#
67+
...............#...................................................#.......#.......................#......#.............#.........
68+
...#............................................................................................................................#.
69+
#.....................#......................................................#...........................................#........
70+
...........#..........................................#.......................................#.....................#.............
71+
....#.##.......#..............#.....#..............................#....................................#.....................#...
72+
...............................................................................................................#...............#..
73+
.#...........................#...#.....#...#...................#....#........#.........#................................#.........
74+
.................#............................................................................#........................#..#.......
75+
.............#....#.....................................................#............#.........................#.......#..........
76+
#....#........................#....................................................................##.............................
77+
...#.#.....................................#...............................................#.....................#....#.........#.
78+
.........#..............................#....................................#.................#............#.....................
79+
................................................................#.................................................................
80+
#.......#..............................................#.............................#...........#................................
81+
.#......#.........#.............................#...............................................................#.................
82+
.........#.....#......#.......................................................................................................#...
83+
#..............................#........#............................#.................................#.....#.....#..............
84+
......#.....................................#.......#........#....................................................................
85+
#.......#.....................................#......................................................................#............
86+
.....................................#......................................#......#.##..................#.....#.#................
87+
.......................#.........#......#...........................................................................##............
88+
.....#......#.#.................................................#.............................#............#.#.......#............
89+
..................#.......................................#.#.............................#..................................#....
90+
......................#.......#.....................#...................#......#.........................................#........
91+
.........#...................................................................................................#....#...............
92+
.........................#...........#..........#...............#.....#...................................................##......
93+
#...#......#.#.....................#..................#.................#..................#......................................
94+
..............................#.....#................#.....................#............#...#....#............#...#...............
95+
.........................#..................#.............................................................................#.......
96+
.......#....................#.......................#.....................#..........#...........#..........#...................#.
97+
....#..........#..#................#.....#..........#........#..................#.....................##...............#.........#
98+
.....................................................................................#......................................#.....
99+
.......................................#...................................#.......................#..........................#...
100+
.......#...............................#............#.....#..............................................#.................#......
101+
.#..........................#..................................................................#...#.#.......#.#..........#.......
102+
#...............................................................#.#............................#...............#......#...........
103+
...#..........................#.#..#.#.............................................................#......#.......................
104+
..................#........#.....................................................#...............................................#
105+
....................#.............#.........#........##................#.............#............#..#............................
106+
.........................#.................................#...........................................#.....#....................
107+
..........................................##..........#................................#........................................#.
108+
.................................................................................#..........#....................##........##.....
109+
.....###......#.....#...................................................................................#..#...................#..
110+
........#.....#.....#.....#..........#.................................................#.#......................#.................
111+
...........................................#....#..............#..#..............#................................................
112+
....#.............#...................................#.....#........................................................#............
113+
.........#........#.............#..................#..........#..........................###.......#....#.#.#.....................
114+
..................#....................#...................#...#..................................................................
115+
.......#...............#................................................................................#................#........
116+
....#..........................#......#....................................#.............#...#......................#...#.........
117+
.#..#....................#.....................#.............#...............................#....................#....##.........
118+
..#..#........#......#.................##..#.......#....................#.............................................#...........
119+
..................#......#.............#.##.................................##.....................................#...#..........
120+
.#...##........#........................................................................................#.....#...#....#..........
121+
............#........................#............................................#.........#.............#.......................
122+
................#......#........#...........................................................#............#........................
123+
..............................#.#......#................................#...........#......................................#......
124+
...................#...................#.............#............#.............#.................................................
125+
.........#.......#....#........#.........#.....................#..#.......#..#...#.............................#..................
126+
.........#...#.........................................................................#.....#.......#............................
127+
.........................#..........#...............................................#.....................................#....#..
128+
....#..................##..##...........#.........#..................................................#...#...........#...........#
129+
.......#.......#.....#...............#.#.....#.............#........................#..............................#..#.........#.
130+
........................#........................................#.............................##.................................

0 commit comments

Comments
 (0)