From ecb70c966a0af1e995504021af2f16606666f631 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 19 Dec 2016 10:13:43 +0000 Subject: [PATCH] Day 19 --- advent19.hs | 37 +++++++++++ day19.html | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 216 insertions(+) create mode 100644 advent19.hs create mode 100644 day19.html diff --git a/advent19.hs b/advent19.hs new file mode 100644 index 0000000..9b9406c --- /dev/null +++ b/advent19.hs @@ -0,0 +1,37 @@ +{-# LANGUAGE BangPatterns #-} + +import Prelude hiding (length, take, drop) +import Data.Sequence + +-- input = 5 +input = 3012210 + +main :: IO () +main = do + part1 + part2 + +part1 :: IO () +part1 = print $ 2 * (input - 2 ^ (toInteger (floor $ logBase 2 (fromIntegral input)))) + 1 + +part2 :: IO () +part2 = print $ index (presentSteps initial) 0 + +presentSteps :: Seq Int -> Seq Int +presentSteps elves + | isFinished elves = elves + | otherwise = presentSteps $ next elves + +initial :: Seq Int +initial = fromList [1..input] + +isFinished :: Seq Int -> Bool +isFinished elves = length elves == 1 + +next :: Seq Int -> Seq Int +next elves = prefix >< (midfix |> suffix) + where + target = length elves `quot` 2 + prefix = drop 1 $ take target elves + midfix = drop (target+1) elves + suffix = index elves 0 diff --git a/day19.html b/day19.html new file mode 100644 index 0000000..441174e --- /dev/null +++ b/day19.html @@ -0,0 +1,179 @@ + + + + +Day 19 - Advent of Code 2016 + + + + + + +

Advent of Code

Neil Smith (AoC++) 38*

   sub y{2016}

+ + + +
+

--- Day 19: An Elephant Named Joseph ---

The Elves contact you over a highly secure emergency channel. Back at the North Pole, the Elves are busy misunderstanding White Elephant parties.

+

Each Elf brings a present. They all sit in a circle, numbered starting with position 1. Then, starting with the first Elf, they take turns stealing all the presents from the Elf to their left. An Elf with no presents is removed from the circle and does not take turns.

+

For example, with five Elves (numbered 1 to 5):

+
  1
+5   2
+ 4 3
+
+
    +
  • Elf 1 takes Elf 2's present.
  • +
  • Elf 2 has no presents and is skipped.
  • +
  • Elf 3 takes Elf 4's present.
  • +
  • Elf 4 has no presents and is also skipped.
  • +
  • Elf 5 takes Elf 1's two presents.
  • +
  • Neither Elf 1 nor Elf 2 have any presents, so both are skipped.
  • +
  • Elf 3 takes Elf 5's three presents.
  • +
+

So, with five Elves, the Elf that sits starting in position 3 gets all the presents.

+

With the number of Elves given in your puzzle input, which Elf gets all the presents?

+
+

Your puzzle answer was 1830117.

--- Part Two ---

Realizing the folly of their present-exchange rules, the Elves agree to instead steal presents from the Elf directly across the circle. If two Elves are across the circle, the one on the left (from the perspective of the stealer) is stolen from. The other rules remain unchanged: Elves with no presents are removed from the circle entirely, and the other elves move in slightly to keep the circle evenly spaced.

+

For example, with five Elves (again numbered 1 to 5):

+
    +
  • The Elves sit in a circle; Elf 1 goes first: +
      1
    +5   2
    + 4 3
    +
  • +
  • Elves 3 and 4 are across the circle; Elf 3's present is stolen, being the one to the left. Elf 3 leaves the circle, and the rest of the Elves move in: +
      1           1
    +5   2  -->  5   2
    + 4 -          4
    +
  • +
  • Elf 2 steals from the Elf directly across the circle, Elf 5: +
      1         1 
    +-   2  -->     2
    +  4         4 
    +
  • +
  • Next is Elf 4 who, choosing between Elves 1 and 2, steals from Elf 1: +
     -          2  
    +    2  -->
    + 4          4
    +
  • +
  • Finally, Elf 2 steals from Elf 4: +
     2
    +    -->  2  
    + -
    +
  • +
+

So, with five Elves, the Elf that sits starting in position 2 gets all the presents.

+

With the number of Elves given in your puzzle input, which Elf now gets all the presents?

+
+

Your puzzle answer was 1417887.

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.

+

Your puzzle input was 3012210.

+

You can also this puzzle.

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