parent
369e70d38e
commit
44730f650f
|
@ -0,0 +1,62 @@
|
|||
import Data.List.Split
|
||||
import qualified Data.Map as M
|
||||
import Control.Lens
|
||||
import Data.List
|
||||
import Linear.V2
|
||||
|
||||
type Sensor = (V2 Int, V2 Int)
|
||||
type Cave = M.Map (V2 Int) Char
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <- map (splitOneOf "=,:") . lines <$> readFile "input"
|
||||
let p = map parse input
|
||||
let c = M.fromList $ fillCave p
|
||||
putStrLn $ intercalate "\n" $ print2D 50 $ map fst $ M.toList c
|
||||
let f = fillCave2 p
|
||||
print $ M.filterWithKey (\(V2 _ a) b -> a == 10) f
|
||||
print $ length $ M.filterWithKey (\(V2 _ a) b -> a == 10 && b == '#') $ M.union f c
|
||||
|
||||
parse :: [String] -> Sensor
|
||||
parse [_, a, _, b, _, c, _, d] = (V2 (read a) (read b), V2 (read c) (read d))
|
||||
|
||||
dist :: Sensor -> Int
|
||||
dist (a, b) = sum $ abs $ a - b
|
||||
|
||||
fillCave :: [Sensor] -> [(V2 Int, Char)]
|
||||
fillCave (x:xs) = sensor ++ fillCave xs
|
||||
where d = dist x
|
||||
pRel = [V2 x y | x<-[(-d)..(d)],y<-[(-d)..(d)], abs x + abs y <= d]
|
||||
sensor = zip (map (+fst x) pRel) (repeat '#')
|
||||
fillCave [] = []
|
||||
|
||||
fillCave2 :: [Sensor] -> Cave
|
||||
fillCave2 (x:xs) = newC `M.union` fillCave2 xs
|
||||
where newC = M.insert (snd x) 'B' $ M.insert (fst x) 'S' (M.empty)
|
||||
fillCave2 [] = M.empty
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
p :: Int -> [Int] -> String
|
||||
p n (x:xs) =
|
||||
if n == x
|
||||
then '#' : p (n + 1) xs
|
||||
else '.' : p (n + 1) (x : xs)
|
||||
p _ [] = []
|
||||
|
||||
print2D :: Int -> [V2 Int] -> [String]
|
||||
print2D n coords
|
||||
| n == pred minY = []
|
||||
| otherwise = p minX line : print2D (n - 1) coords
|
||||
where
|
||||
line = nub $ sort $ map (^. _x) (filter (\(V2 _ a) -> a == n) coords)
|
||||
minY = minimum $ map (^. _y) coords
|
||||
minX = minimum $ map (^. _x) coords
|
|
@ -0,0 +1,14 @@
|
|||
Sensor at x=2, y=18: closest beacon is at x=-2, y=15
|
||||
Sensor at x=9, y=16: closest beacon is at x=10, y=16
|
||||
Sensor at x=13, y=2: closest beacon is at x=15, y=3
|
||||
Sensor at x=12, y=14: closest beacon is at x=10, y=16
|
||||
Sensor at x=10, y=20: closest beacon is at x=10, y=16
|
||||
Sensor at x=14, y=17: closest beacon is at x=10, y=16
|
||||
Sensor at x=8, y=7: closest beacon is at x=2, y=10
|
||||
Sensor at x=2, y=0: closest beacon is at x=2, y=10
|
||||
Sensor at x=0, y=11: closest beacon is at x=2, y=10
|
||||
Sensor at x=20, y=14: closest beacon is at x=25, y=17
|
||||
Sensor at x=17, y=20: closest beacon is at x=21, y=22
|
||||
Sensor at x=16, y=7: closest beacon is at x=15, y=3
|
||||
Sensor at x=14, y=3: closest beacon is at x=15, y=3
|
||||
Sensor at x=20, y=1: closest beacon is at x=15, y=3
|
Loading…
Reference in New Issue
Block a user