Skip to content

Commit 2d5470e

Browse files
committed
Day15 part1
1 parent fb24ab8 commit 2d5470e

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

Day15.hs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
module Day15 where
2+
3+
import qualified Data.Map as M
4+
import Data.List
5+
import qualified Data.Set as S
6+
7+
data Dir = U|L|D|R
8+
deriving (Show,Eq)
9+
10+
data Thing = Wall | Box
11+
deriving (Show,Eq)
12+
13+
type Pos = (Int,Int)
14+
type Chart = M.Map Pos Thing
15+
16+
data State = State {chart :: Chart, robot :: Pos, moves :: [Dir]}
17+
deriving Show
18+
19+
parse :: String -> State
20+
parse s = State chart robot moves
21+
where ls = lines s
22+
(chartLines,"":moveLines) = break (=="") ls
23+
coords = [((x,y),c) | (y,line) <- zip [0..] chartLines
24+
, (x,c) <- zip [0..] line]
25+
robot = head [pos | (pos,'@') <- coords]
26+
chart = M.fromList [(pos,case c of '#' -> Wall; 'O' -> Box) | (pos,c) <- coords, c /= '@', c /= '.']
27+
parseMove 'v' = D
28+
parseMove '^' = U
29+
parseMove '<' = L
30+
parseMove '>' = R
31+
moves = map parseMove $ concat moveLines
32+
33+
example1 = parse <$> readFile "example1-15"
34+
example2 = parse <$> readFile "example2-15"
35+
slurp = parse <$> readFile "input-15"
36+
37+
visualize :: State -> String
38+
visualize (State chart robot _) = unlines [ [r (x,y) | x <- [0..maxX]] | y <- [0..maxY] ]
39+
where ((maxX,maxY),_) = M.findMax chart
40+
r p
41+
| p == robot = '@'
42+
| otherwise = case M.lookup p chart
43+
of Just Box -> 'O'
44+
Just Wall -> '#'
45+
Nothing -> '.'
46+
47+
(>>>) :: Pos -> Dir -> Pos
48+
(x,y) >>> U = (x,y-1)
49+
(x,y) >>> D = (x,y+1)
50+
(x,y) >>> L = (x-1,y)
51+
(x,y) >>> R = (x+1,y)
52+
53+
swapIn :: Pos -> Thing -> Chart -> (Chart,Maybe Thing)
54+
swapIn p t c = (M.insert p t c, M.lookup p c)
55+
56+
push :: Pos -> Dir -> Chart -> Maybe Chart
57+
push p d original = go remaining (p >>> d) (M.lookup p original)
58+
where remaining = M.delete p original
59+
go c _ Nothing = Just c
60+
go c _ (Just Wall) = Nothing
61+
go c p (Just Box) = let (c',thing) = swapIn p Box c
62+
in go c' (p >>> d) thing
63+
64+
tick :: State -> State
65+
tick (State chart robot (dir:moves)) =
66+
case push robot' dir chart
67+
of Just chart' -> State chart' robot' moves
68+
Nothing -> State chart robot moves
69+
where robot' = robot >>> dir
70+
71+
finish :: State -> State
72+
finish s@(State _ _ []) = s
73+
finish s = finish $ tick s
74+
75+
gps :: State -> Int
76+
gps (State chart _ _) = sum [y*100+x | ((x,y),Box) <- M.toList chart]
77+
78+
part1 = gps . finish
79+
80+
main = do
81+
print . part1 =<< slurp

example1-15

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
########
2+
#..O.O.#
3+
##@.O..#
4+
#...O..#
5+
#.#.O..#
6+
#...O..#
7+
#......#
8+
########
9+
10+
<^^>>>vv<v>>v<<

example2-15

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
##########
2+
#..O..O.O#
3+
#......O.#
4+
#.OO..O.O#
5+
#..O@..O.#
6+
#O#..O...#
7+
#O..O..O.#
8+
#.OO.O.OO#
9+
#....O...#
10+
##########
11+
12+
<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
13+
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
14+
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
15+
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
16+
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
17+
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
18+
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
19+
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
20+
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
21+
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^

0 commit comments

Comments
 (0)