From 864aa7e1075c6b7c75037dc4b11afe4a30d73618 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 6 Dec 2018 09:25:16 +0000 Subject: [PATCH] Day 6 --- advent-of-code.cabal | 9 ++ data/advent06.txt | 50 +++++++++++ problems/day06.html | 186 +++++++++++++++++++++++++++++++++++++++ src/advent06/advent06.hs | 119 +++++++++++++++++++++++++ 4 files changed, 364 insertions(+) create mode 100644 data/advent06.txt create mode 100644 problems/day06.html create mode 100644 src/advent06/advent06.hs diff --git a/advent-of-code.cabal b/advent-of-code.cabal index 9196505..f4fb19a 100644 --- a/advent-of-code.cabal +++ b/advent-of-code.cabal @@ -58,3 +58,12 @@ executable advent05 main-is: advent05.hs default-language: Haskell2010 build-depends: base >= 4.7 && < 5 + +executable advent06 + hs-source-dirs: src/advent06 + main-is: advent06.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , text + , megaparsec + , containers \ No newline at end of file diff --git a/data/advent06.txt b/data/advent06.txt new file mode 100644 index 0000000..bb0948a --- /dev/null +++ b/data/advent06.txt @@ -0,0 +1,50 @@ +77, 279 +216, 187 +72, 301 +183, 82 +57, 170 +46, 335 +55, 89 +71, 114 +313, 358 +82, 88 +78, 136 +339, 314 +156, 281 +260, 288 +125, 249 +150, 130 +210, 271 +190, 258 +73, 287 +187, 332 +283, 353 +66, 158 +108, 97 +237, 278 +243, 160 +61, 52 +353, 107 +260, 184 +234, 321 +181, 270 +104, 84 +290, 109 +193, 342 +43, 294 +134, 211 +50, 129 +92, 112 +309, 130 +291, 170 +89, 204 +186, 177 +286, 302 +188, 145 +40, 52 +254, 292 +270, 287 +238, 216 +299, 184 +141, 264 +117, 129 diff --git a/problems/day06.html b/problems/day06.html new file mode 100644 index 0000000..390a26a --- /dev/null +++ b/problems/day06.html @@ -0,0 +1,186 @@ + + + + +Day 6 - Advent of Code 2018 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 12*

   <y>2018</y>

+ + + +
+

--- Day 6: Chronal Coordinates ---

The device on your wrist beeps several times, and once again you feel like you're falling.

+

"Situation critical," the device announces. "Destination indeterminate. Chronal interference detected. Please specify new target coordinates."

+

The device then produces a list of coordinates (your puzzle input). Are they places it thinks are safe or dangerous? It recommends you check manual page 729. The Elves did not give you a manual.

+

If they're dangerous, maybe you can minimize the danger by finding the coordinate that gives the largest distance from the other points.

+

Using only the Manhattan distance, determine the area around each coordinate by counting the number of integer X,Y locations that are closest to that coordinate (and aren't tied in distance to any other coordinate).

+

Your goal is to find the size of the largest area that isn't infinite. For example, consider the following list of coordinates:

+
1, 1
+1, 6
+8, 3
+3, 4
+5, 5
+8, 9
+
+

If we name these coordinates A through F, we can draw them on a grid, putting 0,0 at the top left:

+
..........
+.A........
+..........
+........C.
+...D......
+.....E....
+.B........
+..........
+..........
+........F.
+
+

This view is partial - the actual grid extends infinitely in all directions. Using the Manhattan distance, each location's closest coordinate can be determined, shown here in lowercase:

+
aaaaa.cccc
+aAaaa.cccc
+aaaddecccc
+aadddeccCc
+..dDdeeccc
+bb.deEeecc
+bBb.eeee..
+bbb.eeefff
+bbb.eeffff
+bbb.ffffFf
+
+

Locations shown as . are equally far from two or more coordinates, and so they don't count as being closest to any.

+

In this example, the areas of coordinates A, B, C, and F are infinite - while not shown here, their areas extend forever outside the visible grid. However, the areas of coordinates D and E are finite: D is closest to 9 locations, and E is closest to 17 (both including the coordinate's location itself). Therefore, in this example, the size of the largest area is 17.

+

What is the size of the largest area that isn't infinite?

+
+

Your puzzle answer was 3006.

--- Part Two ---

On the other hand, if the coordinates are safe, maybe the best you can do is try to find a region near as many coordinates as possible.

+

For example, suppose you want the sum of the Manhattan distance to all of the coordinates to be less than 32. For each location, add up the distances to all of the given coordinates; if the total of those distances is less than 32, that location is within the desired region. Using the same coordinates as above, the resulting region looks like this:

+
..........
+.A........
+..........
+...###..C.
+..#D###...
+..###E#...
+.B.###....
+..........
+..........
+........F.
+
+

In particular, consider the highlighted location 4,3 located at the top middle of the region. Its calculation is as follows, where abs() is the absolute value function:

+
    +
  • Distance to coordinate A: abs(4-1) + abs(3-1) =  5
  • +
  • Distance to coordinate B: abs(4-1) + abs(3-6) =  6
  • +
  • Distance to coordinate C: abs(4-8) + abs(3-3) =  4
  • +
  • Distance to coordinate D: abs(4-3) + abs(3-4) =  2
  • +
  • Distance to coordinate E: abs(4-5) + abs(3-5) =  3
  • +
  • Distance to coordinate F: abs(4-8) + abs(3-9) = 10
  • +
  • Total distance: 5 + 6 + 4 + 2 + 3 + 10 = 30
  • +
+

Because the total distance to all coordinates (30) is less than 32, the location is within the region.

+

This region, which also includes coordinates D and E, has a total size of 16.

+

Your actual region will need to be much larger than this example, though, instead including all locations with a total distance of less than 10000.

+

What is the size of the region containing all locations which have a total distance to all given coordinates of less than 10000?

+
+

Your puzzle answer was 42998.

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 diff --git a/src/advent06/advent06.hs b/src/advent06/advent06.hs new file mode 100644 index 0000000..2ff18f5 --- /dev/null +++ b/src/advent06/advent06.hs @@ -0,0 +1,119 @@ +{-# LANGUAGE OverloadedStrings #-} + +import Data.List + +import Data.Text (Text) +import qualified Data.Text.IO as TIO + +import Data.Void (Void) + +import Text.Megaparsec +import Text.Megaparsec.Char +import qualified Text.Megaparsec.Char.Lexer as L +import qualified Control.Applicative as CA + +import qualified Data.Map.Strict as M + +type Coord = (Integer, Integer) -- x, y +type Bounds = (Integer, Integer, Integer, Integer) +type Region = M.Map Coord Int + +main :: IO () +main = do + text <- TIO.readFile "data/advent06.txt" + let coords = successfulParse text + let boundingBox = findBounds coords + print $ length coords + print boundingBox + print $ part1 coords boundingBox + print $ part2 coords boundingBox + -- print $ part1 activity + -- print $ part2 activity + + +part1 coords bounds = largestRegion $ regionSizes $ finite edgeLabels regions + where regions = findRegions coords bounds + edgeLabels = infinite regions bounds + +part2 coords bounds = M.size $ M.filter (< 10000) $ safeCells coords bounds + +findRegions :: [Coord] -> Bounds -> Region +findRegions coords (minX, maxX, minY, maxY) = M.fromList labelledCells + where cells = [(x, y) | x <- [minX .. maxX], y <- [minY .. maxY] ] + starts = zip [1..] coords + labelledCells = map (\c -> (c, nearestStart 0 c starts)) cells + +nearestStart :: Int -> Coord -> [(Int, Coord)] -> Int +nearestStart tieLabel cell starts = nearestLabel + where distances = sort $ map (\(l, s) -> (distance s cell , l)) starts + nearestLabel = if fst (distances!!0) == fst (distances!!1) + then tieLabel + else snd (distances!!0) + + +safeCells :: [Coord] -> Bounds -> Region +safeCells coords (minX, maxX, minY, maxY) = M.fromList distanceCells + where cells = [(x, y) | x <- [minX .. maxX], y <- [minY .. maxY] ] + distanceCells = map (\c -> (c, fromIntegral $ sumDistance c coords) ) cells + + +sumDistance :: Coord -> [Coord] -> Integer +sumDistance here others = sum $ map (\c -> distance here c) others + + +infinite :: Region -> Bounds -> [Int] +infinite regions (minX, maxX, minY, maxY) = nub $ sort $ M.elems $ M.filterWithKey onEdge regions + where onEdge (x, y) _ = (x == minX) || (x == maxX) || (y == minY) || (y == maxY) + +finite :: [Int] -> Region -> Region +finite excluded regions = M.filter (\r -> r `notElem` excludedTied) regions + where excludedTied = (0:excluded) + + +regionSizes :: Region -> [(Int, Int)] +regionSizes regions = map (\g -> (g!!0, length g)) $ group $ sort $ M.elems regions + + +largestRegion :: [(Int, Int)] -> Int +largestRegion = maximum . map snd + + +findBounds :: [Coord] -> (Integer, Integer, Integer, Integer) +findBounds coords = ( minX - (maxY - minY) -- small x edge + , maxX + (maxY - minY) -- large x edge + , minY - (maxX - minX) -- small x edge + , maxY + (maxX - minX) -- large y edge + ) + where maxX = maximum $ map fst coords + minX = minimum $ map fst coords + maxY = maximum $ map snd coords + minY = minimum $ map snd coords + +-- Manhattan distance +distance :: Coord -> Coord -> Integer +distance (x1, y1) (x2, y2) = (abs (x1 - x2)) + (abs (y1 - y2)) + + +-- Parse the input file + +type Parser = Parsec Void Text + +sc :: Parser () +sc = L.space (skipSome spaceChar) CA.empty CA.empty +-- sc = L.space (skipSome (char ' ')) CA.empty CA.empty + +lexeme = L.lexeme sc +integer = lexeme L.decimal +symb = L.symbol sc + +commaP = symb "," + + +coordFileP = many coordP +coordP = (,) <$> integer <* commaP <*> integer + +successfulParse :: Text -> [Coord] +successfulParse input = + case parse coordFileP "input" input of + Left _error -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err + Right coords -> coords \ No newline at end of file -- 2.34.1