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

Skip to content

Commit a5890e9

Browse files
committed
day10+11
1 parent 4b0a538 commit a5890e9

File tree

7 files changed

+228
-0
lines changed

7 files changed

+228
-0
lines changed

day10/one.hs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import Data.Char
2+
import Text.Parsec
3+
import Parsing
4+
import qualified Data.HashMap.Strict as M
5+
import Chart2d
6+
import Data.List
7+
8+
main :: IO ()
9+
main = optimisticInteract readD solve
10+
11+
readD :: Parser [[Char]]
12+
readD = (many1 $ oneOf "|-LJ7F.S") `sepEndBy` newline
13+
14+
15+
solve lines = unlines [
16+
show $ lines
17+
, show start
18+
, show answer
19+
]
20+
where
21+
answer = fillFrom [start] 0 M.empty chart
22+
23+
chart = readM lines
24+
start = head . M.keys $ M.filter (=='S') chart
25+
26+
fillFrom cs v mem ch
27+
| null cs' = v
28+
| otherwise = fillFrom cs' (v+1) mem' ch
29+
where
30+
cs' = concatMap pipenext cs
31+
mem' = M.union mem . M.fromList $ zip cs' (repeat True)
32+
33+
pipenext c = [nx | nx <- next c, any (==c) $ next nx, not $ nx `M.member` mem]
34+
35+
next c@(x,y)
36+
| here == '.' = []
37+
| here == '|' = [(x,y-1),(x,y+1)]
38+
| here == '-' = [(x-1,y),(x+1,y)]
39+
| here == 'L' = [(x,y-1),(x+1,y)]
40+
| here == 'J' = [(x,y-1),(x-1,y)]
41+
| here == '7' = [(x-1,y),(x,y+1)]
42+
| here == 'F' = [(x,y+1),(x+1,y)]
43+
| here == 'S' = [(x+1,y),(x-1,y),(x,y-1),(x,y+1)]
44+
where
45+
here = ch M.! c
46+

day10/test.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.....
2+
.S-7.
3+
.|.|.
4+
.L-J.
5+
.....

day10/test2.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
...........
2+
.S-------7.
3+
.|F-----7|.
4+
.||.....||.
5+
.||.....||.
6+
.|L-7.F-J|.
7+
.|..|.|..|.
8+
.L--J.L--J.
9+
...........

day10/two.hs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import Data.Char
2+
import Text.Parsec
3+
import Parsing
4+
import qualified Data.HashMap.Strict as M
5+
import Chart2d
6+
import Data.List
7+
8+
main :: IO ()
9+
main = optimisticInteract readD solve
10+
11+
readD :: Parser [[Char]]
12+
readD = (many1 $ oneOf "|-LJ7F.S") `sepEndBy` newline
13+
14+
15+
solve lines = unlines [
16+
show $ lines
17+
, showMC chart
18+
, show v
19+
, show answer
20+
]
21+
where
22+
answer = length $ findClosure loop (mapStart chart start)
23+
(v, loop) = fillFrom [start] 0 M.empty chart
24+
25+
mapStart ch s@(x,y) = M.insert s s' ch
26+
where
27+
s'
28+
| ns == [(x,y-1),(x,y+1)] = '|'
29+
| ns == [(x-1,y),(x+1,y)] = '-'
30+
| ns == [(x+1,y),(x,y-1)] = 'L'
31+
| ns == [(x-1,y),(x,y-1)] = 'J'
32+
| ns == [(x-1,y),(x,y+1)] = '7'
33+
| ns == [(x+1,y),(x,y+1)] = 'F'
34+
where
35+
ns = filter (`M.member` loop) $ pipenext chart s
36+
37+
chart = readM lines
38+
start = head . M.keys $ M.filter (=='S') chart
39+
40+
fillFrom cs v mem ch
41+
| null cs' = (v,mem)
42+
| otherwise = fillFrom cs' (v+1) mem' ch
43+
where
44+
cs' = filter (not . (`M.member` mem)) $ concatMap (pipenext ch) cs
45+
mem' = M.union mem . M.fromList $ zip cs' (repeat True)
46+
47+
pipenext ch c = [nx | nx <- next c, any (==c) $ next nx]
48+
where
49+
next c@(x,y)
50+
| here == '.' = []
51+
| here == '|' = [(x,y-1),(x,y+1)]
52+
| here == '-' = [(x-1,y),(x+1,y)]
53+
| here == 'L' = [(x,y-1),(x+1,y)]
54+
| here == 'J' = [(x,y-1),(x-1,y)]
55+
| here == '7' = [(x-1,y),(x,y+1)]
56+
| here == 'F' = [(x,y+1),(x+1,y)]
57+
| here == 'S' = [(x+1,y),(x-1,y),(x,y-1),(x,y+1)]
58+
where
59+
here = ch M.! c
60+
61+
findClosure loop ch = concatMap (helper False 0) [y | y <- [0..maxy]]
62+
where
63+
(maxx, maxy) = maximum $ M.keys ch
64+
65+
helper _ x _
66+
| x > maxx = []
67+
helper b x y
68+
| not inLoop && b = (x,y) : helper b (x+1) y
69+
| not inLoop = helper b (x+1) y
70+
where
71+
inLoop = M.member (x,y) loop
72+
helper b x y
73+
| here == 'L' && there == 'J' = helper b (x'+1) y
74+
| here == 'L' && there == '7' = helper (not b) (x'+1) y
75+
| here == 'F' && there == '7' = helper b (x'+1) y
76+
| here == 'F' && there == 'J' = helper (not b) (x'+1) y
77+
| here == '|' = helper (not b) (x+1) y
78+
where
79+
here = ch M.! (x,y)
80+
there = ch M.! (x',y)
81+
x' = head . dropWhile (\x' -> ch M.! (x',y) == '-') $ [x+1..maxx]

day11/one.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Data.Char
2+
import Text.Parsec
3+
import Parsing
4+
import qualified Data.HashMap.Strict as M
5+
import Chart2d
6+
import Data.List
7+
8+
main :: IO ()
9+
main = optimisticInteract readD solve
10+
11+
readD :: Parser [[Char]]
12+
readD = (many1 $ oneOf ".#") `sepEndBy` newline
13+
14+
15+
solve galaxy = unlines [
16+
show $ galaxy
17+
, show $ answer
18+
]
19+
where
20+
expand = transpose . concatMap helper . transpose . concatMap helper
21+
where
22+
helper rc
23+
| any (=='#') rc = [rc]
24+
| otherwise = [rc, rc]
25+
26+
manhattan (x1,y1) (x2,y2) = abs (x1-x2) + abs(y1-y2)
27+
28+
galaxyPos = M.keys . M.filter (=='#') . readM $ expand galaxy
29+
30+
answer = sum [manhattan a b | (i, a) <- zip [1..] galaxyPos, b <- drop i galaxyPos]

day11/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+
#...#.....

day11/two.hs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Data.Char
2+
import Text.Parsec
3+
import Parsing
4+
import qualified Data.HashMap.Strict as M
5+
import Chart2d
6+
import Data.List
7+
8+
main :: IO ()
9+
main = optimisticInteract readD solve
10+
11+
readD :: Parser [[Char]]
12+
readD = (many1 $ oneOf ".#") `sepEndBy` newline
13+
14+
15+
solve galaxy = unlines [
16+
show $ galaxy
17+
, show $ answer
18+
]
19+
where
20+
galaxyM = readM galaxy
21+
22+
(expandedX, expandedY) = (findEmpty $ transpose galaxy, findEmpty galaxy)
23+
24+
findEmpty g = map fst . filter helper $ zip [0..] g
25+
where
26+
helper (_,r) = not . any (=='#') $ r
27+
28+
expandGalaxy factor = helper galaxyM (reverse $ sort expandedX) (reverse $ sort expandedY)
29+
where
30+
growth = factor-1
31+
32+
helper gm (ex:exs) eys = helper (expandX gm ex) exs eys
33+
helper gm exs (ey:eys) = helper (expandY gm ey) exs eys
34+
helper gm [] [] = gm
35+
36+
expandX gm ex = M.mapKeys (\(x,y) -> (expandD ex x, y)) gm
37+
expandY gm ey = M.mapKeys (\(x,y) -> (x, expandD ey y)) gm
38+
39+
expandD eD d
40+
| d > eD = d + growth
41+
| otherwise = d
42+
43+
manhattan (x1,y1) (x2,y2) = abs (x1-x2) + abs(y1-y2)
44+
45+
galaxyPos = M.keys . M.filter (=='#') $ expandGalaxy 1000000
46+
47+
answer = sum [manhattan a b | (i, a) <- zip [1..] galaxyPos, b <- drop i galaxyPos]

0 commit comments

Comments
 (0)