Add aoc03.
This commit is contained in:
2
03-joltage/.gitignore
vendored
Normal file
2
03-joltage/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
aoc3
|
||||||
|
*.o
|
||||||
12
03-joltage/Makefile
Normal file
12
03-joltage/Makefile
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
all: test
|
||||||
|
|
||||||
|
.PHONY: test clean
|
||||||
|
|
||||||
|
aoc3: aoc3.c
|
||||||
|
cc -O2 -o $@ $<
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-rm -f aoc3
|
||||||
|
|
||||||
|
test: aoc3
|
||||||
|
./aoc3 < input01.txt
|
||||||
47
03-joltage/aoc3.c
Normal file
47
03-joltage/aoc3.c
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user