From cfe831809e32c1b0c9746eb9939fe0a2cd9a1d37 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 15 Dec 2017 08:58:13 +0000 Subject: [PATCH] Day 15 --- advent-of-code.cabal | 5 + problems/day15.html | 203 ++++++++++++++++ src/advent14/advent14.hs | 2 - src/advent15/advent15.hs | 37 +++ src/advent15/advent15.ipynb | 462 ++++++++++++++++++++++++++++++++++++ 5 files changed, 707 insertions(+), 2 deletions(-) create mode 100644 problems/day15.html create mode 100644 src/advent15/advent15.hs create mode 100644 src/advent15/advent15.ipynb diff --git a/advent-of-code.cabal b/advent-of-code.cabal index e133aae..4bd4b0d 100644 --- a/advent-of-code.cabal +++ b/advent-of-code.cabal @@ -143,3 +143,8 @@ executable advent14serial , split , containers +executable advent15 + hs-source-dirs: src/advent15 + main-is: advent15.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 diff --git a/problems/day15.html b/problems/day15.html new file mode 100644 index 0000000..2e3ba68 --- /dev/null +++ b/problems/day15.html @@ -0,0 +1,203 @@ + + + + +Day 15 - Advent of Code 2017 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 30*

   0xffff&2017

+ + + +
+

--- Day 15: Dueling Generators ---

Here, you encounter a pair of dueling generators. The generators, called generator A and generator B, are trying to agree on a sequence of numbers. However, one of them is malfunctioning, and so the sequences don't always match.

+

As they do this, a judge waits for each of them to generate its next value, compares the lowest 16 bits of both values, and keeps track of the number of times those parts of the values match.

+

The generators both work on the same principle. To create its next value, a generator will take the previous value it produced, multiply it by a factor (generator A uses 16807; generator B uses 48271), and then keep the remainder of dividing that resulting product by 2147483647. That final remainder is the value it produces next.

+

To calculate each generator's first value, it instead uses a specific starting value as its "previous value" (as listed in your puzzle input).

+

For example, suppose that for starting values, generator A uses 65, while generator B uses 8921. Then, the first five pairs of generated values are:

+
--Gen. A--  --Gen. B--
+   1092455   430625591
+1181022009  1233683848
+ 245556042  1431495498
+1744312007   137874439
+1352636452   285222916
+
+

In binary, these pairs are (with generator A's value first in each pair):

+
00000000000100001010101101100111
+00011001101010101101001100110111
+
+01000110011001001111011100111001
+01001001100010001000010110001000
+
+00001110101000101110001101001010
+01010101010100101110001101001010
+
+01100111111110000001011011000111
+00001000001101111100110000000111
+
+01010000100111111001100000100100
+00010001000000000010100000000100
+
+

Here, you can see that the lowest (here, rightmost) 16 bits of the third value match: 1110001101001010. Because of this one match, after processing these five pairs, the judge would have added only 1 to its total.

+

To get a significant sample, the judge would like to consider 40 million pairs. (In the example above, the judge would eventually find a total of 588 pairs that match in their lowest 16 bits.)

+

After 40 million pairs, what is the judge's final count?

+
+

Your puzzle answer was 631.

--- Part Two ---

In the interest of trying to align a little better, the generators get more picky about the numbers they actually give to the judge.

+

They still generate values in the same way, but now they only hand a value to the judge when it meets their criteria:

+
    +
  • Generator A looks for values that are multiples of 4.
  • +
  • Generator B looks for values that are multiples of 8.
  • +
+

Each generator functions completely independently: they both go through values entirely on their own, only occasionally handing an acceptable value to the judge, and otherwise working through the same sequence of values as before until they find one.

+

The judge still waits for each generator to provide it with a value before comparing them (using the same comparison method as before). It keeps track of the order it receives values; the first values from each generator are compared, then the second values from each generator, then the third values, and so on.

+

Using the example starting values given above, the generators now produce the following first five values each:

+
--Gen. A--  --Gen. B--
+1352636452  1233683848
+1992081072   862516352
+ 530830436  1159784568
+1980017072  1616057672
+ 740335192   412269392
+
+

These values have the following corresponding binary values:

+
01010000100111111001100000100100
+01001001100010001000010110001000
+
+01110110101111001011111010110000
+00110011011010001111010010000000
+
+00011111101000111101010001100100
+01000101001000001110100001111000
+
+01110110000001001010100110110000
+01100000010100110001010101001000
+
+00101100001000001001111001011000
+00011000100100101011101101010000
+
+

Unfortunately, even though this change makes more bits similar on average, none of these values' lowest 16 bits match. Now, it's not until the 1056th pair that the judge finds the first match:

+
--Gen. A--  --Gen. B--
+1023762912   896885216
+
+00111101000001010110000111100000
+00110101011101010110000111100000
+
+

This change makes the generators much slower, and the judge is getting impatient; it is now only willing to consider 5 million pairs. (Using the values from the example above, after five million pairs, the judge would eventually find a total of 309 pairs that match in their lowest 16 bits.)

+

After 5 million pairs, but using this new generator logic, what is the judge's final count?

+
+

Your puzzle answer was 279.

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/advent14/advent14.hs b/src/advent14/advent14.hs index 8c49253..698853c 100644 --- a/src/advent14/advent14.hs +++ b/src/advent14/advent14.hs @@ -2,12 +2,10 @@ import Data.List.Split (chunksOf) import Data.Char (ord) import Text.Printf (printf) import Data.Bits (xor) --- import qualified Data.Map.Strict as M import qualified Data.Set as S import qualified Data.Graph as G import Control.Parallel.Strategies (parMap, rpar) - type CellSet = S.Set (Int, Int) puzzleKey = "xlqgujun" diff --git a/src/advent15/advent15.hs b/src/advent15/advent15.hs new file mode 100644 index 0000000..7cec0d0 --- /dev/null +++ b/src/advent15/advent15.hs @@ -0,0 +1,37 @@ +import Data.Word +import Data.Bits + +generatorAStart = 873 +generatorBStart = 583 + +main :: IO () +main = do + print $ part1 + print $ part2 + +part1 = length $ filter (uncurry (==)) $ take 40000000 $ zip streamA streamB + + +part2 = length $ filter (uncurry (==)) $ take 5000000 $ zip fsA fsB + where fsA = filteredStream 3 streamA + fsB = filteredStream 7 streamB + + +generatorA = generator 2147483647 16807 +generatorB = generator 2147483647 48271 + +streamA = stream generatorA generatorAStart +streamB = stream generatorB generatorBStart + +generator :: Word64 -> Word64 -> Word64 -> Word64 +generator divisor factor n = fromIntegral $ fromIntegral n * factor `rem` divisor + +toWord16 :: Word64 -> Word16 +toWord16 = fromIntegral + +stream :: (Word64 -> Word64) -> Word64 -> [Word16] +stream gen n0 = map toWord16 $ drop 1 $ iterate gen n0 + +filteredStream :: Word16 -> [Word16] -> [Word16] +filteredStream f str = filter (\n -> n .&. f == 0) str + diff --git a/src/advent15/advent15.ipynb b/src/advent15/advent15.ipynb new file mode 100644 index 0000000..d89f1da --- /dev/null +++ b/src/advent15/advent15.ipynb @@ -0,0 +1,462 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import Data.Word\n", + "import Data.Bits" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "a :: Word8\n", + "a=156" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "b :: Word16\n", + "b = 202" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
Redundant bracket
Found:
(fromIntegral a) * b
Why Not:
fromIntegral a * b
" + ], + "text/plain": [ + "Line 1: Redundant bracket\n", + "Found:\n", + "(fromIntegral a) * b\n", + "Why not:\n", + "fromIntegral a * b" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "31512" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "(fromIntegral a) * b" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "24" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "(156 * 202) `mod` 256" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Generator A starts with 873\n", + "\n", + "Generator B starts with 583" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "generator :: Word64 -> Word64 -> Word64 -> Word64\n", + "generator divisor factor n = fromIntegral $ fromIntegral n * factor `rem` divisor" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "generatorA = generator 2147483647 16807\n", + "generatorB = generator 2147483647 48271" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "toWord16 :: Word64 -> Word16\n", + "toWord16 = fromIntegral" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[43879,63289,58186,5831,38948]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "map toWord16 $ take 5 $ drop 1 $ iterate generatorA 65" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[54071,34184,58186,52231,10244]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "map toWord16 $ take 5 $ drop 1 $ iterate generatorB 8921" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "stream :: (Word64 -> Word64) -> Word64 -> [Word16]\n", + "stream gen n0 = map toWord16 $ drop 1 $ iterate gen n0" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "588" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "length $ filter (uncurry (==)) $ take 40000000 $ zip (stream generatorA 65) (stream generatorB 8921)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "631" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "length $ filter (uncurry (==)) $ take 40000000 $ zip (stream generatorA 873) (stream generatorB 583)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "filteredStream :: Word16 -> [Word16] -> [Word16]\n", + "filteredStream f stream = filter (\\n -> n .&. f == 0) stream" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[38948,48816,54372,43440,40536]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "take 5 $ filteredStream 3 $ stream generatorA 65" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "38948" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "toWord16 1352636452" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1352636452,1992081072,530830436,1980017072,740335192]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "take 5 $ filter (\\n -> n .&. 3 == 0) $ iterate generatorA 65" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1233683848,862516352,1159784568,1616057672,412269392]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "take 5 $ filter (\\n -> n .&. 7 == 0) $ iterate generatorB 8921" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "solE = length $ filter (uncurry (==)) $ take 5000000 $ zip fsA fsB\n", + " where fsA = filteredStream 3 $ stream generatorA 65\n", + " fsB = filteredStream 7 $ stream generatorB 8921" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "309" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "solE" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "sol = length $ filter (uncurry (==)) $ take 5000000 $ zip fsA fsB\n", + " where fsA = filteredStream 3 $ stream generatorA 873\n", + " fsB = filteredStream 7 $ stream generatorB 583" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "279" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "sol" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Haskell", + "language": "haskell", + "name": "haskell" + }, + "language_info": { + "codemirror_mode": "ihaskell", + "file_extension": ".hs", + "name": "haskell", + "version": "8.0.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} -- 2.34.1