Advertisement
Guest User

Advent of Code: Day 19, 2016

a guest
Dec 19th, 2016
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. N_ELVES = 3018458
  2.  
  3. class Elf():
  4.     n = 0
  5.     def __init__(self, right=None):
  6.         self.left = None
  7.         self.right = right
  8.         Elf.n += 1
  9.         self.n = Elf.n
  10.    
  11.     def remove_self(self):
  12.         self.left.right = self.right
  13.         self.right.left = self.left
  14.         return self.left # return elf to left for easy iterating
  15.  
  16. def do_stuff(*args, part_two=False):
  17.     # Create all of the elves and link them together
  18.     Elf.n = 0
  19.     first_elf = Elf()
  20.     last_elf = first_elf
  21.     for i in range(N_ELVES - 1):
  22.         new_elf = Elf(right=last_elf)
  23.         last_elf.left = new_elf
  24.         last_elf = new_elf
  25.     last_elf.left = first_elf
  26.     total_elves = N_ELVES
  27.    
  28.     # Find first elf that is removed from the game
  29.     current_elf = first_elf
  30.     if part_two:
  31.         for _ in range(N_ELVES // 2):
  32.             current_elf = current_elf.left
  33.     else:
  34.         current_elf = current_elf.left
  35.            
  36.     # Take one elf out of the game at a time
  37.     while total_elves > 1:
  38.         if part_two:
  39.             current_elf = current_elf.remove_self()
  40.             if total_elves % 2 == 1:
  41.                 current_elf = current_elf.left
  42.         else:
  43.             current_elf = current_elf.remove_self().left
  44.         total_elves -= 1
  45.    
  46.     # Show the remaining elf
  47.     print("Part {}:".format(2 if part_two else 1), current_elf.n)
  48.    
  49. do_stuff()
  50. do_stuff(part_two=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement