summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-12-06 14:36:22 +0000
committerNat Lasseter <user@4574.co.uk>2024-12-06 14:36:22 +0000
commit21851bc112ea69f41ac44350f5f0f3906317513c (patch)
tree66e45fac9d167d315e532d6a7e1d77e61d9fb578
parentea4dfa2548c5aefe45e4f0ce0b7c7fc2e35fb745 (diff)
[Day 06] Part 1
-rw-r--r--day06/day06.hs82
-rw-r--r--day06/day06.input130
2 files changed, 212 insertions, 0 deletions
diff --git a/day06/day06.hs b/day06/day06.hs
new file mode 100644
index 0000000..fa022f7
--- /dev/null
+++ b/day06/day06.hs
@@ -0,0 +1,82 @@
+import Data.List
+import Data.Maybe
+
+data Cell = Clr | Obs deriving (Show, Eq)
+type Grid = [[Cell]]
+type Coo = (Int, Int)
+data Dir = U | R | D | L deriving (Show, Eq)
+type Guard = (Coo, Dir)
+
+(!?) :: Grid -> Coo -> Maybe Cell
+grid !? (row, col)
+ | row < 0 = Nothing
+ | row >= length grid = Nothing
+ | col < 0 = Nothing
+ | col >= (length $ head grid) = Nothing
+ | otherwise = Just (grid !! row !! col)
+
+findGuard :: Int -> [[Char]] -> Guard
+findGuard r (h:t)
+ | c == Nothing = findGuard (r + 1) t
+ | otherwise = ((r, (fromJust c)), U)
+ where c = elemIndex '^' h
+
+consume :: [Char] -> (Grid, Guard)
+consume file =
+ let toCell = \ln -> case ln of
+ '#' -> Obs
+ otherwise -> Clr
+ grid = map (map toCell) (lines file)
+ guard = findGuard 0 (lines file) in
+ (grid, guard)
+
+step :: Grid -> Guard -> Maybe Guard
+step grid (coo@(r,c), U)
+ | cell == Nothing = Nothing
+ | cell == Just Obs = Just (coo, R)
+ | cell == Just Clr = Just (ncoo, U)
+ where
+ ncoo = (r - 1, c)
+ cell = grid !? ncoo
+step grid (coo@(r,c), R)
+ | cell == Nothing = Nothing
+ | cell == Just Obs = Just (coo, D)
+ | cell == Just Clr = Just (ncoo, R)
+ where
+ ncoo = (r, c + 1)
+ cell = grid !? ncoo
+step grid (coo@(r,c), D)
+ | cell == Nothing = Nothing
+ | cell == Just Obs = Just (coo, L)
+ | cell == Just Clr = Just (ncoo, D)
+ where
+ ncoo = (r + 1, c)
+ cell = grid !? ncoo
+step grid (coo@(r,c), L)
+ | cell == Nothing = Nothing
+ | cell == Just Obs = Just (coo, U)
+ | cell == Just Clr = Just (ncoo, L)
+ where
+ ncoo = (r, c - 1)
+ cell = grid !? ncoo
+
+(?:) :: Eq a => a -> [a] -> [a]
+el ?: arr
+ | el `elem` arr = arr
+ | otherwise = el:arr
+
+route :: Grid -> [Coo] -> Guard -> [Coo]
+route grid seen guard@(coo, dir)
+ | nguard == Nothing = coo ?: seen
+ | otherwise = route grid (coo ?: seen) (fromJust nguard)
+ where
+ nguard = step grid guard
+
+part1 :: Grid -> Guard -> Int
+part1 grid guard = length $ route grid [] guard
+
+main = do
+ file <- readFile "day06.input"
+ let (grid, guard) = consume file
+ putStrLn ("Part 1: " ++ (show $ part1 grid guard))
+ --putStrLn ("Part 2: " ++ (show $ part2 lns))
diff --git a/day06/day06.input b/day06/day06.input
new file mode 100644
index 0000000..356b341
--- /dev/null
+++ b/day06/day06.input
@@ -0,0 +1,130 @@
+......#...........#..............................#..............................#...........................##...............#....
+.............#..............#........#......##...................................................#............##...#..........#...
+...#..................#..............#.........#...#.........................................................................#..#.
+.....#..........#...................#.....................#..........#...........#......#..#...................#..................
+...#...............................................##..................#............#.....#...................#...................
+.#.#..#...................#.##.................#..........#................................#......#.........##..................#.
+....................#...................#....#.........#....................#....................................................#
+..........#......#...................................................................................................#....#.......
+.#...............#.........#...........#...............#...................#......#...............................................
+............#...........#........#..##.......#........#.....#................................##...................................
+...................#.#.............###.................##.................#.......................#.....#..............##.........
+.........................#...#............................#......#.......................#........................................
+.....................#..............................................#.......#......##......................................#......
+............................#.#............................................................................#.................#....
+.#.....#.......................................................#..........#.......................................................
+..............#..............................#.....#....#........#....................#....#.#...........#.#..................#...
+........................#...#...##......#...........................#.............#............................#..................
+...#........................................................#...#...........................#...#...............#........#........
+.........#..#..........#.....................................#..................................................................#.
+...........................................#.........#....#........................................#..#...........#...............
+..#..........#...........................................................................................#..............#.........
+...#.......#......................................#...............#...............................................................
+.#........#......#.#....................................#...#...........#..........#........#.#.......#.......................#...
+......................#...........#......##....................#..................#......#..............................#.......#.
+..............#......................#..............#...#.#............................................................#.#........
+..............#..................#...#..................#...#.....#...#...............#...........................................
+.............#............................##..#...#...............................................................##..............
+..#............................#.......#.#....................................#...................................................
+.......#.....#..........#......#.....#...................................#...........................#......#....#..............#.
+.#.................#.............................................#................#.............#.....#....................#......
+..............#.........#..#.....#...........#..............................#.................................#............#......
+....#.#............#............................................................#..........#......................................
+.........................................#.......#......#....................................#.....................#...#......#...
+....#.............................#....................................#..........#................................#..............
+.........#..#......#..............................................................#....................#..........................
+.......................................................##...#........#......................................................#.#..#
+..........................#.......##..##................................................................................#.........
+..................................##..................................................................#..........................#
+.#..................................................#..............#.....................#.....#..................................
+.......##........................#......................................................#..........#..............................
+.........#....#....#.........#.#........................#...#.....................................................#.....#.........
+....#.............................#....#....................#.....................#.....#......#.....................#..........#.
+............................................................................................#..........#......#...................
+..................................................................................#..........................#.#.....#.........#..
+..................................................#...................................................#......#.....#............#.
+..........................................^#................................................................................#.....
+...............#..........#.............................................#.....#....#...................#................#.........
+#..#....#........#........#.........#.......#.................#.......................#.#.............#...................#.......
+..#................................................................#......................................#.......................
+........#......................#.....................##.#..........#.............................#.............................#..
+....#..........#........................#.........................................................................................
+......#...#.#.#.........#.................#.....#.....#...#......#..................................................##........#...
+...#....#....................#...................................................................#......#...............#.........
+..#.......#.........................#..........#........#............................#......#....................#................
+.................................#.......................................#....................................................#..#
+......................#...#........................#.......................#.............#........................................
+......#.......#...................................................................................................#...............
+.............#............#......#............................#...........................................##......................
+..........................#.....................................................................................#.................
+....#..............................#......................#........................................#....#...........#.............
+................#.......#.........................................................................................................
+.............#............#........#.....#.........#.#.#............................#..........#..............#....#.......#.....#
+..#.....#...............................................................................................#...............#.........
+.......#...........#............#................#.......................................#..........................#......#......
+..........#........................................#......................#............#....................................#..#..
+.......#......#..............................#........................................................#.#.................#.......
+.#.................#..#.....................................................................................#...........##........
+#...................................#........................................#..............#.##........#..#.#.............#......
+......#...............#........#....#.........#.........#...#.......................#...........................#............#....
+...#...................................................................................#.......................................#..
+.....#.......................................#................#...............................................................#...
+.#................................................##................................#....................#...............#........
+.................................................#..#..................#.......#........................#...............#.....#...
+.....##.#.............................#.....................#................................................................#...#
+...........#....................#...........#...............#.................................................#.......#.......#..#
+........#....................................................#.....#....#.........................................#.....#.#.......
+...............#..........................#............#.#.................................................#......................
+...........#......#.....................#...................................................................#.#..............#....
+......#...#.........................#..........................................................#..................................
+.....................#................................................#..#........................................................
+..............#.................#............#....................#.#.......................................#............#........
+............#.....................#................#......#..........................#.#.........#............#...................
+........................................................................#.....................#...................................
+.............................#....................................................................................................
+.........................#...#..........................................#............................................#............
+..#...............................#.....#.....#.............#.......#..#.................##.#.......................#.........#.#.
+......................................#...........................................................................................
+...........#.....................................#................#...#.........................#........................#........
+..#...#..#..........................#.......#....#......................................................#................#........
+.....................................#...#.#........#...............#..#......#.....................................#...#.........
+.#..............#..............................................................#.........................##......................#
+..........#..................................#....#.........#...#.#..........#...#...................................##...........
+...............#.....................................................................................................#............
+.#.......#....................#...#..........................#.#...............#......................#........#.................#
+.............#....................#........................#..................#......#....#....#..................................
+........#..............................................................#.....................#....................................
+.......................#..............#.............#......#.........................................#.........................#..
+.......#.......#.#........#.......................#..#.......................................................#.#..................
+...............#..#....#.......................#.....#..............................#..................#.....................#....
+......#..............#...........................#............................##...........................#......................
+.......................................#........#................................#..........................#.....................
+.#.....................................#............#.............................................................#..#............
+..................#..................#.........................#............#..........#.........................#...#............
+.......................................................#..............#..........................................................#
+...#....#..#...#..#...................##....................#.....................#...................#.....#..........#..........
+.......................#........#....................##.................#.......................................................#.
+........#.......#.......................#.#......##...##........#..##.........................................#...............##..
+....#..........................#.........##..#......#...........................#........#........#...#...........................
+...#..........................#.....#.....................................#.......................................................
+....................................##.......#..............................#.............#...#......#.............#..............
+.#..............................#...............................................................................................#.
+.....#................................................................................##.....#..............#.#...................
+...........................................................................................#....#................#................
+...#..#...................................................#....#.......#.......#.................................#........#.......
+.#....#....................#..............................................................................#.............#.......#.
+.........#.................#.......................................#...........#......#.......#............#............#.........
+........................................#...............................#.........................................................
+..............#....#......................#..........#........#.....................................#...........#..........#......
+......##....#.............#..........#......................#.........................................##..........................
+....................#.............................#.....................##.#..........#..#......................###.........#.....
+.#.......................................##........................#.....#.....#..#...............................................
+..............................#............................#.......................................................##.............
+.........#.............................#..............#......#.....#..............#.#............................#........#....#..
+............#..............#.............................#..........................#.............................#...............
+.................#...........#..............#.....#..............................#.............#................#.....#...........
+........#.....................#........#.....................................#..........................#......#.#................
+.....................................................#..#........................#.##..#.........#................................
+......#..............................................#..........................#...............................#..........#......
+.................................................#.................##.......................#...................................#.
+....................#.....#.......#.............#................................#..........#...#.................................