Advent of Code 2016 solutions https://adventofcode.com/2016/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/python3
  2. """Solution for day 19 of Advent of Code 2016.
  3. There are two different solutions presented here. The "manually" methods simulate the present-thieving step by step
  4. to get the results. I used these to figure out the patterns to both part 1 and part 2 and thus write simple, super
  5. quick formulae to get the answer.
  6. For part 1, the results look something like this:
  7. Number of Elves | Winning Elf
  8. 1 | 0
  9. 2 | 0
  10. 3 | 2
  11. 4 | 0
  12. 5 | 2
  13. 6 | 4
  14. 7 | 6
  15. 8 | 0
  16. So if the number of elves is a power of two, then the first elf wins. Otherwise, the winning elf is the one with a
  17. number double the difference between the last power of two and the number of elves.
  18. For part 2, the results are more complicated (as you'd expect!):
  19. Number of Elves | Winning Elf
  20. 1 | 0
  21. 2 | 0
  22. 3 | 2
  23. 4 | 0
  24. 5 | 1
  25. 6 | 2
  26. 7 | 4
  27. 8 | 6
  28. 9 | 8
  29. 10 | 0
  30. 11 | 1
  31. 12 | 2
  32. 13 | 3
  33. ... | ...
  34. 18 | 8
  35. 19 | 10
  36. 20 | 12
  37. ... | ...
  38. 28 | 0
  39. The points where elf 0 wins (and the sequence resets) now follow the pattern 3^n+1 (instead of 2^n from part 1).
  40. Between those points, the winning elf increases by 1 for the first 3^n elves, then by 2 thereafter.
  41. """
  42. import math
  43. seed = 3004953
  44. print('Part one: %s' % (1 + 2 * int(seed - 2 ** math.floor(math.log(seed, 2)))))
  45. lp = 3 ** int(math.floor(0.0001 + math.log(seed - 1, 3))) # Add a small amount to avoid floating point errors
  46. print('Part two: %s' % ((seed - lp) * (1 + max(0, (seed - 2 * lp) / (lp + 1)))))
  47. def run_part1_manually(n):
  48. elves = {}
  49. for i in range(n):
  50. elves[i] = i + 1 if i < n - 1 else 0
  51. current = 0
  52. while len(elves) > 1:
  53. neighbour = elves[current]
  54. elves[current] = elves[neighbour]
  55. del elves[neighbour]
  56. current = elves[current]
  57. return elves.keys()[0]
  58. def run_part2_manually(n):
  59. elves = range(n)
  60. current = 0
  61. while len(elves) > 1:
  62. target = (current + int(math.floor(len(elves) / 2))) % len(elves)
  63. del elves[target]
  64. if target > current:
  65. current = (current + 1) % len(elves)
  66. else:
  67. current %= len(elves)
  68. return elves[0]