-
Notifications
You must be signed in to change notification settings - Fork 2
/
01.c
55 lines (46 loc) · 984 Bytes
/
01.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
#include "adventofcode.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define PUZZLE_NAME "Day 1: Chronal Calibration"
int pt1(const char *s) {
int freq = 0;
while (*s != '\0') {
int n;
s = parse_int(&n, s);
freq += n;
if (*s == '\n')
s++;
}
return freq;
}
int pt2(char *input) {
int freq = 0;
uint8_t seen[256 * 1024] = {0};
for (;;) {
const char *s = input;
int hash;
int n;
while (*s != '\0') {
s = parse_int(&n, s);
freq += n;
hash = freq + 128 * 1024;
if (seen[hash] == 1) {
return freq;
}
seen[hash] = 1;
if (*s == '\n')
s++;
}
}
}
int main(void) {
clock_t t = clock_time();
char input[1024 * 64];
read_input_file(input, 1024 * 64, "01.txt");
printf("--- %s ---\n", PUZZLE_NAME);
printf("Part 1: %d\n", pt1(input));
printf("Part 2: %d\n", pt2(input));
printf("Time: %.2fms\n", clock_time_since(t));
return EXIT_SUCCESS;
}