-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23.c
107 lines (86 loc) · 2.34 KB
/
day23.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Advent of Code - Day 23
// @curiouskiwi @gary_anderson
// 23 Dec 2020
#include <stdio.h>
// #define NUMCUPS 9
#define NUMCUPS 1000000
typedef struct cup
{
int value;
struct cup *next;
}
cup;
cup c[NUMCUPS];
void printcups(cup *check);
int main(void)
{
char *puzzle = "624397158";
//char *puzzle = "389125467";
cup *current;
cup *removed;
// **initialisation for part 1**
// for (int i = 0; i < 9; i++)
// {
// c[i].value = puzzle[i] - '0';
// c[i].next = &c[(i+1)%NUMCUPS];
// }
for (int i = 0; i < NUMCUPS; i++)
{
c[i].value = i+1;
c[i].next = &c[(i+1)%NUMCUPS];
}
for (int i = 0; i < 8; i++)
{
c[puzzle[i]-'1'].next = &c[puzzle[i+1]-'1'];
}
c[puzzle[8]-'1'].next = &c[9];
c[NUMCUPS-1].next = &c[puzzle[0]-'1'];
current = &c[puzzle[0]-'1'];
int counter = 0;
while (counter < 10000000)
{
// printf("current = %d\n", current->value);
removed = current->next;
current->next = removed->next->next->next;
int placetoput = current->value - 1;
if (placetoput == 0) placetoput = NUMCUPS;
while (removed->value == placetoput || removed->next->value == placetoput ||
removed->next->next->value == placetoput)
{
placetoput--;
if (placetoput == 0) placetoput = NUMCUPS;
}
// printf("pl: %i\n", placetoput);
cup *newplace;
// int i = 0;
newplace = &c[placetoput-1];
// while (newplace->value != placetoput)
// {
// newplace = newplace->next;
// // printf("***\n");
// // if (++i==10) break;
// }
removed->next->next->next = newplace->next;
newplace->next = removed;
counter++;
current = current->next;
}
while (current->value != 1)
{
current = current->next;
// printf("***\n");
// if (++i==10) break;
}
printf("For part 2: ");
printf("%i- %i - %i \n", current->value, current->next->value, current->next->next->value);
printf("%li\n", (long)current->next->value * current->next->next->value);
}
void printcups(cup *check)
{
for (int i = 0; i < 9; i++)
{
printf("%i ->", check->value);
check = check->next;
}
printf("\n");
}