My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add solution for 2016/19

+55
+52
2016/day19.py
··· 1 + import fileinput 2 + 3 + 4 + class LinkedElf: 5 + def __init__(self, num): 6 + self.num = num 7 + self.next = None 8 + 9 + 10 + def adjacent_elves(num_elves): 11 + head = LinkedElf(1) 12 + curr = head 13 + 14 + for i in range(2, num_elves + 1): 15 + curr.next = LinkedElf(i) 16 + curr = curr.next 17 + 18 + curr.next = head 19 + curr = head 20 + 21 + while curr is not curr.next: 22 + curr.next = curr.next.next 23 + curr = curr.next 24 + 25 + return curr.num 26 + 27 + 28 + def circular_elves(num_elves): 29 + seen = set() 30 + victim = num_elves // 2 31 + 32 + # Start with a double jump if number of elves is odd 33 + double = (num_elves % 2) == 1 34 + 35 + while True: 36 + seen.add(victim) 37 + 38 + if len(seen) == num_elves: 39 + return victim + 1 40 + 41 + for _ in range(2 if double else 1): 42 + victim = (victim + 1) % num_elves 43 + while victim in seen: 44 + victim = (victim + 1) % num_elves 45 + 46 + double = not double 47 + 48 + 49 + if __name__ == '__main__': 50 + num_elves = int(fileinput.input()[0]) 51 + print "Winning Elf in part 1:", adjacent_elves(num_elves) 52 + print "Winning Elf in part 2:", circular_elves(num_elves)
+1
2016/inputs/19.txt
··· 1 + 3017957
+2
2016/outputs/19.txt
··· 1 + 1841611 2 + 1423634