diff --git a/03-joltage/.gitignore b/03-joltage/.gitignore new file mode 100644 index 0000000..945bf38 --- /dev/null +++ b/03-joltage/.gitignore @@ -0,0 +1,2 @@ +aoc3 +*.o diff --git a/03-joltage/Makefile b/03-joltage/Makefile new file mode 100644 index 0000000..412638e --- /dev/null +++ b/03-joltage/Makefile @@ -0,0 +1,12 @@ +all: test + +.PHONY: test clean + +aoc3: aoc3.c + cc -O2 -o $@ $< + +clean: + -rm -f aoc3 + +test: aoc3 + ./aoc3 < input01.txt diff --git a/03-joltage/aoc3.c b/03-joltage/aoc3.c new file mode 100644 index 0000000..c30b658 --- /dev/null +++ b/03-joltage/aoc3.c @@ -0,0 +1,47 @@ +#include +#include +#include + +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; +} diff --git a/03-joltage/input.txt b/03-joltage/input01.txt similarity index 100% rename from 03-joltage/input.txt rename to 03-joltage/input01.txt