Skip to content

Commit 68a1641

Browse files
committed
day6 part one
1 parent 53c6d3f commit 68a1641

File tree

5 files changed

+234
-7
lines changed

5 files changed

+234
-7
lines changed

aoc2024.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ source-repository head
2525

2626
library
2727
exposed-modules:
28+
Data.Matrix
2829
Day1
2930
Day2
3031
Day3
3132
Day4
32-
Day4.Matrix
3333
Day5
34+
Day6
3435
other-modules:
3536
Paths_aoc2024
3637
autogen-modules:

input/day6.txt

Lines changed: 130 additions & 0 deletions
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+
.....................................................#.........#.......................................#.....##..#................
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{-# LANGUAGE LambdaCase #-}
22
{-# LANGUAGE ScopedTypeVariables #-}
33

4-
module Day4.Matrix
4+
module Data.Matrix
55
( Matrix,
6+
Position,
67
buildMatrix,
78
groupWith,
9+
lookupValue,
810
lookup,
911
lookupMultiple,
1012
filterWithKey,
@@ -13,13 +15,16 @@ module Day4.Matrix
1315
where
1416

1517
import qualified Data.IntMap.Lazy as IntMap
18+
import Data.List (find)
1619
import Data.Map.Lazy (Map)
1720
import qualified Data.Map.Lazy as Map
1821
import Data.Maybe
1922
import Prelude hiding (lookup)
2023

2124
newtype Matrix v = Matrix (Map (Int, Int) v)
2225

26+
type Position = (Int, Int)
27+
2328
buildMatrix :: [[a]] -> Matrix a
2429
buildMatrix xs = Matrix (go xs 0 Map.empty)
2530
where
@@ -36,21 +41,31 @@ buildMatrix xs = Matrix (go xs 0 Map.empty)
3641
size :: Matrix v -> Int
3742
size (Matrix hmap) = Map.size hmap
3843

39-
filterWithKey :: ((Int, Int) -> v -> Bool) -> Matrix v -> Matrix v
44+
filterWithKey :: (Position -> v -> Bool) -> Matrix v -> Matrix v
4045
filterWithKey f (Matrix hmap) = Matrix (Map.filterWithKey f hmap)
4146

42-
lookup :: (Int, Int) -> Matrix v -> Maybe v
47+
lookup :: Position -> Matrix v -> Maybe v
4348
lookup position (Matrix hmap) = Map.lookup position hmap
4449

45-
lookupMultiple :: [(Int, Int)] -> Matrix v -> [v]
50+
lookupMultiple :: [Position] -> Matrix v -> [v]
4651
lookupMultiple positions matrix = mapMaybe (`lookup` matrix) positions
4752

53+
{--
54+
Search for the given value on the matrix.
55+
Return the position of the first match if found and nothing if it doens't exist.
56+
--}
57+
lookupValue :: (Eq v) => v -> Matrix v -> Maybe Position
58+
lookupValue v (Matrix hmap) =
59+
let entries = Map.toAscList hmap
60+
mEntry = find ((== v) . snd) entries
61+
in fst <$> mEntry
62+
4863
{--
4964
Given a matrix of elements and a function mapping a position into an aggregation of its values,
5065
group the elements by the aggregation result.
5166
The values are grouped in order.
5267
--}
53-
groupWith :: forall v. ((Int, Int) -> Int) -> Matrix v -> [[v]]
68+
groupWith :: forall v. (Position -> Int) -> Matrix v -> [[v]]
5469
groupWith f (Matrix hmap) =
5570
let sortedEntries = Map.toAscList hmap
5671
intMap = foldr (\(position, value) -> insertValue (f position) value) IntMap.empty sortedEntries

src/Day4.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Day4 (partOne, partTwo) where
22

33
import Data.List (sort, transpose)
4-
import qualified Day4.Matrix as M
4+
import qualified Data.Matrix as M
55

66
partOne :: String -> Int
77
partOne input =

0 commit comments

Comments
 (0)