-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15b.c
42 lines (37 loc) · 988 Bytes
/
day15b.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
#include "common.h"
#include "vector.h"
#include "map.h"
size_t const N_ROUNDS = 30000000;
int main (int argc, char ** argv) {
Vector_t nums;
vector_init(&nums, N_ROUNDS);
HashMap_t last;
hashmap_init(&last, 1024, N_ROUNDS/1024);
do {
long int x;
int ret = scanf("%li", &x);
if (ret == 1) {
hashmap_set(&last, x, nums.count);
vector_push_back(&nums, x);
} else if (ret == EOF) {
break;
} else {
ERROR("scanf failed");
}
} while (1);
intptr_t x_index = -1;
while (nums.count < N_ROUNDS) {
long int y = 0;
if (x_index != -1) {
y = nums.count - 1 - x_index;
}
if (hashmap_contains(&last, y)) {
x_index = hashmap_get(&last, y);
} else {
x_index = -1;
}
hashmap_set(&last, y, nums.count);
vector_push_back(&nums, y);
}
DISP(nums.data[nums.count-1]);
}