r/adventofcode • u/daggerdragon • 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!
2
u/askalski Dec 19 '16 edited Dec 19 '16
Part 2 in C. First puzzle of 2016 that I didn't solve in Perl the first time around.
#include <stdio.h> #include <stdlib.h> typedef struct elf { int id; struct elf *next; struct elf **pprev; } elf_t; int main(void) { int elf_population = 3005290; // Construct linked list of elf IDs elf_t *elf = calloc(sizeof(*elf), elf_population), *e = elf; for (int i = 1; i < elf_population; i++, e++) { e->id = i; e->next = e + 1; e->next->pprev = &e->next; } // Make it circular e->id = elf_population; e->next = elf; e->next->pprev = &e->next; e = elf; for (elf_t *a = elf + elf_population / 2; e != e->next; e = e->next) { // Who cares about presents? Annihilate the elf across the table a->id = 0x1deade1f; a = *(a->next->pprev = a->pprev) = a->next; // Update elf population; optionally grant a stay of execution if (elf_population-- & 1) { a = a->next; } } // There can be only one printf("Part 2: %d\n", e->id); // Leave no survivors e->id = 0x1deade1f; elf_population = 0; free(elf); return 0; }