Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 948ddba

Browse files
committed
day 16
1 parent 1592011 commit 948ddba

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

day16/one.hs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Text.Parsec
2+
import Parsing
3+
import Chart2d
4+
import qualified Data.HashMap.Strict as M
5+
import Searches
6+
7+
main :: IO ()
8+
main = optimisticInteract readD solve
9+
10+
readD :: Parser [String]
11+
readD = many1 (oneOf ".|/\\-") `sepEndBy` newline
12+
13+
solve input = unlines [
14+
showMC $ energized
15+
, show $ M.size energized
16+
]
17+
where
18+
inpchart = readM input
19+
20+
energized = M.fromList $ floodFill ((0,0),'R') contBeam
21+
where
22+
contBeam (c,d) = filter contained beams
23+
where
24+
here = inpchart M.! c
25+
beams = [(move c d', d') | d' <- splitBeam here d]
26+
contained (c,_) = M.member c inpchart
27+
28+
splitBeam '.' d = [d]
29+
splitBeam '/' 'R' = "U"
30+
splitBeam '/' 'L' = "D"
31+
splitBeam '/' 'U' = "R"
32+
splitBeam '/' 'D' = "L"
33+
splitBeam '\\' 'R' = "D"
34+
splitBeam '\\' 'L' = "U"
35+
splitBeam '\\' 'U' = "L"
36+
splitBeam '\\' 'D' = "R"
37+
splitBeam '-' d
38+
| d `elem` "UD" = "RL"
39+
| otherwise = [d]
40+
splitBeam '|' d
41+
| d `elem` "RL" = "UD"
42+
| otherwise = [d]
43+
44+
move (x,y) 'R' = (x+1,y)
45+
move (x,y) 'L' = (x-1,y)
46+
move (x,y) 'U' = (x,y-1)
47+
move (x,y) 'D' = (x,y+1)

day16/test.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.|...\....
2+
|.-.\.....
3+
.....|-...
4+
........|.
5+
..........
6+
.........\
7+
..../.\\..
8+
.-.-/..|..
9+
.|....-|.\
10+
..//.|....

day16/two.hs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import Text.Parsec
2+
import Parsing
3+
import Chart2d
4+
import qualified Data.HashMap.Strict as M
5+
import Searches
6+
7+
main :: IO ()
8+
main = optimisticInteract readD solve
9+
10+
readD :: Parser [String]
11+
readD = many1 (oneOf ".|/\\-") `sepEndBy` newline
12+
13+
solve input = unlines [
14+
show $ input
15+
, show answer
16+
]
17+
where
18+
inpchart = readM input
19+
20+
starts = concat $ [[((0,y),'R'),((mx,y),'L')] | y <- [0..my]] ++ [[((x,0),'D'),((x,my),'U')] | x <- [0..mx]]
21+
where
22+
(mx,my) = maximum . M.keys $ inpchart
23+
24+
answer = maximum . map (M.size . energized) $ starts
25+
26+
energized start = M.fromList $ floodFill start contBeam
27+
where
28+
contBeam (c,d) = filter contained beams
29+
where
30+
here = inpchart M.! c
31+
beams = [(move c d', d') | d' <- splitBeam here d]
32+
contained (c,_) = M.member c inpchart
33+
34+
splitBeam '.' d = [d]
35+
splitBeam '/' 'R' = "U"
36+
splitBeam '/' 'L' = "D"
37+
splitBeam '/' 'U' = "R"
38+
splitBeam '/' 'D' = "L"
39+
splitBeam '\\' 'R' = "D"
40+
splitBeam '\\' 'L' = "U"
41+
splitBeam '\\' 'U' = "L"
42+
splitBeam '\\' 'D' = "R"
43+
splitBeam '-' d
44+
| d `elem` "UD" = "RL"
45+
| otherwise = [d]
46+
splitBeam '|' d
47+
| d `elem` "RL" = "UD"
48+
| otherwise = [d]
49+
50+
move (x,y) 'R' = (x+1,y)
51+
move (x,y) 'L' = (x-1,y)
52+
move (x,y) 'U' = (x,y-1)
53+
move (x,y) 'D' = (x,y+1)

0 commit comments

Comments
 (0)