Day 19 - Advent of Code 2016
Working solutions for the day 19 puzzles.
Part One
""" day_19_01.py """
# usage: python3 day_19_01.py 3014387
import sys
def next_elf(x):
""" next elf with gifts """
while circle[x := (x + 1) % elves] == 0:
pass
return x
elves = int(sys.argv[1])
circle = [1 for i in range(1, elves + 1)]
i = 0
while circle[i] != elves:
j = next_elf(i)
circle[i] += circle[j]
circle[j] = 0
i = next_elf(j)
print(i + 1)
Part Two
/* day_19_02.c */
// usage: gcc -o day_19_02 day_19_02.c
// ./day_19_02 3014387
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
int elves = atoi(argv[1]);
int remaining = elves;
int** circle = (int**) malloc(elves * sizeof(int*));
for (int i = 0; i < elves; i++) {
circle[i] = (int*) malloc(3 * sizeof(int));
}
for (int i = 0; i < elves; i++) {
circle[i][0] = 1;
circle[i][1] = (i - 1) % elves;
circle[i][2] = (i + 1) % elves;
}
int elf = 0, other_elf;
while (circle[elf][0] != elves) {
other_elf = elf;
for (int i = 0; i < remaining / 2; i++) {
other_elf = circle[other_elf][2];
}
circle[elf][0] += circle[other_elf][0];
circle[circle[other_elf][1]][2] = circle[other_elf][2];
circle[circle[other_elf][2]][1] = circle[other_elf][1];
remaining -= 1;
elf = circle[elf][2];
}
printf("%d\n", elf + 1);
for (int i = 0; i < elves; i++) {
free(circle[i]);
}
free(circle);
return 0;
}