aoc2023/day1/p2.c

97 lines
3.6 KiB
C
Raw Permalink Normal View History

2023-12-02 17:34:17 +04:00
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
int main(int argc, char **argv) {
FILE *input;
char *line = NULL;
2023-12-06 05:18:43 +04:00
size_t line_buf_length = 0;
2023-12-02 17:34:17 +04:00
if (argc == 1 || argv[1][0] == '-')
input = stdin;
else if ((input = fopen(argv[1], "r")) == NULL) {
2023-12-06 05:18:43 +04:00
fprintf(stderr, "Cannot open file %s\n", argv[1]);
2023-12-02 17:34:17 +04:00
return -1;
}
2023-12-06 05:18:43 +04:00
uint64_t calibration_value = 0;
2023-12-02 17:34:17 +04:00
2023-12-06 05:18:43 +04:00
while (getline(&line, &line_buf_length, input) != -1) {
uint64_t current_calibration_value = 0;
2023-12-02 17:34:17 +04:00
int8_t last_digit = 0;
2023-12-06 05:18:43 +04:00
for (size_t i = 0; i < line_buf_length-1; ++i)
2023-12-02 17:34:17 +04:00
if (line[i]>>4 == 0x3) {
if (current_calibration_value == 0)
current_calibration_value = (line[i]&0xf) * 10;
last_digit = line[i]&0xf;
} else {
2023-12-06 05:18:43 +04:00
uint8_t digit = 0;
2023-12-02 17:34:17 +04:00
switch (line[i]) {
case 'o': // 1
2023-12-06 05:18:43 +04:00
if (i+2 <= line_buf_length)
if (line[i+1] == 'n' && line[i+2] == 'e')
2023-12-02 17:34:17 +04:00
digit = 1;
break;
case 't': // 2 3
2023-12-06 05:18:43 +04:00
if (i+2 <= line_buf_length)
2023-12-02 17:34:17 +04:00
if (line[i+1] == 'w' && line[i+2] == 'o') {
digit = 2;
break;
}
2023-12-06 05:18:43 +04:00
if (i+4 <= line_buf_length)
if (line[i+1] == 'h' && line[i+2] == 'r' && line[i+3] == 'e' && line[i+4] == 'e')
2023-12-02 17:34:17 +04:00
digit = 3;
break;
case 'f': // 4 5
2023-12-06 05:18:43 +04:00
if (i+3 <= line_buf_length)
2023-12-02 17:34:17 +04:00
if (line[i+1] == 'o' && line[i+2] == 'u' && line[i+3] == 'r') {
digit = 4;
break;
}
2023-12-06 05:18:43 +04:00
if (i+3 <= line_buf_length)
if (line[i+1] == 'i' && line[i+2] == 'v' && line[i+3] == 'e')
2023-12-02 17:34:17 +04:00
digit = 5;
break;
case 's': // 6 7
2023-12-06 05:18:43 +04:00
if (i+2 <= line_buf_length)
2023-12-02 17:34:17 +04:00
if (line[i+1] == 'i' && line[i+2] == 'x') {
digit = 6;
break;
}
2023-12-06 05:18:43 +04:00
if (i+4 <= line_buf_length)
if (line[i+1] == 'e' && line[i+2] == 'v' && line[i+3] == 'e' && line[i+4] == 'n')
2023-12-02 17:34:17 +04:00
digit = 7;
break;
case 'e': // 8
2023-12-06 05:18:43 +04:00
if (i+4 <= line_buf_length)
if (line[i+1] == 'i' && line[i+2] == 'g' && line[i+3] == 'h' && line[i+4] == 't')
2023-12-02 17:34:17 +04:00
digit = 8;
break;
case 'n': // 9
2023-12-06 05:18:43 +04:00
if (i+3 <= line_buf_length)
if (line[i+1] == 'i' && line[i+2] == 'n' && line[i+3] == 'e')
2023-12-02 17:34:17 +04:00
digit = 9;
break;
}
if (digit != 0) {
if (current_calibration_value == 0)
current_calibration_value = digit * 10;
last_digit = digit;
}
}
if (current_calibration_value != 0)
calibration_value += current_calibration_value + last_digit;
2023-12-06 05:18:43 +04:00
memset(line, 0, line_buf_length);
2023-12-02 17:34:17 +04:00
}
2023-12-06 05:18:43 +04:00
printf("%lu\n", calibration_value);
2023-12-02 17:34:17 +04:00
return 0;
}