From: Neil Smith Date: Wed, 11 Dec 2019 17:48:15 +0000 (+0000) Subject: Using just rationals for part 1. X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=7a21eab3c81fc8d8214c80b0c447b96b9d7c016b;p=advent-of-code-19.git Using just rationals for part 1. --- diff --git a/advent10/package.yaml b/advent10/package.yaml index 75a923b..9069b19 100644 --- a/advent10/package.yaml +++ b/advent10/package.yaml @@ -56,4 +56,12 @@ executables: dependencies: - base >= 2 && < 6 - containers + - linear + + advent10i: + main: advent10i.hs + source-dirs: src + dependencies: + - base >= 2 && < 6 + - containers - linear \ No newline at end of file diff --git a/advent10/src/advent10.hs b/advent10/src/advent10.hs index 95c0287..ba90ba1 100644 --- a/advent10/src/advent10.hs +++ b/advent10/src/advent10.hs @@ -1,17 +1,14 @@ import Data.Ratio import qualified Data.Set as S import qualified Data.Map.Strict as M --- import Data.Map.Strict ((!)) -import Linear (V2(..), (^+^), (^-^), (*^), (*^)) +import Linear (V2(..), (^+^), (*^)) import Linear.Metric (norm) import Data.List import Data.Ord - -type Bounds = (Int, Int) -type Position = V2 Int -type Delta = V2 (Ratio Int) +type Bounds = (Integer, Integer) +type Position = V2 Rational type Asteroids = S.Set Position @@ -30,12 +27,14 @@ main = do print $ part2 targets -part2 targets = 100 * x + y +part2 targets = numerator $ 100 * x + y where V2 x y = (targetSequence targets)!!199 bestVisible :: Bounds -> Asteroids -> (Position, Int) -bestVisible bounds asteroids = maximumBy (comparing snd) $ S.toList $ S.map (visibleCount bounds asteroids) asteroids +bestVisible bounds asteroids = maximumBy (comparing snd) + $ S.toList + $ S.map (visibleCount bounds asteroids) asteroids visibleCount :: Bounds -> Asteroids -> Position -> (Position, Int) visibleCount bounds asteroids origin = (origin, S.size $ visible bounds origin asteroids) @@ -53,22 +52,13 @@ screenings bounds origin@(V2 ox oy) screened0 target@(V2 tx ty) | origin == target = screened0 | otherwise = S.union screened0 screened where maxComponent = max (abs (tx - ox)) (abs (ty - oy)) - delta = V2 ((tx - ox) % maxComponent) ((ty - oy) % maxComponent) - startR = V2 (tx % 1) (ty % 1) - rawScreens = takeWhile (inBounds bounds) [startR ^+^ n *^ delta | n <- [1..]] - screens = filter isIntegral rawScreens - screenInteger = map integerVec screens - fullScreened = S.fromList screenInteger - screened = S.delete target fullScreened - -inBounds :: Bounds -> Delta -> Bool -inBounds (maxX, maxY) (V2 x y) = (x >= 0) && (x <= (maxX % 1)) && (y >= 0) && (y <= (maxY % 1)) + delta = V2 ((tx - ox) / maxComponent) ((ty - oy) / maxComponent) + rawScreens = takeWhile (inBounds bounds) [target ^+^ n *^ delta | n <- [1..]] + screened = S.fromList rawScreens -integerVec :: Delta -> Position -integerVec (V2 x y) = V2 (numerator x) (numerator y) +inBounds :: Bounds -> Position -> Bool +inBounds (maxX, maxY) (V2 x y) = (x >= 0) && (x <= (maxX % 1)) && (y >= 0) && (y <= (maxY % 1)) -isIntegral :: Delta -> Bool -isIntegral (V2 x y) = (denominator x == 1) && (denominator y == 1) makeTargets :: Position -> Asteroids -> Targets @@ -78,16 +68,12 @@ makeTargets origin asteroids = S.foldl' addTarget M.empty asteroids targetInfo :: Position -> Position -> TargetInfo targetInfo origin target = (angle, range) where V2 dx dy = target - origin - angle = atan2 (fromIntegral dy) (fromIntegral dx) + angle = atan2 (fromRational dy) (fromRational dx) -- recipRange = 1 / (norm (V2 (fromIntegral dy) (fromIntegral dx))) - range = norm (V2 (fromIntegral dy) (fromIntegral dx)) + range = norm (V2 (fromRational dy) (fromRational dx)) -possibleTargets :: Float -> Targets -> Targets -possibleTargets angle targets = M.filterWithKey (\(a, _) _ -> a > angle) targets - -firstTarget :: Targets -> (TargetInfo, Position) -firstTarget targets = M.findMin targets +targetSequence :: Targets -> [Position] targetSequence targets = targetNext ((- pi / 2) - 0.001) targets targetNext :: Float -> Targets -> [Position] @@ -100,12 +86,18 @@ targetNext angle targets targets' = M.delete (targetAngle, targetRange) targets angle' = targetAngle +possibleTargets :: Float -> Targets -> Targets +possibleTargets angle targets = M.filterWithKey (\(a, _) _ -> a > angle) targets + +firstTarget :: Targets -> (TargetInfo, Position) +firstTarget targets = M.findMin targets + successfulParse :: String -> (Asteroids, Bounds) -successfulParse input = ( S.fromList [(V2 x y) | x <- [0..maxX], y <- [0..maxY] +successfulParse input = ( S.fromList [(V2 (fromIntegral x % 1) (fromIntegral y % 1)) | x <- [0..maxX], y <- [0..maxY] , isAsteroid x y ] - , (maxX, maxY) + , (fromIntegral maxX, fromIntegral maxY) ) where grid = lines input maxX = (length $ head grid) - 1 diff --git a/advent10/src/advent10i.hs b/advent10/src/advent10i.hs new file mode 100644 index 0000000..78a68af --- /dev/null +++ b/advent10/src/advent10i.hs @@ -0,0 +1,121 @@ +import Data.Ratio +import qualified Data.Set as S +import qualified Data.Map.Strict as M +-- import Data.Map.Strict ((!)) +-- import Linear (V2(..), (^+^), (^-^), (*^), (*^)) +import Linear (V2(..), (^+^), (*^)) +import Linear.Metric (norm) + +import Data.List +import Data.Ord + + +type Bounds = (Int, Int) +type Position = V2 Int +type Delta = V2 (Ratio Int) + +type Asteroids = S.Set Position + +type TargetInfo = (Float, Float) +type Targets = M.Map TargetInfo Position + +main :: IO () +main = do + text <- readFile "data/advent10.txt" + let (asteroids, bounds) = successfulParse text + -- print asteroids + let (monitor, visCount) = bestVisible bounds asteroids + print visCount -- part 1 + let targets = makeTargets monitor (S.delete monitor asteroids) + -- print targets + print $ part2 targets + + +part2 targets = 100 * x + y + where V2 x y = (targetSequence targets)!!199 + + +bestVisible :: Bounds -> Asteroids -> (Position, Int) +bestVisible bounds asteroids = maximumBy (comparing snd) $ S.toList $ S.map (visibleCount bounds asteroids) asteroids + +visibleCount :: Bounds -> Asteroids -> Position -> (Position, Int) +visibleCount bounds asteroids origin = (origin, S.size $ visible bounds origin asteroids) + +visible :: Bounds -> Position -> Asteroids -> Asteroids +visible bounds origin asteroids = S.delete origin $ S.difference asteroids screened + where screened = allScreenings bounds origin asteroids + +allScreenings :: Bounds -> Position -> Asteroids -> Asteroids +allScreenings bounds origin asteroids = S.foldl' (screenings bounds origin) S.empty asteroids + + +screenings :: Bounds -> Position -> Asteroids -> Position -> Asteroids +screenings bounds origin@(V2 ox oy) screened0 target@(V2 tx ty) + | origin == target = screened0 + | otherwise = S.union screened0 screened + where maxComponent = max (abs (tx - ox)) (abs (ty - oy)) + delta = V2 ((tx - ox) % maxComponent) ((ty - oy) % maxComponent) + startR = V2 (tx % 1) (ty % 1) + rawScreens = takeWhile (inBounds bounds) [startR ^+^ n *^ delta | n <- [1..]] + screens = filter isIntegral rawScreens + screenInteger = map integerVec screens + fullScreened = S.fromList screenInteger + screened = S.delete target fullScreened + +inBounds :: Bounds -> Delta -> Bool +inBounds (maxX, maxY) (V2 x y) = (x >= 0) && (x <= (maxX % 1)) && (y >= 0) && (y <= (maxY % 1)) + +integerVec :: Delta -> Position +integerVec (V2 x y) = V2 (numerator x) (numerator y) + +isIntegral :: Delta -> Bool +isIntegral (V2 x y) = (denominator x == 1) && (denominator y == 1) + + +makeTargets :: Position -> Asteroids -> Targets +makeTargets origin asteroids = S.foldl' addTarget M.empty asteroids + where addTarget m t = M.insert (targetInfo origin t) t m + +targetInfo :: Position -> Position -> TargetInfo +targetInfo origin target = (angle, range) + where V2 dx dy = target - origin + angle = atan2 (fromIntegral dy) (fromIntegral dx) + -- recipRange = 1 / (norm (V2 (fromIntegral dy) (fromIntegral dx))) + range = norm (V2 (fromIntegral dy) (fromIntegral dx)) + +possibleTargets :: Float -> Targets -> Targets +possibleTargets angle targets = M.filterWithKey (\(a, _) _ -> a > angle) targets + +firstTarget :: Targets -> (TargetInfo, Position) +firstTarget targets = M.findMin targets + +targetSequence targets = targetNext ((- pi / 2) - 0.001) targets + +targetNext :: Float -> Targets -> [Position] +targetNext angle targets + | M.null targets = [] + | M.null possibles = targetNext (- pi) targets + | otherwise = (target:(targetNext angle' targets')) + where possibles = possibleTargets angle targets + ((targetAngle, targetRange), target) = firstTarget possibles + targets' = M.delete (targetAngle, targetRange) targets + angle' = targetAngle + + +successfulParse :: String -> (Asteroids, Bounds) +successfulParse input = ( S.fromList [(V2 x y) | x <- [0..maxX], y <- [0..maxY] + , isAsteroid x y + ] + , (maxX, maxY) + ) + where grid = lines input + maxX = (length $ head grid) - 1 + maxY = (length grid) - 1 + isAsteroid x y = (grid!!y)!!x == '#' + + +showPattern (maxX, maxY) asteroids = unlines rows + where rows = [[cell x y | x <- [0..maxX]] | y <- [0..maxY] ] + cell x y = if S.member (V2 x y) asteroids then '#' else '.' + + \ No newline at end of file diff --git a/problems/day09.html b/problems/day09.html new file mode 100644 index 0000000..4895749 --- /dev/null +++ b/problems/day09.html @@ -0,0 +1,151 @@ + + + + +Day 9 - Advent of Code 2019 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 20*

   var y=2019;

+ + + +
+ +

--- Day 9: Sensor Boost ---

You've just said goodbye to the rebooted rover and left Mars when you receive a faint distress signal coming from the asteroid belt. It must be the Ceres monitoring station!

+

In order to lock on to the signal, you'll need to boost your sensors. The Elves send up the latest BOOST program - Basic Operation Of System Test.

+

While BOOST (your puzzle input) is capable of boosting your sensors, for tenuous safety reasons, it refuses to do so until the computer it runs on passes some checks to demonstrate it is a complete Intcode computer.

+

Your existing Intcode computer is missing one key feature: it needs support for parameters in relative mode.

+

Parameters in mode 2, relative mode, behave very similarly to parameters in position mode: the parameter is interpreted as a position. Like position mode, parameters in relative mode can be read from or written to.

+

The important difference is that relative mode parameters don't count from address 0. Instead, they count from a value called the relative base. The relative base starts at 0.

+

The address a relative mode parameter refers to is itself plus the current relative base. When the relative base is 0, relative mode parameters and position mode parameters with the same value refer to the same address.

+

For example, given a relative base of 50, a relative mode parameter of -7 refers to memory address 50 + -7 = 43.

+

The relative base is modified with the relative base offset instruction:

+
    +
  • Opcode 9 adjusts the relative base by the value of its only parameter. The relative base increases (or decreases, if the value is negative) by the value of the parameter.
  • +
+

For example, if the relative base is 2000, then after the instruction 109,19, the relative base would be 2019. If the next instruction were 204,-34, then the value at address 1985 would be output.

+

Your Intcode computer will also need a few other capabilities:

+
    +
  • The computer's available memory should be much larger than the initial program. Memory beyond the initial program starts with the value 0 and can be read or written like any other memory. (It is invalid to try to access memory at a negative address, though.)
  • +
  • The computer should have support for large numbers. Some instructions near the beginning of the BOOST program will verify this capability.
  • +
+

Here are some example programs that use these features:

+
    +
  • 109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99 takes no input and produces a copy of itself as output.
  • +
  • 1102,34915192,34915192,7,4,7,99,0 should output a 16-digit number.
  • +
  • 104,1125899906842624,99 should output the large number in the middle.
  • +
+

The BOOST program will ask for a single input; run it in test mode by providing it the value 1. It will perform a series of checks on each opcode, output any opcodes (and the associated parameter modes) that seem to be functioning incorrectly, and finally output a BOOST keycode.

+

Once your Intcode computer is fully functional, the BOOST program should report no malfunctioning opcodes when run in test mode; it should only output a single value, the BOOST keycode. What BOOST keycode does it produce?

+
+

Your puzzle answer was 3340912345.

--- Part Two ---

You now have a complete Intcode computer.

+

Finally, you can lock on to the Ceres distress signal! You just need to boost your sensors using the BOOST program.

+

The program runs in sensor boost mode by providing the input instruction the value 2. Once run, it will boost the sensors automatically, but it might take a few seconds to complete the operation on slower hardware. In sensor boost mode, the program will output a single value: the coordinates of the distress signal.

+

Run the BOOST program in sensor boost mode. What are the coordinates of the distress signal?

+
+

Your puzzle answer was 51754.

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/problems/day10.html b/problems/day10.html new file mode 100644 index 0000000..93130dd --- /dev/null +++ b/problems/day10.html @@ -0,0 +1,267 @@ + + + + +Day 10 - Advent of Code 2019 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 20*

   0x0000|2019

+ + + +
+ +

--- Day 10: Monitoring Station ---

You fly into the asteroid belt and reach the Ceres monitoring station. The Elves here have an emergency: they're having trouble tracking all of the asteroids and can't be sure they're safe.

+

The Elves would like to build a new monitoring station in a nearby area of space; they hand you a map of all of the asteroids in that region (your puzzle input).

+

The map indicates whether each position is empty (.) or contains an asteroid (#). The asteroids are much smaller than they appear on the map, and every asteroid is exactly in the center of its marked position. The asteroids can be described with X,Y coordinates where X is the distance from the left edge and Y is the distance from the top edge (so the top-left corner is 0,0 and the position immediately to its right is 1,0).

+

Your job is to figure out which asteroid would be the best place to build a new monitoring station. A monitoring station can detect any asteroid to which it has direct line of sight - that is, there cannot be another asteroid exactly between them. This line of sight can be at any angle, not just lines aligned to the grid or diagonally. The best location is the asteroid that can detect the largest number of other asteroids.

+

For example, consider the following map:

+
.#..#
+.....
+#####
+....#
+...##
+
+

The best location for a new monitoring station on this map is the highlighted asteroid at 3,4 because it can detect 8 asteroids, more than any other location. (The only asteroid it cannot detect is the one at 1,0; its view of this asteroid is blocked by the asteroid at 2,2.) All other asteroids are worse locations; they can detect 7 or fewer other asteroids. Here is the number of other asteroids a monitoring station on each asteroid could detect:

+
.7..7
+.....
+67775
+....7
+...87
+
+

Here is an asteroid (#) and some examples of the ways its line of sight might be blocked. If there were another asteroid at the location of a capital letter, the locations marked with the corresponding lowercase letter would be blocked and could not be detected:

+
#.........
+...A......
+...B..a...
+.EDCG....a
+..F.c.b...
+.....c....
+..efd.c.gb
+.......c..
+....f...c.
+...e..d..c
+
+

Here are some larger examples:

+
    +
  • Best is 5,8 with 33 other asteroids detected:

    +
    ......#.#.
    +#..#.#....
    +..#######.
    +.#.#.###..
    +.#..#.....
    +..#....#.#
    +#..#....#.
    +.##.#..###
    +##...#..#.
    +.#....####
    +
  • +
  • Best is 1,2 with 35 other asteroids detected:

    +
    #.#...#.#.
    +.###....#.
    +.#....#...
    +##.#.#.#.#
    +....#.#.#.
    +.##..###.#
    +..#...##..
    +..##....##
    +......#...
    +.####.###.
    +
  • +
  • Best is 6,3 with 41 other asteroids detected:

    +
    .#..#..###
    +####.###.#
    +....###.#.
    +..###.##.#
    +##.##.#.#.
    +....###..#
    +..#.#..#.#
    +#..#.#.###
    +.##...##.#
    +.....#.#..
    +
  • +
  • Best is 11,13 with 210 other asteroids detected:

    +
    .#..##.###...#######
    +##.############..##.
    +.#.######.########.#
    +.###.#######.####.#.
    +#####.##.#.##.###.##
    +..#####..#.#########
    +####################
    +#.####....###.#.#.##
    +##.#################
    +#####.##.###..####..
    +..######..##.#######
    +####.##.####...##..#
    +.#####..#.######.###
    +##...#.##########...
    +#.##########.#######
    +.####.#.###.###.#.##
    +....##.##.###..#####
    +.#.#.###########.###
    +#.#.#.#####.####.###
    +###.##.####.##.#..##
    +
  • +
+

Find the best location for a new monitoring station. How many other asteroids can be detected from that location?

+
+

Your puzzle answer was 221.

--- Part Two ---

Once you give them the coordinates, the Elves quickly deploy an Instant Monitoring Station to the location and discover the worst: there are simply too many asteroids.

+

The only solution is complete vaporization by giant laser.

+

Fortunately, in addition to an asteroid scanner, the new monitoring station also comes equipped with a giant rotating laser perfect for vaporizing asteroids. The laser starts by pointing up and always rotates clockwise, vaporizing any asteroid it hits.

+

If multiple asteroids are exactly in line with the station, the laser only has enough power to vaporize one of them before continuing its rotation. In other words, the same asteroids that can be detected can be vaporized, but if vaporizing one asteroid makes another one detectable, the newly-detected asteroid won't be vaporized until the laser has returned to the same position by rotating a full 360 degrees.

+

For example, consider the following map, where the asteroid with the new monitoring station (and laser) is marked X:

+
.#....#####...#..
+##...##.#####..##
+##...#...#.#####.
+..#.....X...###..
+..#.#.....#....##
+
+

The first nine asteroids to get vaporized, in order, would be:

+
.#....###24...#..
+##...##.13#67..9#
+##...#...5.8####.
+..#.....X...###..
+..#.#.....#....##
+
+

Note that some asteroids (the ones behind the asteroids marked 1, 5, and 7) won't have a chance to be vaporized until the next full rotation. The laser continues rotating; the next nine to be vaporized are:

+
.#....###.....#..
+##...##...#.....#
+##...#......1234.
+..#.....X...5##..
+..#.9.....8....76
+
+

The next nine to be vaporized are then:

+
.8....###.....#..
+56...9#...#.....#
+34...7...........
+..2.....X....##..
+..1..............
+
+

Finally, the laser completes its first full rotation (1 through 3), a second rotation (4 through 8), and vaporizes the last asteroid (9) partway through its third rotation:

+
......234.....6..
+......1...5.....7
+.................
+........X....89..
+.................
+
+

In the large example above (the one with the best monitoring station location at 11,13):

+
    +
  • The 1st asteroid to be vaporized is at 11,12.
  • +
  • The 2nd asteroid to be vaporized is at 12,1.
  • +
  • The 3rd asteroid to be vaporized is at 12,2.
  • +
  • The 10th asteroid to be vaporized is at 12,8.
  • +
  • The 20th asteroid to be vaporized is at 16,0.
  • +
  • The 50th asteroid to be vaporized is at 16,9.
  • +
  • The 100th asteroid to be vaporized is at 10,16.
  • +
  • The 199th asteroid to be vaporized is at 9,6.
  • +
  • The 200th asteroid to be vaporized is at 8,2.
  • +
  • The 201st asteroid to be vaporized is at 10,9.
  • +
  • The 299th and final asteroid to be vaporized is at 11,1.
  • +
+

The Elves are placing bets on which will be the 200th asteroid to be vaporized. Win the bet by determining which asteroid that will be; what do you get if you multiply its X coordinate by 100 and then add its Y coordinate? (For example, 8,2 becomes 802.)

+
+

Your puzzle answer was 806.

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