From 08e5c738343a3baf910691772290c82e4d85d779 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 15 Dec 2022 19:11:33 +0000 Subject: [PATCH] Done day 15 --- advent-of-code22.cabal | 5 + advent15/Main.hs | 93 ++++++++++++++++++ data/advent15.txt | 27 ++++++ data/advent15a.txt | 14 +++ problems/day15.html | 212 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 351 insertions(+) create mode 100644 advent15/Main.hs create mode 100644 data/advent15.txt create mode 100644 data/advent15a.txt create mode 100644 problems/day15.html diff --git a/advent-of-code22.cabal b/advent-of-code22.cabal index 6859245..94190f5 100644 --- a/advent-of-code22.cabal +++ b/advent-of-code22.cabal @@ -170,3 +170,8 @@ executable advent14 import: common-extensions, build-directives main-is: advent14/Main.hs build-depends: text, attoparsec, containers, linear, lens + +executable advent15 + import: common-extensions, build-directives + main-is: advent15/Main.hs + build-depends: text, attoparsec, containers, linear, lens diff --git a/advent15/Main.hs b/advent15/Main.hs new file mode 100644 index 0000000..9ed5d86 --- /dev/null +++ b/advent15/Main.hs @@ -0,0 +1,93 @@ +-- Writeup at https://work.njae.me.uk/2022/12/15/advent-of-code-2022-day-15/ + +import AoC +import Data.Text (Text) +import qualified Data.Text.IO as TIO +import Data.Attoparsec.Text hiding (take, D) +import Control.Applicative +import Data.List +import Data.Ix +import qualified Data.Set as S +import Linear hiding (Trace, trace, distance) +import Control.Lens + +type Position = V2 Int + +data Sensor = Sensor Position Position -- sensor position, beacon position + deriving (Eq, Show) + +newtype Region = Region { getRegion :: Position -> Bool } + +instance Semigroup Region where + r1 <> r2 = Region (\p -> getRegion r1 p || getRegion r2 p) + +instance Monoid Region where + mempty = Region (\p -> False) + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- TIO.readFile dataFileName + let sensors = successfulParse text + -- print sensors + print $ part1 sensors + print $ part2 sensors + +thisY :: Int +-- thisY = 10 +thisY = 2000000 + +searchRange :: (Position, Position) +-- searchRange = ((V2 0 0), (V2 20 20)) +searchRange = ((V2 0 0), (V2 4000000 4000000)) + +part1, part2 :: [Sensor] -> Int +part1 sensors = length $ filter (\p -> p `notElem` occupied) $ filter (getRegion coverage) rowCoords + where coverage = mconcat $ fmap nearby sensors + rowCoords = range ((V2 (globalMinX sensors) thisY), (V2 (globalMaxX sensors) thisY)) + occupied = concatMap (\(Sensor s b) -> [s, b]) sensors + +part2 sensors = x * 4000000 + y + where coverage = mconcat $ fmap nearby sensors + boundaries = S.filter (inRange searchRange) $ S.unions $ fmap justOutside sensors + V2 x y = S.findMin $ S.filter (\p -> not $ getRegion coverage p) boundaries + +manhattan :: Position -> Position -> Int +manhattan p1 p2 = (abs dx) + (abs dy) + where V2 dx dy = p1 ^-^ p2 + +nearby :: Sensor -> Region +nearby (Sensor s b) = Region (\p -> manhattan s p <= dist) + where dist = manhattan s b + +minX, maxX :: Sensor -> Int +minX (Sensor s@(V2 sx _) b) = sx - (manhattan s b) +maxX (Sensor s@(V2 sx _) b) = sx + (manhattan s b) + +globalMinX, globalMaxX :: [Sensor] -> Int +globalMinX = minimum . fmap minX +globalMaxX = maximum . fmap maxX + +justOutside :: Sensor -> S.Set Position +justOutside (Sensor s@(V2 sx sy) b) = S.fromList (topLeft ++ topRight ++ bottomLeft ++ bottomRight) + where d = 1 + manhattan s b + topLeft = [V2 x y | (x, y) <- zip [(sx - d)..sx] [sy..(sy + d)] ] + topRight = [V2 x y | (x, y) <- zip [(sx + d), (sx + d - 1)..sx] [sy..(sy + d)] ] + bottomLeft = [V2 x y | (x, y) <- zip [(sx - d)..sx] [sy, (sy - 1)..(sy - d)] ] + bottomRight = [V2 x y | (x, y) <- zip [(sx + d), (sx + d - 1)..sx] [sy, (sy - 1)..(sy - d)] ] + +-- Parse the input file + +sensorsP :: Parser [Sensor] +sensorP :: Parser Sensor +positionP :: Parser Position + +sensorsP = sensorP `sepBy` endOfLine +sensorP = Sensor <$> ("Sensor at " *> positionP) <*> (": closest beacon is at " *> positionP) +positionP = V2 <$> (("x=" *> signed decimal) <* ", ") <*> ("y=" *> signed decimal) + +successfulParse :: Text -> [Sensor] +successfulParse input = + case parseOnly sensorsP input of + Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err + Right sensors -> sensors diff --git a/data/advent15.txt b/data/advent15.txt new file mode 100644 index 0000000..dc19ef2 --- /dev/null +++ b/data/advent15.txt @@ -0,0 +1,27 @@ +Sensor at x=1326566, y=3575946: closest beacon is at x=1374835, y=2000000 +Sensor at x=2681168, y=3951549: closest beacon is at x=3184941, y=3924923 +Sensor at x=3959984, y=1095746: closest beacon is at x=3621412, y=2239432 +Sensor at x=3150886, y=2479946: closest beacon is at x=3621412, y=2239432 +Sensor at x=3983027, y=2972336: closest beacon is at x=4012908, y=3083616 +Sensor at x=3371601, y=3853300: closest beacon is at x=3184941, y=3924923 +Sensor at x=3174612, y=3992719: closest beacon is at x=3184941, y=3924923 +Sensor at x=3316368, y=1503688: closest beacon is at x=3621412, y=2239432 +Sensor at x=3818181, y=2331216: closest beacon is at x=3621412, y=2239432 +Sensor at x=3960526, y=3229321: closest beacon is at x=4012908, y=3083616 +Sensor at x=61030, y=3045273: closest beacon is at x=-467419, y=2369316 +Sensor at x=3635583, y=3121524: closest beacon is at x=4012908, y=3083616 +Sensor at x=2813357, y=5535: closest beacon is at x=3595763, y=-77322 +Sensor at x=382745, y=1566522: closest beacon is at x=1374835, y=2000000 +Sensor at x=3585664, y=538632: closest beacon is at x=3595763, y=-77322 +Sensor at x=3979654, y=2158646: closest beacon is at x=3621412, y=2239432 +Sensor at x=3996588, y=2833167: closest beacon is at x=4012908, y=3083616 +Sensor at x=3249383, y=141800: closest beacon is at x=3595763, y=-77322 +Sensor at x=3847114, y=225529: closest beacon is at x=3595763, y=-77322 +Sensor at x=3668737, y=3720078: closest beacon is at x=3184941, y=3924923 +Sensor at x=1761961, y=680560: closest beacon is at x=1374835, y=2000000 +Sensor at x=2556636, y=2213691: closest beacon is at x=3621412, y=2239432 +Sensor at x=65365, y=215977: closest beacon is at x=346716, y=-573228 +Sensor at x=709928, y=2270200: closest beacon is at x=1374835, y=2000000 +Sensor at x=3673956, y=2670437: closest beacon is at x=4029651, y=2547743 +Sensor at x=3250958, y=3999227: closest beacon is at x=3184941, y=3924923 +Sensor at x=3009537, y=3292368: closest beacon is at x=3184941, y=3924923 \ No newline at end of file diff --git a/data/advent15a.txt b/data/advent15a.txt new file mode 100644 index 0000000..652e631 --- /dev/null +++ b/data/advent15a.txt @@ -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 \ No newline at end of file diff --git a/problems/day15.html b/problems/day15.html new file mode 100644 index 0000000..c0ac144 --- /dev/null +++ b/problems/day15.html @@ -0,0 +1,212 @@ + + + + +Day 15 - Advent of Code 2022 + + + + + + + + +

Advent of Code

Neil Smith (AoC++) 30*

   int y=2022;

+ + + +
+

--- Day 15: Beacon Exclusion Zone ---

You feel the ground rumble again as the distress signal leads you to a large network of subterranean tunnels. You don't have time to search them all, but you don't need to: your pack contains a set of deployable sensors that you imagine were originally built to locate lost Elves.

+

The sensors aren't very powerful, but that's okay; your handheld device indicates that you're close enough to the source of the distress signal to use them. You pull the emergency sensor system out of your pack, hit the big button on top, and the sensors zoom off down the tunnels.

+

Once a sensor finds a spot it thinks will give it a good reading, it attaches itself to a hard surface and begins monitoring for the nearest signal source beacon. Sensors and beacons always exist at integer coordinates. Each sensor knows its own position and can determine the position of a beacon precisely; however, sensors can only lock on to the one beacon closest to the sensor as measured by the Manhattan distance. (There is never a tie where two beacons are the same distance to a sensor.)

+

It doesn't take long for the sensors to report back their positions and closest beacons (your puzzle input). For example:

+
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
+
+

So, consider the sensor at 2,18; the closest beacon to it is at -2,15. For the sensor at 9,16, the closest beacon to it is at 10,16.

+

Drawing sensors as S and beacons as B, the above arrangement of sensors and beacons looks like this:

+
               1    1    2    2
+     0    5    0    5    0    5
+ 0 ....S.......................
+ 1 ......................S.....
+ 2 ...............S............
+ 3 ................SB..........
+ 4 ............................
+ 5 ............................
+ 6 ............................
+ 7 ..........S.......S.........
+ 8 ............................
+ 9 ............................
+10 ....B.......................
+11 ..S.........................
+12 ............................
+13 ............................
+14 ..............S.......S.....
+15 B...........................
+16 ...........SB...............
+17 ................S..........B
+18 ....S.......................
+19 ............................
+20 ............S......S........
+21 ............................
+22 .......................B....
+
+

This isn't necessarily a comprehensive map of all beacons in the area, though. Because each sensor only identifies its closest beacon, if a sensor detects a beacon, you know there are no other beacons that close or closer to that sensor. There could still be beacons that just happen to not be the closest beacon to any sensor. Consider the sensor at 8,7:

+
               1    1    2    2
+     0    5    0    5    0    5
+-2 ..........#.................
+-1 .........###................
+ 0 ....S...#####...............
+ 1 .......#######........S.....
+ 2 ......#########S............
+ 3 .....###########SB..........
+ 4 ....#############...........
+ 5 ...###############..........
+ 6 ..#################.........
+ 7 .#########S#######S#........
+ 8 ..#################.........
+ 9 ...###############..........
+10 ....B############...........
+11 ..S..###########............
+12 ......#########.............
+13 .......#######..............
+14 ........#####.S.......S.....
+15 B........###................
+16 ..........#SB...............
+17 ................S..........B
+18 ....S.......................
+19 ............................
+20 ............S......S........
+21 ............................
+22 .......................B....
+
+

This sensor's closest beacon is at 2,10, and so you know there are no beacons that close or closer (in any positions marked #).

+

None of the detected beacons seem to be producing the distress signal, so you'll need to work out where the distress beacon is by working out where it isn't. For now, keep things simple by counting the positions where a beacon cannot possibly be along just a single row.

+

So, suppose you have an arrangement of beacons and sensors like in the example above and, just in the row where y=10, you'd like to count the number of positions a beacon cannot possibly exist. The coverage from all sensors near that row looks like this:

+
                 1    1    2    2
+       0    5    0    5    0    5
+ 9 ...#########################...
+10 ..####B######################..
+11 .###S#############.###########.
+
+

In this example, in the row where y=10, there are 26 positions where a beacon cannot be present.

+

Consult the report from the sensors you just deployed. In the row where y=2000000, how many positions cannot contain a beacon?

+
+

Your puzzle answer was 5147333.

--- Part Two ---

Your handheld device indicates that the distress signal is coming from a beacon nearby. The distress beacon is not detected by any sensor, but the distress beacon must have x and y coordinates each no lower than 0 and no larger than 4000000.

+

To isolate the distress beacon's signal, you need to determine its tuning frequency, which can be found by multiplying its x coordinate by 4000000 and then adding its y coordinate.

+

In the example above, the search space is smaller: instead, the x and y coordinates can each be at most 20. With this reduced search area, there is only a single position that could have a beacon: x=14, y=11. The tuning frequency for this distress beacon is 56000011.

+

Find the only possible position for the distress beacon. What is its tuning frequency?

+
+

Your puzzle answer was 13734006908372.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file -- 2.34.1