-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay06.hs
74 lines (55 loc) · 2.02 KB
/
Day06.hs
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
module Day06 where
import qualified Data.Set as S
type Pos = (Int,Int)
data Dir = U | D | L | R
deriving (Show,Eq,Ord)
data World = World {width :: Int, height :: Int, obstacles :: S.Set Pos}
deriving Show
data Guard = Guard {position :: Pos, direction :: Dir}
deriving (Show,Eq,Ord)
parse :: String -> (World,Guard)
parse s = (World w h obs, Guard pos U)
where ls = lines s
w = length (head ls)
h = length ls
points = [((x,y),c) | (y,row) <- zip [0..] ls, (x,c) <- zip [0..] row]
obs = S.fromList [p | (p,'#') <- points]
pos = head [p | (p,'^') <- points]
slurp = parse <$> readFile "input-06"
march :: Guard -> Guard
march (Guard (x,y) U) = Guard (x,y-1) U
march (Guard (x,y) D) = Guard (x,y+1) D
march (Guard (x,y) L) = Guard (x-1,y) L
march (Guard (x,y) R) = Guard (x+1,y) R
turnRight' U = R
turnRight' R = D
turnRight' D = L
turnRight' L = U
turnRight :: Guard -> Guard
turnRight (Guard p d) = Guard p (turnRight' d)
inbounds :: World -> Guard -> Bool
inbounds w g = 0 <= x && x < width w && 0 <= y && y < height w
where (x,y) = position g
step :: World -> Guard -> Guard
step world guard
| position next `S.member` obstacles world = turnRight guard
| otherwise = next
where next = march guard
visits :: World -> Guard -> S.Set Pos
visits world guard = S.fromList . map position . takeWhile (inbounds world) $ iterate (step world) guard
part1 :: (World,Guard) -> Int
part1 (world,guard) = S.size $ visits world guard
loops world init = go S.empty init
where go acc g
| g `S.member` acc = True
| not (inbounds world g) = False
| otherwise = go (S.insert g acc) (step world g)
newObstacle p w = w {obstacles = S.insert p (obstacles w)}
part2 :: (World,Guard) -> Int
part2 (world,init) = length (filter works tryObstacles)
where tryObstacles = S.toList $ visits world init
works obs = loops (newObstacle obs world) init
main =
--print . part1 =<< slurp
print . part2 =<< slurp
-- Took 8s when compiled with `ghc -O2 -main-is Day06 --make Day06.hs`