From 1d3b8dcdc19ee713ed131d0e9970cdad3d1cdffc Mon Sep 17 00:00:00 2001 From: Quinten Kock Date: Thu, 15 Dec 2022 06:31:28 +0100 Subject: [PATCH] day 15 (again, slow) --- 2022/day15.hs | 39 +++++++++++++++++++++++++++++++++++++++ 2022/inputs/day15 | 26 ++++++++++++++++++++++++++ 2022/inputs/day15.test | 14 ++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 2022/day15.hs create mode 100644 2022/inputs/day15 create mode 100644 2022/inputs/day15.test diff --git a/2022/day15.hs b/2022/day15.hs new file mode 100644 index 0000000..aeaae9e --- /dev/null +++ b/2022/day15.hs @@ -0,0 +1,39 @@ +import Data.List.Split (splitOn) +import qualified Data.Set as S + +type Coord = (Int, Int) + +globalY :: Int +globalY = 2000000 + +parseCoord :: String -> Coord +parseCoord xs = (read $ init x, read y) where + [x,y] = map (drop 2) $ splitOn " " xs + +parseLine :: String -> (Coord, Coord) +parseLine xs = (sensor, beacon) where + [sensor, beacon] = map (parseCoord . dropWhile (/='x'))$ splitOn ":" xs + +manhattan :: Coord -> Coord -> Int +manhattan (a,b) (x,y) = abs (a-x) + abs (b-y) + +allWithinRange :: Coord -> Coord -> S.Set Coord +allWithinRange sensor@(a,b) beacon@(x,y) = S.fromList $ filter (\c -> manhattan sensor c <= dist) all where + minx = a - dist + maxx = a + dist + miny = b - dist + maxy = b + dist + dist = manhattan sensor beacon + all = [(x,y) | x <- [minx..maxx], y <- [globalY]] + +main :: IO () +main = do + x <- map parseLine . lines <$> getContents + + let beacons = S.fromList $ map snd x + let reachable = foldr1 S.union $ map (uncurry allWithinRange) x + + let ys = S.size $ S.filter (\x -> snd x == globalY) (reachable S.\\ beacons) + + putStr "part 1: " + print ys \ No newline at end of file diff --git a/2022/inputs/day15 b/2022/inputs/day15 new file mode 100644 index 0000000..7cdce8a --- /dev/null +++ b/2022/inputs/day15 @@ -0,0 +1,26 @@ +Sensor at x=2483411, y=3902983: closest beacon is at x=2289579, y=3633785 +Sensor at x=3429446, y=303715: closest beacon is at x=2876111, y=-261280 +Sensor at x=666423, y=3063763: closest beacon is at x=2264411, y=2779977 +Sensor at x=3021606, y=145606: closest beacon is at x=2876111, y=-261280 +Sensor at x=2707326, y=2596893: closest beacon is at x=2264411, y=2779977 +Sensor at x=3103704, y=1560342: closest beacon is at x=2551409, y=2000000 +Sensor at x=3497040, y=3018067: closest beacon is at x=3565168, y=2949938 +Sensor at x=1708530, y=855013: closest beacon is at x=2551409, y=2000000 +Sensor at x=3107437, y=3263465: closest beacon is at x=3404814, y=3120160 +Sensor at x=2155249, y=2476196: closest beacon is at x=2264411, y=2779977 +Sensor at x=3447897, y=3070850: closest beacon is at x=3404814, y=3120160 +Sensor at x=2643048, y=3390796: closest beacon is at x=2289579, y=3633785 +Sensor at x=3533132, y=3679388: closest beacon is at x=3404814, y=3120160 +Sensor at x=3683790, y=3017900: closest beacon is at x=3565168, y=2949938 +Sensor at x=1943208, y=3830506: closest beacon is at x=2289579, y=3633785 +Sensor at x=3940100, y=3979653: closest beacon is at x=2846628, y=4143786 +Sensor at x=3789719, y=1225738: closest beacon is at x=4072555, y=1179859 +Sensor at x=3939775, y=578381: closest beacon is at x=4072555, y=1179859 +Sensor at x=3880152, y=3327397: closest beacon is at x=3404814, y=3120160 +Sensor at x=3280639, y=2446475: closest beacon is at x=3565168, y=2949938 +Sensor at x=2348869, y=2240374: closest beacon is at x=2551409, y=2000000 +Sensor at x=3727441, y=2797456: closest beacon is at x=3565168, y=2949938 +Sensor at x=3973153, y=2034945: closest beacon is at x=4072555, y=1179859 +Sensor at x=38670, y=785556: closest beacon is at x=311084, y=-402911 +Sensor at x=3181909, y=2862960: closest beacon is at x=3565168, y=2949938 +Sensor at x=3099490, y=3946226: closest beacon is at x=2846628, y=4143786 diff --git a/2022/inputs/day15.test b/2022/inputs/day15.test new file mode 100644 index 0000000..a612424 --- /dev/null +++ b/2022/inputs/day15.test @@ -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