Files
aoc25/03-joltage/aoc3.c
2025-12-03 10:13:36 +01:00

48 lines
751 B
C

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(void)
{
uint64_t total = 0;
uint8_t max = 0;
uint8_t prev = 0;
int8_t after = -1;
while (1) {
char c = getchar();
if (c >= '0' && c <= '9') {
uint8_t digit = c - '0';
if (digit > max) {
prev = max;
max = digit;
after = -1;
} else {
if (digit > after) {
after = digit;
}
}
} else if (c == '\n' || c == EOF) {
if (after == -1) {
// special case: max is the last digit
total += 10*prev + max;
} else {
total += 10*max + after;
}
max = 0;
prev = 0;
after = -1;
if (c == EOF) break;
} else {
fprintf(stderr, "weird character: %02x\n", c);
exit(1);
}
}
printf("%ld\n", total);
return 0;
}