Day1 C
This commit is contained in:
BIN
day1/c/day1
Executable file
BIN
day1/c/day1
Executable file
Binary file not shown.
118
day1/c/day1.c
Normal file
118
day1/c/day1.c
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define LINE_MAX_LENGTH 256
|
||||||
|
#define NUM_0_CHARCODE 48
|
||||||
|
#define NUM_9_CHARCODE NUM_0_CHARCODE + 9
|
||||||
|
#define PART_2 1
|
||||||
|
#define FORWARD 1
|
||||||
|
#define BACKWARD -1
|
||||||
|
|
||||||
|
char* english_digit_strings[] = {
|
||||||
|
"zero",
|
||||||
|
"one",
|
||||||
|
"two",
|
||||||
|
"three",
|
||||||
|
"four",
|
||||||
|
"five",
|
||||||
|
"six",
|
||||||
|
"seven",
|
||||||
|
"eight",
|
||||||
|
"nine",
|
||||||
|
};
|
||||||
|
|
||||||
|
#define NUM_OF_DIGITS ((sizeof(english_digit_strings)) / (sizeof(english_digit_strings[0])))
|
||||||
|
|
||||||
|
char *english_digit_strings_reversed[NUM_OF_DIGITS];
|
||||||
|
|
||||||
|
int get_num(char *p, int dir) {
|
||||||
|
// Check whether it is a number
|
||||||
|
if (*p >= NUM_0_CHARCODE && *p <= NUM_9_CHARCODE) {
|
||||||
|
return *p - NUM_0_CHARCODE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check spelled out numbers
|
||||||
|
if (PART_2) {
|
||||||
|
// Check each digit
|
||||||
|
for (int i = 0; i < NUM_OF_DIGITS; i++) {
|
||||||
|
char *reference = dir == FORWARD ? english_digit_strings[i] : english_digit_strings_reversed[i];
|
||||||
|
char *tmp = p;
|
||||||
|
int match = 1;
|
||||||
|
while (*reference != '\0') {
|
||||||
|
if (*tmp != *reference) {
|
||||||
|
match = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
reference++;
|
||||||
|
tmp += dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (match) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* reverse_string(char* str) {
|
||||||
|
char *p = str;
|
||||||
|
while (*p != '\0') p++;
|
||||||
|
|
||||||
|
int len = p - str;
|
||||||
|
|
||||||
|
char *newstr = (char *)malloc(len + 1);
|
||||||
|
memset(newstr, 0, len + 1);
|
||||||
|
p--;
|
||||||
|
|
||||||
|
|
||||||
|
char* newp = newstr;
|
||||||
|
while (p != str) {
|
||||||
|
*newp = *p;
|
||||||
|
newp++;
|
||||||
|
p--;
|
||||||
|
}
|
||||||
|
*newp = *p;
|
||||||
|
|
||||||
|
return newstr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void create_reversed_digit_strings() {
|
||||||
|
for (int i = 0; i < NUM_OF_DIGITS; i++) {
|
||||||
|
english_digit_strings_reversed[i] = reverse_string(english_digit_strings[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char *p, *buf, c;
|
||||||
|
int sum = 0, n = -1;
|
||||||
|
create_reversed_digit_strings();
|
||||||
|
|
||||||
|
buf = (char *)malloc(LINE_MAX_LENGTH);
|
||||||
|
memset(buf, 0, LINE_MAX_LENGTH);
|
||||||
|
p = buf;
|
||||||
|
|
||||||
|
while ((c = getchar()) != EOF) {
|
||||||
|
*p++ = c;
|
||||||
|
if (c == '\n') {
|
||||||
|
// Find last number (p points to the end of line)
|
||||||
|
while ((n = get_num(p, BACKWARD)) == -1) p--;
|
||||||
|
sum += n;
|
||||||
|
// Find first number
|
||||||
|
p = buf;
|
||||||
|
while ((n = get_num(p, FORWARD)) == -1) p++;
|
||||||
|
sum += n * 10;
|
||||||
|
|
||||||
|
memset(buf, 0, LINE_MAX_LENGTH);
|
||||||
|
p = buf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(buf);
|
||||||
|
for (int i = 0; i < NUM_OF_DIGITS; i++) {
|
||||||
|
free(english_digit_strings_reversed[i]);
|
||||||
|
}
|
||||||
|
printf("%i\n", sum);
|
||||||
|
}
|
||||||
1000
day1/input.txt
Normal file
1000
day1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
4
day1/sample.txt
Normal file
4
day1/sample.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
1abc2
|
||||||
|
pqr3stu8vwx
|
||||||
|
a1b2c3d4e5f
|
||||||
|
treb7uchet
|
||||||
7
day1/sample2.txt
Normal file
7
day1/sample2.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
two1nine
|
||||||
|
eightwothree
|
||||||
|
abcone2threexyz
|
||||||
|
xtwone3four
|
||||||
|
4nineeightseven2
|
||||||
|
zoneight234
|
||||||
|
7pqrstsixteen
|
||||||
Reference in New Issue
Block a user