Init commit with a complete day 1.
This commit is contained in:
commit
83ae802789
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
bin
|
18
Makefile
Normal file
18
Makefile
Normal file
@ -0,0 +1,18 @@
|
||||
CC=cc
|
||||
|
||||
CFLAGS=-static --std=c2x -O3 -Wall -Werror -Wextra -pedantic
|
||||
|
||||
EXES = $(wildcard day*)
|
||||
|
||||
all: create_bin_dir $(EXES)
|
||||
|
||||
create_bin_dir:
|
||||
@mkdir -p bin
|
||||
|
||||
day1: day1/p1.c day1/p2.c
|
||||
${CC} ${CFLAGS} $@/p1.c -o bin/$@p1
|
||||
${CC} ${CFLAGS} $@/p2.c -o bin/$@p2
|
||||
|
||||
|
||||
clean:
|
||||
rm -f bin/day*p*
|
1000
day1/in.my
Normal file
1000
day1/in.my
Normal file
File diff suppressed because it is too large
Load Diff
4
day1/in.test
Normal file
4
day1/in.test
Normal file
@ -0,0 +1,4 @@
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
7
day1/in.test2
Normal file
7
day1/in.test2
Normal file
@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
42
day1/p1.c
Normal file
42
day1/p1.c
Normal file
@ -0,0 +1,42 @@
|
||||
#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;
|
||||
size_t line_length = 0;
|
||||
|
||||
if (argc == 1 || argv[1][0] == '-')
|
||||
input = stdin;
|
||||
else if ((input = fopen(argv[1], "r")) == NULL) {
|
||||
printf("Cannot open file %s\n", argv[1]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int32_t calibration_value = 0;
|
||||
|
||||
while (getline(&line, &line_length, input) != -1) {
|
||||
int32_t current_calibration_value = -1;
|
||||
int8_t last_digit = 0;
|
||||
|
||||
for (size_t i = 0; i < line_length-1; ++i) {
|
||||
if (line[i]>>4 == 0x3) {
|
||||
if (current_calibration_value == -1)
|
||||
current_calibration_value = (line[i]&0xf) * 10;
|
||||
last_digit = line[i]&0xf;
|
||||
}
|
||||
}
|
||||
|
||||
if (current_calibration_value != -1)
|
||||
calibration_value += current_calibration_value + last_digit;
|
||||
|
||||
memset(line, 0, line_length);
|
||||
}
|
||||
|
||||
printf("%d\n", calibration_value);
|
||||
|
||||
return 0;
|
||||
}
|
103
day1/p2.c
Normal file
103
day1/p2.c
Normal file
@ -0,0 +1,103 @@
|
||||
#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;
|
||||
size_t line_length = 0;
|
||||
|
||||
if (argc == 1 || argv[1][0] == '-')
|
||||
input = stdin;
|
||||
else if ((input = fopen(argv[1], "r")) == NULL) {
|
||||
printf("Cannot open file %s\n", argv[1]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int32_t calibration_value = 0;
|
||||
|
||||
while (getline(&line, &line_length, input) != -1) {
|
||||
int32_t current_calibration_value = 0;
|
||||
int8_t last_digit = 0;
|
||||
|
||||
for (size_t i = 0; i < line_length-1; ++i) {
|
||||
if (line[i]>>4 == 0x3) {
|
||||
if (current_calibration_value == 0)
|
||||
current_calibration_value = (line[i]&0xf) * 10;
|
||||
last_digit = line[i]&0xf;
|
||||
} else {
|
||||
int8_t digit = 0;
|
||||
switch (line[i]) {
|
||||
case 'o': // 1
|
||||
if (i+2 <= line_length)
|
||||
if (line[i+1] == 'n' && line[i+2] == 'e') {
|
||||
digit = 1;
|
||||
}
|
||||
break;
|
||||
case 't': // 2 3
|
||||
if (i+2 <= line_length)
|
||||
if (line[i+1] == 'w' && line[i+2] == 'o') {
|
||||
digit = 2;
|
||||
break;
|
||||
}
|
||||
if (i+4 <= line_length)
|
||||
if (line[i+1] == 'h' && line[i+2] == 'r' && line[i+3] == 'e' && line[i+4] == 'e') {
|
||||
digit = 3;
|
||||
}
|
||||
break;
|
||||
case 'f': // 4 5
|
||||
if (i+3 <= line_length)
|
||||
if (line[i+1] == 'o' && line[i+2] == 'u' && line[i+3] == 'r') {
|
||||
digit = 4;
|
||||
break;
|
||||
}
|
||||
if (i+3 <= line_length)
|
||||
if (line[i+1] == 'i' && line[i+2] == 'v' && line[i+3] == 'e') {
|
||||
digit = 5;
|
||||
}
|
||||
break;
|
||||
case 's': // 6 7
|
||||
if (i+2 <= line_length)
|
||||
if (line[i+1] == 'i' && line[i+2] == 'x') {
|
||||
digit = 6;
|
||||
break;
|
||||
}
|
||||
if (i+4 <= line_length)
|
||||
if (line[i+1] == 'e' && line[i+2] == 'v' && line[i+3] == 'e' && line[i+4] == 'n') {
|
||||
digit = 7;
|
||||
}
|
||||
break;
|
||||
case 'e': // 8
|
||||
if (i+4 <= line_length)
|
||||
if (line[i+1] == 'i' && line[i+2] == 'g' && line[i+3] == 'h' && line[i+4] == 't') {
|
||||
digit = 8;
|
||||
}
|
||||
break;
|
||||
case 'n': // 9
|
||||
if (i+3 <= line_length)
|
||||
if (line[i+1] == 'i' && line[i+2] == 'n' && line[i+3] == 'e') {
|
||||
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;
|
||||
|
||||
memset(line, 0, line_length);
|
||||
}
|
||||
|
||||
printf("%d\n", calibration_value);
|
||||
|
||||
return 0;
|
||||
}
|
23
run
Executable file
23
run
Executable file
@ -0,0 +1,23 @@
|
||||
#!/usr/bin/sh
|
||||
|
||||
DAY=1
|
||||
if [ -n "$1" ]; then
|
||||
DAY=$1
|
||||
fi
|
||||
|
||||
PART=1
|
||||
if [ -n "$2" ]; then
|
||||
PART=$2
|
||||
fi
|
||||
|
||||
INPUT=my
|
||||
if [ -n "$3" ]; then
|
||||
INPUT=$3
|
||||
fi
|
||||
|
||||
mkdir -p bin
|
||||
|
||||
set -e
|
||||
|
||||
echo -n "Day $DAY part $PART with '$INPUT' input: "
|
||||
bin/day${DAY}p$PART < day$DAY/in.$INPUT
|
8
test
Executable file
8
test
Executable file
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/sh
|
||||
|
||||
test_case() {
|
||||
[[ $(bin/day$1p$2 < day$1/in.$3) = "$4" ]] && { echo -ne "\e[32mpassed\e[0m"; } || { echo -ne "\e[31mfailed\e[0m"; }
|
||||
}
|
||||
|
||||
echo "Day $1 with test data part 1 $(test_case $1 1 "test" "142"), and part 2 $(test_case $1 2 "test2" "281").";
|
||||
echo "Day $1 with my data part 1 $(test_case $1 1 "my" "55621"), and part 2 $(test_case $1 2 "my" "53592").";
|
Loading…
Reference in New Issue
Block a user