r/adventofcode • • Dec 19 '16

SOLUTION MEGATHREAD --- 2016 Day 19 Solutions ---

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

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".


/⧹w+/ IS MANDATORY [?]


[Update @ 00:15] 2 gold, silver cap. Thank you for subscribing to Easter Bunny Facts!

  • Fact: The Easter Bunny will sometimes leave eggs in the microchip assembly room.

[Update @ 00:30] 11 gold, silver cap.

  • Fact: The Easter Bunny killed everyone who found out how he got these stars.

[Update @ 00:45] 45 gold, silver cap.

  • Fact: In space, The Easter Bunny can hear you scream.

[Update @ 01:00] 66 gold, silver cap.

  • Fact: The Easter Bunny purposefully deleted your comments.

[Update @ 01:15] 92 gold, silver cap.

  • Fact: The Easter Bunny has bottled your frustration. Don't ask why.

[Update @ 01:20] Leaderboard capped!

  • Fact: What you have just done is no better than what the Easter Bunny has done. Thief.

Thank you for subscribing to Easter Bunny Facts!


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

10 Upvotes

130 comments sorted by

View all comments

Show parent comment

2

u/bblum Dec 19 '16

Wow, Sequence is super elite. I just transliterated my solution (just above or just below, depending who gets more upvotes :P) to use Sequence instead of list and it runs in 9 seconds. Wish I'd known about it earlier!

1

u/cobbpg Dec 19 '16

I just kept traversing the ever shrinking list linearly:

solution19 = part2
  where
    input = 3005290 :: Int
    part1 = go1 input [1..input]
    part2 = go2 (shiftR input 1) (2 - (input .&. 1)) [1..input]
    go1 _ [elf] = elf
    go1 n elves = n' `seq` go1 n' ((if odd n then tail else id) (odds elves))
      where
        n' = shiftR n 1
    go2 _ _ [elf] = elf
    go2 0 0 [elf, _] = elf
    go2 _ _ [_, elf] = elf
    go2 n m elves = go2 0 m' elves'
      where
        m' = (m + n - length elves) `mod` 3
        elves' = [e | (i, e) <- zip [0..] elves, i < n || (m + n - i) `mod` 3 == 0]
    odds (x : _ : xs) = x : odds xs
    odds [x] = [x]
    odds [] = []