-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay10.hs
148 lines (128 loc) · 3.79 KB
/
Day10.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
module Main where
import Utilities
import Geometry
import Data.List
import Data.Ratio
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set
-- Input processing
type Input = [Position] -- (0,0) is top left
parse :: String -> Input
parse s = [p | (p, c) <- readGrid s, c == '#']
-- Part One
-- directions from pos that contain at least one point
view :: [Position] -> Position -> Set Position
view ps pos =
Set.fromList [fst (normalize (p .-. pos)) | p <- ps, p /= pos]
-- direction in reduced form, with common divisor
normalize :: Position -> (Position, Int)
normalize (Position x y) = (Position (x `div` d) (y `div` d), d)
where
d = gcd x y
-- point with the most other points in view, plus the count
best :: [Position] -> (Int, Position)
best ps = maximum [(Set.size (view ps p), p) | p <- ps]
solve1 :: Input -> Int
solve1 = fst . best
tests1 :: [(String, (Int, Position))]
tests1 = [
(".#..#\n\
\.....\n\
\#####\n\
\....#\n\
\...##\n", (8, Position 3 4)),
("......#.#.\n\
\#..#.#....\n\
\..#######.\n\
\.#.#.###..\n\
\.#..#.....\n\
\..#....#.#\n\
\#..#....#.\n\
\.##.#..###\n\
\##...#..#.\n\
\.#....####\n", (33, Position 5 8)),
("#.#...#.#.\n\
\.###....#.\n\
\.#....#...\n\
\##.#.#.#.#\n\
\....#.#.#.\n\
\.##..###.#\n\
\..#...##..\n\
\..##....##\n\
\......#...\n\
\.####.###.\n", (35, Position 1 2)),
(".#..#..###\n\
\####.###.#\n\
\....###.#.\n\
\..###.##.#\n\
\##.##.#.#.\n\
\....###..#\n\
\..#.#..#.#\n\
\#..#.#.###\n\
\.##...##.#\n\
\.....#.#..\n", (41, Position 6 3)),
(largeExample, (210, Position 11 13))]
largeExample :: String
largeExample =
".#..##.###...#######\n\
\##.############..##.\n\
\.#.######.########.#\n\
\.###.#######.####.#.\n\
\#####.##.#.##.###.##\n\
\..#####..#.#########\n\
\####################\n\
\#.####....###.#.#.##\n\
\##.#################\n\
\#####.##.###..####..\n\
\..######..##.#######\n\
\####.##.####...##..#\n\
\.#####..#.######.###\n\
\##...#.##########...\n\
\#.##########.#######\n\
\.####.#.###.###.#.##\n\
\....##.##.###..#####\n\
\.#.#.###########.###\n\
\#.#.#.#####.####.###\n\
\###.##.####.##.#..##\n"
-- Part Two
-- for each angle, a non-empty list of points at that angle, closest first
multiview :: [Position] -> Position -> Map Angle [Position]
multiview ps pos =
fmap Map.elems $
Map.fromListWith Map.union
[(angle dp, Map.singleton (norm dp) p) |
p <- ps, p /= pos, let dp = p .-. pos]
-- a representation of angles increasing clockwise from straight up
data Angle = Angle {
quadrant :: Int, -- quadrant, counted clockwise from top right
tan :: Ratio Int } -- tangent of angle from start of that quadrant
deriving (Eq, Ord, Show)
-- angle of a non-zero point
angle :: Position -> Angle
angle (Position x y)
| x >= 0 && y < 0 = Angle 0 (- x % y)
| y >= 0 && x > 0 = Angle 1 (y % x)
| x <= 0 && y > 0 = Angle 2 (- x % y)
| y <= 0 && x < 0 = Angle 3 (y % x)
| otherwise = error "angle of (0,0)"
viewFromBest :: [Position] -> Map Angle [Position]
viewFromBest ps = multiview ps (snd (best ps))
-- points in order encountered in clockwise sweeps
vaporize :: Map Angle [Position] -> [Position]
vaporize = concat . transpose . Map.elems
solve2 :: Input -> Int
solve2 ps = 100*x + y
where
Position x y = vaporize (viewFromBest ps)!!199
tests2 :: [(String, Int)]
tests2 = [(largeExample, 802)]
main :: IO ()
main = do
s <- readFile "input/10.txt"
let input = parse s
putStr (unlines (failures "best" (best . parse) tests1))
print (solve1 input)
putStr (unlines (failures "solve2" (solve2 . parse) tests2))
print (solve2 input)