Skip to content

Commit fb4ad59

Browse files
committed
2020 Day 23 solution in C
1 parent cb76189 commit fb4ad59

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

2020/23b.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <stdio.h>
2+
3+
struct node {
4+
int n;
5+
struct node *next;
6+
struct node *prev;
7+
};
8+
9+
struct node c[1000001];
10+
11+
struct node *remove_three(struct node *start_node) {
12+
struct node *left = start_node->prev;
13+
struct node *last = start_node->next->next;
14+
struct node *right = last->next;
15+
16+
left->next = right;
17+
start_node->prev = NULL;
18+
19+
right->prev = left;
20+
last->next = NULL;
21+
return start_node;
22+
}
23+
24+
void insert_three(struct node *start_node, struct node *where) {
25+
struct node *left = where;
26+
struct node *right = where->next;
27+
28+
left->next = start_node;
29+
start_node->prev = left;
30+
31+
struct node *last = start_node->next->next;
32+
last->next = right;
33+
right->prev = last;
34+
}
35+
36+
void insert_before(struct node *node, struct node *where) {
37+
struct node *left = where->prev;
38+
39+
left->next = node;
40+
node->prev = left;
41+
42+
node->next = where;
43+
where->prev = node;
44+
}
45+
46+
int main() {
47+
int input[] = {3, 6, 8, 1, 9, 5, 7, 4, 2};
48+
for (int i=1; i < 1000001; i++)
49+
c[i].n = i;
50+
for (int i=10; i < 1000001; i++) {
51+
c[i].next = c + i + 1;
52+
c[i].prev = c + i - 1;
53+
}
54+
c[10].prev = c + 1000000;
55+
c[1000000].next = c + 10;
56+
for (int i = 0; i < 9; i++) {
57+
insert_before(&c[input[i]], c + 10);
58+
}
59+
60+
struct node *current_cup = &c[3];
61+
for (int i = 0; i < 10000000; i++) {
62+
int destination = current_cup->n - 1;
63+
if (destination < 1)
64+
destination = 1000000;
65+
struct node *three = remove_three(current_cup->next);
66+
while (destination == three->n ||
67+
destination == three->next->n ||
68+
destination == three->next->next->n ||
69+
destination == current_cup->n) {
70+
destination--;
71+
if (destination < 1)
72+
destination = 1000000;
73+
}
74+
insert_three(three, c + destination);
75+
current_cup = current_cup->next;
76+
}
77+
78+
printf("%lld\n", ((long long) c[1].next->n)*c[1].next->next->n);
79+
}

2020/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
23b.exe: 23b.c
2+
gcc -o 23b.exe -Wall -Wextra -std=c11 -g 23b.c

0 commit comments

Comments
 (0)