From: Neil Smith Date: Sun, 10 Dec 2017 16:46:59 +0000 (+0000) Subject: Day 10 X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-17.git;a=commitdiff_plain;h=85761c8d6f9d0edfbf382c15c47edc7a412f92ee Day 10 --- diff --git a/advent-of-code.cabal b/advent-of-code.cabal index 347896b..3ad97bd 100644 --- a/advent-of-code.cabal +++ b/advent-of-code.cabal @@ -94,4 +94,10 @@ executable advent09 main-is: advent09.hs default-language: Haskell2010 build-depends: base >= 4.7 && < 5 - \ No newline at end of file + +executable advent10 + hs-source-dirs: src/advent10 + main-is: advent10.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , split diff --git a/data/advent10.txt b/data/advent10.txt new file mode 100644 index 0000000..64328f2 --- /dev/null +++ b/data/advent10.txt @@ -0,0 +1 @@ +189,1,111,246,254,2,0,120,215,93,255,50,84,15,94,62 \ No newline at end of file diff --git a/problems/day10.html b/problems/day10.html new file mode 100644 index 0000000..50c374b --- /dev/null +++ b/problems/day10.html @@ -0,0 +1,186 @@ + + + + +Day 10 - Advent of Code 2017 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 20*

   $year=2017;

+ + + +
+

--- Day 10: Knot Hash ---

You come across some programs that are trying to implement a software emulation of a hash based on knot-tying. The hash these programs are implementing isn't very strong, but you decide to help them anyway. You make a mental note to remind the Elves later not to invent their own cryptographic functions.

+

This hash function simulates tying a knot in a circle of string with 256 marks on it. Based on the input to be hashed, the function repeatedly selects a span of string, brings the ends together, and gives the span a half-twist to reverse the order of the marks within it. After doing this many times, the order of the marks is used to build the resulting hash.

+
  4--5   pinch   4  5           4   1
+ /    \  5,0,1  / \/ \  twist  / \ / \
+3      0  -->  3      0  -->  3   X   0
+ \    /         \ /\ /         \ / \ /
+  2--1           2  1           2   5
+
+

To achieve this, begin with a list of numbers from 0 to 255, a current position which begins at 0 (the first element in the list), a skip size (which starts at 0), and a sequence of lengths (your puzzle input). Then, for each length:

+
    +
  • Reverse the order of that length of elements in the list, starting with the element at the current position.
  • +
  • Move the current position forward by that length plus the skip size.
  • +
  • Increase the skip size by one.
  • +
+

The list is circular; if the current position and the length try to reverse elements beyond the end of the list, the operation reverses using as many extra elements as it needs from the front of the list. If the current position moves past the end of the list, it wraps around to the front. Lengths larger than the size of the list are invalid.

+

Here's an example using a smaller list:

+

Suppose we instead only had a circular list containing five elements, 0, 1, 2, 3, 4, and were given input lengths of 3, 4, 1, 5.

+
    +
  • The list begins as [0] 1 2 3 4 (where square brackets indicate the current position).
  • +
  • The first length, 3, selects ([0] 1 2) 3 4 (where parentheses indicate the sublist to be reversed).
  • +
  • After reversing that section (0 1 2 into 2 1 0), we get ([2] 1 0) 3 4.
  • +
  • Then, the current position moves forward by the length, 3, plus the skip size, 0: 2 1 0 [3] 4. Finally, the skip size increases to 1.
  • +
+
    +
  • The second length, 4, selects a section which wraps: 2 1) 0 ([3] 4.
  • +
  • The sublist 3 4 2 1 is reversed to form 1 2 4 3: 4 3) 0 ([1] 2.
  • +
  • The current position moves forward by the length plus the skip size, a total of 5, causing it not to move because it wraps around: 4 3 0 [1] 2. The skip size increases to 2.
  • +
+
    +
  • The third length, 1, selects a sublist of a single element, and so reversing it has no effect.
  • +
  • The current position moves forward by the length (1) plus the skip size (2): 4 [3] 0 1 2. The skip size increases to 3.
  • +
+
    +
  • The fourth length, 5, selects every element starting with the second: 4) ([3] 0 1 2. Reversing this sublist (3 0 1 2 4 into 4 2 1 0 3) produces: 3) ([4] 2 1 0.
  • +
  • Finally, the current position moves forward by 8: 3 4 2 1 [0]. The skip size increases to 4.
  • +
+

In this example, the first two numbers in the list end up being 3 and 4; to check the process, you can multiply them together to produce 12.

+

However, you should instead use the standard list size of 256 (with values 0 to 255) and the sequence of lengths in your puzzle input. Once this process is complete, what is the result of multiplying the first two numbers in the list?

+
+

Your puzzle answer was 38415.

--- Part Two ---

The logic you've constructed forms a single round of the Knot Hash algorithm; running the full thing requires many of these rounds. Some input and output processing is also required.

+

First, from now on, your input should be taken not as a list of numbers, but as a string of bytes instead. Unless otherwise specified, convert characters to bytes using their ASCII codes. This will allow you to handle arbitrary ASCII strings, and it also ensures that your input lengths are never larger than 255. For example, if you are given 1,2,3, you should convert it to the ASCII codes for each character: 49,44,50,44,51.

+

Once you have determined the sequence of lengths to use, add the following lengths to the end of the sequence: 17, 31, 73, 47, 23. For example, if you are given 1,2,3, your final sequence of lengths should be 49,44,50,44,51,17,31,73,47,23 (the ASCII codes from the input string combined with the standard length suffix values).

+

Second, instead of merely running one round like you did above, run a total of 64 rounds, using the same length sequence in each round. The current position and skip size should be preserved between rounds. For example, if the previous example was your first round, you would start your second round with the same length sequence (3, 4, 1, 5, 17, 31, 73, 47, 23, now assuming they came from ASCII codes and include the suffix), but start with the previous round's current position (4) and skip size (4).

+

Once the rounds are complete, you will be left with the numbers from 0 to 255 in some order, called the sparse hash. Your next task is to reduce these to a list of only 16 numbers called the dense hash. To do this, use numeric bitwise XOR to combine each consecutive block of 16 numbers in the sparse hash (there are 16 such blocks in a list of 256 numbers). So, the first element in the dense hash is the first sixteen elements of the sparse hash XOR'd together, the second element in the dense hash is the second sixteen elements of the sparse hash XOR'd together, etc.

+

For example, if the first sixteen elements of your sparse hash are as shown below, and the XOR operator is ^, you would calculate the first output number like this:

+
65 ^ 27 ^ 9 ^ 1 ^ 4 ^ 3 ^ 40 ^ 50 ^ 91 ^ 7 ^ 6 ^ 0 ^ 2 ^ 5 ^ 68 ^ 22 = 64
+

Perform this operation on each of the sixteen blocks of sixteen numbers in your sparse hash to determine the sixteen numbers in your dense hash.

+

Finally, the standard way to represent a Knot Hash is as a single hexadecimal string; the final output is the dense hash in hexadecimal notation. Because each number in your dense hash will be between 0 and 255 (inclusive), always represent each number as two hexadecimal digits (including a leading zero as necessary). So, if your first three numbers are 64, 7, 255, they correspond to the hexadecimal numbers 40, 07, ff, and so the first six characters of the hash would be 4007ff. Because every Knot Hash is sixteen such numbers, the hexadecimal representation is always 32 hexadecimal digits (0-f) long. +

Here are some example hashes:

+
    +
  • The empty string becomes a2582a3a0e66e6e86e3812dcb672a272.
  • +
  • AoC 2017 becomes 33efeb34ea91902bb2f59c9920caa6cd.
  • +
  • 1,2,3 becomes 3efbe78a8d82f29979031a4aa0b16a9d.
  • +
  • 1,2,4 becomes 63960835bcdc130f0b66d7ff4f6a5a8e.
  • +
+

Treating your puzzle input as a string of ASCII characters, what is the Knot Hash of your puzzle input? Ignore any leading or trailing whitespace you might encounter.

+
+

Your puzzle answer was 9de8846431eef262be78f590e39a4848.

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/advent10/advent10.hs b/src/advent10/advent10.hs new file mode 100644 index 0000000..cf1fff3 --- /dev/null +++ b/src/advent10/advent10.hs @@ -0,0 +1,52 @@ +import Data.List.Split (splitOn, chunksOf) +import Data.Char (ord) +import Data.Bits (xor) +import Text.Printf (printf) + +main :: IO () +main = do + text <- readFile "data/advent10.txt" + let ls = map read $ splitOn "," text + print $ part1 ls + putStrLn $ part2 text + + +part1 :: [Int] -> Int +part1 lengths = (tied!!0) * (tied!!1) + where (tied, _, _) = foldl step ([0..255], 0, 0) lengths + + +part2 :: String -> String +part2 text = densify tied + where lengths = p2lengths text + (tied, _, _) = foldl step ([0..255], 0, 0) lengths + +step :: ([Int], Int, Int) -> Int -> ([Int], Int, Int) +step (original, start, skip) len = (replaced, start', skip + 1) + where replaced = tie original start len + start' = (start + len + skip) `mod` (length original) + +tie :: [a] -> Int -> Int -> [a] +tie original start len = replace original replacement start + where replacement = reverse $ extract original start len + +extract :: [a] -> Int -> Int -> [a] +extract items from len = take len $ drop from $ items ++ items + +replace :: [a] -> [a] -> Int -> [a] +replace original replacement from = take (length original) (start ++ replacement ++ remainder) + where excess = drop (length original - from) replacement + stub = drop (length excess) original + start = take from (excess ++ stub) + remainder = drop (length $ start ++ replacement) original + + +p2lengths :: String -> [Int] +p2lengths text = take (length chunk * 64) $ cycle chunk + where chunk = map ord text ++ [17, 31, 73, 47, 23] + +densify :: [Int] -> String +densify ns = concatMap (printf "%02x") codes + where chunks = chunksOf 16 ns + compress = foldl1 xor + codes = map compress chunks diff --git a/src/advent10/advent10.ipynb b/src/advent10/advent10.ipynb new file mode 100644 index 0000000..2a39401 --- /dev/null +++ b/src/advent10/advent10.ipynb @@ -0,0 +1,680 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "{-# LANGUAGE NegativeLiterals #-}\n", + "{-# LANGUAGE FlexibleContexts #-}" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": {}, + "outputs": [], + "source": [ + "import Data.List.Split (splitOn, chunksOf)\n", + "import Data.Char (ord, chr)\n", + "import Data.Bits\n", + "import Numeric (showHex)\n", + "import Text.Printf" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "extract items from len = take len $ drop from $ items ++ items" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "-- replace original replacement from \n", + "-- | from + length replacement <= length original = replacement ++ drop (length replacement) original\n", + "-- | otherwise = drop (length original) extended ++ \n", + "-- where extended = take from original ++ replacement ++ drop suflen original\n", + "-- suflen = from + length replacement - length original " + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [], + "source": [ + "replace original replacement from = take (length original) (start ++ replacement ++ remainder)\n", + " where excess = drop (length original - from) replacement\n", + " stub = drop (length excess) original\n", + " start = take from (excess ++ stub)\n", + " remainder = drop (length $ start ++ replacement) original " + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0,1,2]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "extract [0, 1, 2, 3, 4] 0 3" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2,1,0]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "reverse [0,1,2]" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [], + "source": [ + "l0 = [0,1,2,3,4]" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2,1,0,3,4]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "replace l0 (reverse $ extract l0 0 3) 0" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[4,0,1]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "extract l0 4 3" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0,4,2,3,1]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "replace l0 (reverse $ extract l0 4 3) 4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "0 1 2 3 4\n", + "0 1 2 3 [4]\n", + "0 1) 2 3 ([4]\n", + "0 4) 2 3 (1\n", + "\n", + "0 1 2 3 4 | 0 1 2 3 4\n", + "0 1 2 3 [4] | 0 1 2 3 4\n", + "0 1 2 3 ([4] | 0 1) 2 3 4\n", + "0 1 2 3 ( 1 | 0 4) 2 3 4\n", + "\n", + "\n", + " 0 1 2 3 4 | 0 1 2 3 4\n", + " [0] 1 2 3 4 | 0 1 2 3 4\n", + "([0] 1 2) 3 4 | 0 1 2 3 4\n", + "( 2 1 0) 3 4 | 0 1 2 3 4" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [], + "source": [ + "tie original start len = replace original replacement start\n", + " where replacement = reverse $ extract original start len" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
Redundant bracket
Found:
(start + len + skip) `mod` (length original)
Why Not:
(start + len + skip) `mod` length original
" + ], + "text/plain": [ + "Line 3: Redundant bracket\n", + "Found:\n", + "(start + len + skip) `mod` (length original)\n", + "Why not:\n", + "(start + len + skip) `mod` length original" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "step (original, start, skip) len = (replaced, start', skip + 1)\n", + " where replaced = tie original start len\n", + " start' = (start + len + skip) `mod` (length original)" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0,1,2,3,4]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "[0..4]" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([2,1,0,3,4],3,1)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "step ([0..4], 0, 0) 3" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([3,4,2,1,0],4,4)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "foldl step ([0..4], 0, 0) [3, 4, 1, 5]" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [], + "source": [ + "part1 :: [Int] -> Int\n", + "part1 lengths = (tied!!0) * (tied!!1)\n", + " where (tied, _, _) = foldl step ([0..255], 0, 0) lengths" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": {}, + "outputs": [], + "source": [ + "part2 text = tied\n", + " where lengths = p2lengths text\n", + " (tied, _, _) = foldl step ([0..255], 0, 0) lengths" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": {}, + "outputs": [], + "source": [ + "main :: IO ()\n", + "main = do \n", + " text <- readFile \"../../data/advent10.txt\"\n", + " let instrs = map read $ splitOn \",\" text\n", + " print $ part1 instrs\n", + " print $ part2 text" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "38415\n", + "[166,221,219,74,89,118,226,182,77,185,188,87,238,181,229,173,45,68,84,147,10,194,196,201,55,80,171,119,186,54,154,46,52,161,9,149,135,255,180,210,129,214,127,18,51,236,56,78,24,187,97,25,131,70,143,106,211,218,189,224,179,220,41,141,240,233,76,26,253,207,159,172,86,241,136,215,12,197,112,62,23,138,0,169,99,50,235,205,122,103,116,71,251,160,206,22,4,202,252,40,234,242,193,39,32,191,63,217,152,117,33,163,61,213,164,133,6,16,110,58,102,43,126,67,31,30,95,199,2,183,1,208,239,96,20,13,57,66,38,132,177,195,44,203,247,65,14,227,100,82,151,3,28,150,98,146,140,37,42,120,168,91,83,27,209,148,176,250,101,162,145,79,115,15,228,192,142,104,155,29,19,53,49,35,128,5,184,248,222,123,124,216,178,64,113,198,244,245,246,167,47,11,170,212,109,114,237,94,144,69,243,254,121,231,59,108,17,153,249,34,72,7,130,21,200,175,81,134,105,137,75,165,156,174,232,125,92,88,8,93,157,223,225,48,111,85,107,204,36,158,90,139,190,230,60,73]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "main" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "97" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "ord 'a'" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'a'" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "chr 97" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "7 `xor` 3" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"ff\"" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "showHex 255 \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "ff" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "printf \"%02x\" 255" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [], + "source": [ + "p2lengths text = take (length chunk * 64) $ cycle chunk\n", + " where chunk = map ord text ++ [17, 31, 73, 47, 23]" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": {}, + "outputs": [], + "source": [ + "densify ns = concatMap (printf \"%02x\") codes\n", + " where chunks = chunksOf 16 ns\n", + " compress = foldl1 xor\n", + " codes = map compress chunks" + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "metadata": {}, + "outputs": [], + "source": [ + "part2 :: String -> String\n", + "part2 text = densify tied\n", + " where lengths = p2lengths text\n", + " (tied, _, _) = foldl step ([0..255], 0, 0) lengths" + ] + }, + { + "cell_type": "code", + "execution_count": 121, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "a2582a3a0e66e6e86e3812dcb672a272" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "putStrLn $ part2 \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "33efeb34ea91902bb2f59c9920caa6cd" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "putStrLn $ part2 \"AoC 2017\"" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3efbe78a8d82f29979031a4aa0b16a9d" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "putStrLn $ part2 \"1,2,3\"" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "63960835bcdc130f0b66d7ff4f6a5a8e" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "putStrLn $ part2 \"1,2,4\"" + ] + }, + { + "cell_type": "code", + "execution_count": 129, + "metadata": {}, + "outputs": [], + "source": [ + "main :: IO ()\n", + "main = do \n", + " text <- readFile \"../../data/advent10.txt\"\n", + " let instrs = map read $ splitOn \",\" text\n", + " print $ part1 instrs\n", + " putStrLn $ part2 text" + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "38415\n", + "9de8846431eef262be78f590e39a4848" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "main" + ] + }, + { + "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 +}