Day15 C
This commit is contained in:
BIN
day15/c/day15
Executable file
BIN
day15/c/day15
Executable file
Binary file not shown.
112
day15/c/day15.c
Normal file
112
day15/c/day15.c
Normal file
@@ -0,0 +1,112 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LINE_MAX_LENGTH 32*1024
|
||||
#define STRBUF_SIZE 64
|
||||
#define NUM_BOXES 256
|
||||
#define MAX_LENS_PER_BOX 32
|
||||
#define NUM_0_CHARCODE 48
|
||||
|
||||
typedef struct lens {
|
||||
char label[STRBUF_SIZE];
|
||||
int label_len;
|
||||
int focal_length;
|
||||
} lens_t;
|
||||
|
||||
typedef struct box {
|
||||
lens_t lenses[MAX_LENS_PER_BOX];
|
||||
int lenses_num;
|
||||
} box_t;
|
||||
|
||||
int hash(char*, int);
|
||||
|
||||
int main() {
|
||||
char *p, *buf, c, *start;
|
||||
|
||||
buf = (char *)malloc(LINE_MAX_LENGTH);
|
||||
memset(buf, 0, LINE_MAX_LENGTH);
|
||||
p = buf;
|
||||
char strbuf[STRBUF_SIZE];
|
||||
memset(strbuf, 0, STRBUF_SIZE * sizeof(char));
|
||||
|
||||
box_t boxes[NUM_BOXES];
|
||||
memset(boxes, 0, NUM_BOXES * sizeof(box_t));
|
||||
|
||||
int sum = 0;
|
||||
while ((c = getchar()) != EOF) {
|
||||
*p++ = c;
|
||||
if (c == '\n') {
|
||||
p = buf;
|
||||
start = p;
|
||||
while (*p != '\0') {
|
||||
while (*p != ',' && *p != '\n') p++;
|
||||
strncpy(strbuf, start, p - start);
|
||||
sum += hash(strbuf, p - start);
|
||||
// Find = or -
|
||||
while (*p != '-' && *p != '=') p--;
|
||||
int box_num = hash(strbuf, p - start);
|
||||
if (*p == '-') {
|
||||
for (int i = 0; i < boxes[box_num].lenses_num; i++) {
|
||||
if (boxes[box_num].lenses[i].label_len == p - start && !strncmp(boxes[box_num].lenses[i].label, strbuf, p - start)) {
|
||||
for (int j = i; j < boxes[box_num].lenses_num - 1; j++) {
|
||||
memcpy(&boxes[box_num].lenses[j], &boxes[box_num].lenses[j + 1], sizeof(lens_t));
|
||||
}
|
||||
boxes[box_num].lenses_num--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
p++;
|
||||
} else if (*p == '=') {
|
||||
int found = 0;
|
||||
int focal_length = *(p+1) - NUM_0_CHARCODE;
|
||||
for (int i = 0; i < boxes[box_num].lenses_num; i++) {
|
||||
if (boxes[box_num].lenses[i].label_len == p - start && !strncmp(boxes[box_num].lenses[i].label, strbuf, p - start)) {
|
||||
boxes[box_num].lenses[i].focal_length = focal_length;
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
boxes[box_num].lenses[boxes[box_num].lenses_num].label_len = p - start;
|
||||
boxes[box_num].lenses[boxes[box_num].lenses_num].focal_length = focal_length;
|
||||
strncpy(boxes[box_num].lenses[boxes[box_num].lenses_num].label, strbuf, p - start);
|
||||
boxes[box_num].lenses_num++;
|
||||
}
|
||||
p++;
|
||||
p++;
|
||||
}
|
||||
|
||||
p++;
|
||||
start = p;
|
||||
memset(strbuf, 0, STRBUF_SIZE * sizeof(char));
|
||||
}
|
||||
memset(buf, 0, LINE_MAX_LENGTH);
|
||||
p = buf;
|
||||
}
|
||||
}
|
||||
|
||||
int part2 = 0;
|
||||
for (int i = 0; i < NUM_BOXES; i++) {
|
||||
for (int j = 0; j < boxes[i].lenses_num; j++) {
|
||||
part2 += (i+1) * (j+1) * boxes[i].lenses[j].focal_length;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%i\n", sum);
|
||||
printf("%i\n", part2);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
int hash(char *str, int len) {
|
||||
int current_value = 0;
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
current_value += str[i];
|
||||
current_value *= 17;
|
||||
current_value %= 256;
|
||||
}
|
||||
|
||||
return current_value;
|
||||
}
|
||||
1
day15/input.txt
Normal file
1
day15/input.txt
Normal file
File diff suppressed because one or more lines are too long
1
day15/sample.txt
Normal file
1
day15/sample.txt
Normal file
@@ -0,0 +1 @@
|
||||
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
|
||||
Reference in New Issue
Block a user