day 1 and 2

This commit is contained in:
Duy Truong 2023-12-02 00:01:25 -08:00
commit 498953170e
9 changed files with 1292 additions and 0 deletions

1000
01/input.txt Normal file

File diff suppressed because it is too large Load Diff

63
01/main.c Normal file
View File

@ -0,0 +1,63 @@
#include <stdio.h>
#define MAX_LENGTH 52
int convert(char* c) {
switch (*c) {
case 'z':
return (c[1] == 'e' && c[2] == 'r' && c[3] == 'o') ? 0 : -1;
case 'o':
return (c[1] == 'n' && c[2] == 'e') ? 1 : -1;
case 't':
if (c[1] == 'w' && c[2] == 'o')
return 2;
else if (c[1] == 'h' && c[2] == 'r' && c[3] == 'e' && c[4] == 'e')
return 3;
else
return -1;
case 'f':
if (c[1] == 'o' && c[2] == 'u' && c[3] == 'r')
return 4;
else if (c[1] == 'i' && c[2] == 'v' && c[3] == 'e')
return 5;
else
return -1;
case 's':
if (c[1] == 'i' && c[2] == 'x')
return 6;
else if (c[1] == 'e' && c[2] == 'v' && c[3] == 'e' && c[4] == 'n')
return 7;
else
return -1;
case 'e':
return (c[1] == 'i' && c[2] == 'g' && c[3] == 'h' && c[4] == 't') ? 8 : -1;
case 'n':
return (c[1] == 'i' && c[2] == 'n' && c[3] == 'e') ? 9 : -1;
}
return -1;
}
int main(void) {
int num1, num2, sum, word_num;
char buf[MAX_LENGTH];
num1 = 0;
num2 = 0;
sum = 0;
while (fgets(buf, MAX_LENGTH, stdin)) {
for (char* c = buf; *c != '\n'; c++) {
if (*c >= '0' && *c <= '9') {
num1 = num1 == 0 ? *c - '0' : num1;
num2 = *c - '0';
} else if ((word_num = convert(c)) >= 0) {
num1 = num1 == 0 ? word_num : num1;
num2 = word_num;
}
}
sum += num1*10 + num2;
num1 = 0;
}
printf("%d\n", sum);
return 0;
}

20
01/main1.c Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
int main(void) {
int c, num1, num2, sum;
num1 = 0;
num2 = 0;
sum = 0;
while ((c = fgetc(stdin)) != EOF) {
if (c >= '0' && c <= '9') {
num1 = num1 == 0 ? c - '0' : num1;
num2 = c - '0';
} else if (c == '\n') {
sum += num1*10 + num2;
num1 = 0;
}
}
printf("%d\n", sum);
return 0;
}

12
01/part1.sed Normal file
View File

@ -0,0 +1,12 @@
s/one/o1e/g
s/two/t2o/g
s/three/t3e/g
s/four/4/g
s/five/5e/g
s/six/6/g
s/seven/7n/g
s/eight/e8t/g
s/nine/n9e/g
s/[^0-9]//g
s/^(.).*(.)$/\1\2/g
s/^(.)$/\1\1/g

7
01/test.txt Normal file
View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

100
02/input.txt Normal file
View File

@ -0,0 +1,100 @@
Game 1: 4 blue, 7 red, 5 green; 3 blue, 4 red, 16 green; 3 red, 11 green
Game 2: 20 blue, 8 red, 1 green; 1 blue, 2 green, 8 red; 9 red, 4 green, 18 blue; 2 green, 7 red, 2 blue; 10 blue, 2 red, 5 green
Game 3: 2 red, 5 green, 1 blue; 3 blue, 5 green; 8 blue, 13 green, 2 red; 9 green, 3 blue; 12 green, 13 blue; 3 green, 3 blue, 1 red
Game 4: 1 red, 6 green, 4 blue; 3 green, 1 blue, 1 red; 7 blue, 1 red, 2 green
Game 5: 2 green, 9 blue, 1 red; 3 green, 1 blue, 3 red; 1 red, 4 blue, 9 green
Game 6: 2 blue, 5 red, 7 green; 5 blue, 8 red, 3 green; 2 red, 9 blue, 2 green
Game 7: 7 green, 7 blue, 2 red; 2 red, 7 green, 16 blue; 17 blue, 3 green, 3 red; 2 blue, 5 green, 3 red
Game 8: 4 red, 3 green; 9 green, 2 red, 2 blue; 1 red, 3 blue, 6 green
Game 9: 5 red, 3 green, 13 blue; 11 red, 15 blue, 1 green; 7 red, 2 blue
Game 10: 15 red, 3 green; 7 green, 4 blue, 11 red; 13 red, 13 blue; 2 blue, 5 green, 8 red
Game 11: 7 red, 3 green; 7 blue, 16 red, 4 green; 6 green, 6 blue, 12 red; 11 red, 4 green, 4 blue; 10 red, 6 blue, 2 green; 3 green, 7 red, 6 blue
Game 12: 1 blue, 2 red; 2 green, 15 blue; 6 green, 5 blue; 6 blue, 4 green; 5 blue, 3 green; 3 red, 3 blue, 10 green
Game 13: 10 red, 4 green; 9 red, 2 blue, 3 green; 6 red, 7 green, 1 blue; 9 red, 7 green, 1 blue; 3 blue; 3 blue, 3 red, 8 green
Game 14: 12 blue, 3 red, 4 green; 3 green, 1 red; 6 green, 16 blue
Game 15: 2 green, 3 red, 2 blue; 14 blue, 1 red, 17 green; 13 blue, 11 green, 10 red; 5 green, 7 red, 5 blue; 2 green, 3 blue, 6 red; 9 green, 2 blue, 5 red
Game 16: 2 blue, 1 red; 1 red, 2 green, 3 blue; 4 green, 9 blue, 3 red; 1 green, 4 red, 8 blue; 7 blue, 11 red, 1 green
Game 17: 9 green, 8 blue, 6 red; 8 red, 18 green, 1 blue; 18 red, 19 green, 1 blue
Game 18: 1 green, 4 red, 5 blue; 10 green, 8 blue; 12 green, 10 blue
Game 19: 3 red, 11 green, 12 blue; 16 green, 1 red, 20 blue; 9 green, 2 red, 14 blue; 5 blue, 2 green, 2 red; 20 blue, 3 red, 10 green; 4 green, 3 blue
Game 20: 17 red, 3 blue, 9 green; 6 green, 1 red, 7 blue; 6 red, 2 blue; 1 blue, 4 green, 5 red; 6 green, 5 red; 10 blue, 11 green, 2 red
Game 21: 9 red, 4 blue, 6 green; 14 red, 9 green; 1 red, 1 blue, 12 green
Game 22: 5 green, 4 red; 1 green, 1 red, 2 blue; 5 red, 4 green, 4 blue; 2 green, 2 blue, 5 red; 8 green, 4 blue, 16 red; 15 red, 3 green
Game 23: 5 green, 14 red; 6 blue, 2 green, 14 red; 4 blue, 8 red, 4 green; 4 blue, 9 red, 8 green; 9 blue, 3 green
Game 24: 13 blue, 9 green, 13 red; 11 blue, 14 red, 10 green; 12 green, 5 blue, 14 red
Game 25: 11 green, 1 blue; 12 red, 8 green, 5 blue; 1 blue, 8 green, 6 red
Game 26: 4 blue, 1 green; 1 green, 5 red, 6 blue; 8 green, 5 blue, 6 red; 2 blue, 2 red, 8 green; 8 green, 2 red, 4 blue; 7 red, 2 blue, 7 green
Game 27: 8 red, 1 blue, 8 green; 5 red, 2 green; 2 blue, 9 green, 9 red; 2 blue
Game 28: 2 green, 1 blue; 2 green; 1 blue; 1 blue, 1 red; 1 blue; 1 green
Game 29: 12 red, 8 green, 13 blue; 13 green, 15 red; 12 red, 18 green, 10 blue; 7 green, 20 red, 5 blue; 20 red, 7 green, 10 blue; 9 green, 13 blue
Game 30: 5 red, 3 blue; 2 red; 2 green, 6 blue, 7 red; 5 red
Game 31: 14 red, 7 blue, 2 green; 1 green, 11 red, 9 blue; 3 red, 2 green, 5 blue; 1 green, 9 blue, 8 red; 8 blue, 8 red, 1 green
Game 32: 2 green, 6 blue, 2 red; 2 blue, 4 red; 1 green, 9 blue, 1 red; 3 red, 13 blue, 1 green
Game 33: 6 green, 8 blue, 7 red; 3 blue, 1 green, 8 red; 6 red, 11 blue; 10 blue, 3 red, 7 green; 1 blue, 3 red, 6 green
Game 34: 1 red, 1 blue, 8 green; 5 blue, 10 red, 11 green; 2 green, 10 red, 2 blue
Game 35: 2 blue, 15 green; 3 red, 3 blue, 6 green; 13 green, 17 red, 3 blue; 18 green, 1 blue, 18 red; 16 green, 3 blue; 11 green, 15 red
Game 36: 16 red, 4 green, 1 blue; 8 red, 2 blue, 5 green; 5 green, 2 blue, 9 red
Game 37: 3 green, 7 blue; 8 blue, 5 red, 6 green; 5 blue, 1 red, 13 green
Game 38: 6 green, 6 blue; 11 blue, 8 green, 1 red; 5 blue, 16 green
Game 39: 2 red, 4 blue, 5 green; 1 red, 2 green, 8 blue; 16 green, 15 blue, 2 red; 6 green, 16 blue, 1 red; 16 green, 18 blue, 1 red
Game 40: 3 green, 6 blue, 7 red; 1 blue, 17 red; 4 green, 6 red; 13 red
Game 41: 6 red, 5 green, 6 blue; 4 green, 2 blue; 6 red, 1 blue, 4 green; 4 blue, 13 green; 3 blue, 2 red; 2 blue, 5 red, 3 green
Game 42: 8 red, 5 blue; 15 blue, 13 red, 3 green; 6 red, 18 blue, 4 green
Game 43: 5 red, 1 green, 1 blue; 2 red, 2 green, 3 blue; 4 blue, 3 red, 1 green
Game 44: 6 blue, 12 green; 7 blue, 12 red, 11 green; 12 green, 2 blue, 13 red; 8 green, 8 blue, 12 red
Game 45: 18 blue, 15 red, 8 green; 17 red, 3 blue; 1 green, 2 red, 15 blue
Game 46: 3 blue, 2 green, 5 red; 11 blue, 2 green, 19 red; 3 green, 19 red, 13 blue
Game 47: 9 green, 2 red; 7 red, 10 green; 2 blue, 9 green, 1 red; 5 blue
Game 48: 8 blue, 8 green; 1 red, 17 green; 9 green, 6 red, 8 blue; 13 green, 3 red, 1 blue
Game 49: 17 blue, 2 red, 1 green; 12 blue, 1 green, 4 red; 1 green, 2 red, 13 blue
Game 50: 4 red, 2 blue, 9 green; 8 green, 2 blue, 6 red; 9 green, 2 blue, 14 red
Game 51: 6 red, 3 green, 8 blue; 5 green, 16 blue, 1 red; 2 green, 13 red, 14 blue; 14 red, 12 green, 19 blue; 19 blue, 13 green, 9 red; 6 red, 15 blue, 7 green
Game 52: 18 blue, 2 red, 5 green; 2 green, 5 red; 6 red, 10 green, 3 blue; 3 green, 6 blue, 6 red
Game 53: 11 red, 4 green; 2 blue, 3 red; 3 blue, 13 red, 11 green; 11 blue, 8 red, 5 green
Game 54: 4 green, 1 red, 7 blue; 4 green, 8 red, 8 blue; 4 red, 5 green; 8 blue, 4 green, 2 red; 4 green, 3 blue; 3 blue, 3 green, 3 red
Game 55: 9 red, 1 green, 1 blue; 1 green, 8 red; 4 red; 7 blue, 7 green; 6 blue, 5 green, 6 red; 5 blue, 8 red, 4 green
Game 56: 1 blue; 3 red, 2 blue; 1 red, 2 green
Game 57: 7 green, 2 red, 5 blue; 6 green, 1 red; 1 green, 6 red; 1 red, 20 green; 1 green, 4 red, 2 blue; 15 green, 7 red
Game 58: 3 green, 8 red, 5 blue; 2 red, 3 green; 2 blue, 2 green, 12 red; 1 blue, 3 green, 16 red; 4 blue, 9 red, 3 green
Game 59: 2 red, 5 blue, 1 green; 2 red, 3 green; 12 red, 5 blue; 7 green, 3 blue, 4 red; 1 green, 5 blue, 14 red; 8 red, 11 green, 2 blue
Game 60: 12 blue, 3 red, 2 green; 2 green, 6 blue, 1 red; 1 blue, 2 red, 3 green; 7 green, 1 blue, 2 red
Game 61: 6 blue, 6 red, 7 green; 2 green, 5 red, 5 blue; 1 blue, 3 green, 15 red; 6 blue, 8 green, 14 red
Game 62: 1 blue, 6 red, 2 green; 5 green, 5 red, 11 blue; 5 red, 6 green, 8 blue; 2 green, 17 blue; 2 red, 7 green, 5 blue; 3 blue, 5 green, 8 red
Game 63: 6 red, 1 green, 9 blue; 7 red, 1 green, 11 blue; 3 green, 4 red; 4 green, 10 blue, 7 red; 13 blue, 11 green, 5 red; 14 green
Game 64: 13 green, 11 red, 1 blue; 1 red, 2 green; 3 blue, 9 green, 19 red
Game 65: 2 blue, 11 red, 3 green; 5 green, 6 red; 2 blue, 9 green, 9 red; 1 green, 5 blue, 3 red; 4 red, 4 blue, 6 green; 2 blue, 7 green, 1 red
Game 66: 4 red, 7 blue, 3 green; 1 green, 6 blue, 7 red; 1 green, 1 red, 1 blue
Game 67: 1 green, 8 red; 4 green, 1 blue, 3 red; 8 red, 3 green
Game 68: 3 blue, 4 red; 1 blue, 1 green; 2 blue, 6 red, 3 green; 1 blue, 1 green, 3 red; 7 red, 1 blue, 4 green; 1 green, 2 red, 3 blue
Game 69: 6 green, 2 blue, 3 red; 3 blue, 3 red; 1 green; 1 blue, 2 red, 8 green; 1 green, 1 red
Game 70: 7 blue, 15 green, 3 red; 8 green, 6 blue, 5 red; 7 blue, 1 red, 3 green
Game 71: 4 green, 3 blue, 7 red; 6 red, 6 green, 10 blue; 3 red, 9 green; 7 blue, 1 red, 13 green; 3 blue, 5 red, 11 green; 8 blue, 8 red, 5 green
Game 72: 10 green, 4 blue; 4 blue, 8 green, 2 red; 2 red, 6 green, 6 blue; 1 red, 5 blue; 13 green, 5 blue; 8 green, 3 blue, 2 red
Game 73: 9 blue, 1 red, 13 green; 2 red, 16 green, 6 blue; 1 red, 8 blue, 17 green; 7 green, 1 blue; 8 blue, 1 green, 1 red
Game 74: 2 green, 2 red; 1 red, 5 blue; 7 blue, 3 green; 7 blue, 3 green, 7 red
Game 75: 3 green, 5 blue; 2 green, 1 red, 9 blue; 17 green, 13 blue, 3 red; 3 blue, 2 red, 8 green; 7 green, 2 red, 8 blue; 1 green, 14 blue
Game 76: 19 red; 2 blue, 20 red; 3 blue, 3 red; 20 red, 3 blue; 6 red, 4 blue, 1 green
Game 77: 2 red, 5 green; 2 red, 2 green; 4 green; 4 green, 3 red, 3 blue; 2 red
Game 78: 4 green, 16 red; 5 green, 2 red, 2 blue; 4 green, 2 blue, 11 red; 1 blue, 1 green, 6 red; 2 blue, 7 red
Game 79: 8 blue, 2 green; 3 red, 3 green; 3 red, 9 blue, 4 green; 1 red, 2 blue, 4 green; 8 green, 6 red, 9 blue; 2 red, 10 blue, 9 green
Game 80: 9 red, 17 blue, 2 green; 5 red, 1 green, 6 blue; 2 red, 20 blue; 6 red, 12 blue
Game 81: 5 red, 4 blue, 1 green; 15 green, 8 blue, 2 red; 5 blue, 2 red, 9 green; 11 green, 1 blue, 3 red; 15 green, 1 red, 3 blue
Game 82: 2 blue, 12 green; 12 blue, 12 green, 14 red; 4 blue, 16 green, 7 red
Game 83: 6 blue, 7 red, 11 green; 2 red, 6 green, 4 blue; 6 blue, 1 red; 7 blue, 12 red, 13 green; 10 green, 6 blue, 10 red; 6 red, 4 green
Game 84: 2 green, 5 red, 1 blue; 4 green, 3 blue, 2 red; 2 green, 1 red, 1 blue; 5 red, 4 blue, 4 green
Game 85: 1 blue; 1 green, 2 red; 3 red, 11 green; 6 green, 14 red, 1 blue
Game 86: 3 green, 1 blue, 3 red; 3 red, 6 blue, 2 green; 4 blue, 1 red; 5 blue, 4 green, 3 red; 2 blue, 3 red, 4 green; 7 blue, 2 green, 3 red
Game 87: 1 green, 5 red, 5 blue; 6 red, 4 green, 1 blue; 2 green, 4 red, 1 blue; 7 red, 4 green, 5 blue; 3 green, 4 red, 1 blue
Game 88: 3 blue, 18 red, 14 green; 11 red, 14 green; 2 blue, 10 red, 4 green
Game 89: 5 red, 4 green; 3 red, 2 blue, 1 green; 2 blue, 4 green, 3 red; 2 green, 2 blue, 2 red
Game 90: 14 blue, 10 red, 2 green; 11 blue, 3 red, 1 green; 5 blue, 2 green, 14 red
Game 91: 9 blue, 4 red, 4 green; 4 red, 1 blue; 3 blue, 20 red
Game 92: 3 red, 2 green, 7 blue; 2 green, 10 red, 8 blue; 9 red, 5 blue, 5 green; 1 blue, 2 green, 3 red; 10 red, 13 blue, 9 green; 11 blue, 7 red
Game 93: 9 red, 2 blue, 1 green; 6 red, 2 blue, 11 green; 1 green, 1 blue, 10 red; 9 red, 8 green
Game 94: 18 green, 3 red; 2 blue, 4 green, 12 red; 5 red, 1 blue, 13 green; 2 blue, 15 green, 7 red
Game 95: 12 green; 1 red, 3 green, 1 blue; 13 green, 2 red, 1 blue; 9 green; 2 green, 1 blue; 1 blue, 4 green, 1 red
Game 96: 5 red, 4 green, 2 blue; 10 red, 3 blue, 5 green; 14 blue, 11 green, 4 red; 14 green, 7 blue, 13 red; 17 red, 9 green, 6 blue; 8 red, 4 blue, 13 green
Game 97: 3 green, 7 blue; 7 red, 4 blue; 5 blue, 6 red, 2 green
Game 98: 9 green; 8 green, 4 blue; 6 blue, 2 red, 1 green; 4 green, 1 blue; 5 blue, 2 green, 2 red
Game 99: 3 red, 1 green, 5 blue; 1 red; 3 blue, 4 red; 3 blue, 1 green, 5 red
Game 100: 3 red, 3 blue, 10 green; 3 green, 1 blue, 6 red; 5 red, 4 green, 7 blue

45
02/part1.c Normal file
View File

@ -0,0 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 165
#define RED 1
#define GREEN 2
#define BLUE 3
int strtocolor(char* c, char** end) {
if (c[0] == 'r' && c[1] == 'e' && c[2] == 'd') {
*end = c + 3;
return RED;
} else if (c[0] == 'g' && c[1] == 'r' && c[2] == 'e' && c[3] == 'e' && c[4] == 'n') {
*end = c + 5;
return GREEN;
} else if (c[0] == 'b' && c[1] == 'l' && c[2] == 'u' && c[3] == 'e') {
*end = c + 4;
return BLUE;
}
abort();
}
int main(void) {
char buf[MAX_LENGTH];
int game_num, color_num, color, sum;
sum = 0;
while (fgets(buf, MAX_LENGTH, stdin)) {
char* c = buf;
game_num = strtol(c + 5, &c, 10); // +5 skips the "Game "
while (*c != '\n') {
color_num = strtol(c + 2, &c, 10); // +2 skips ": ", ", ", or "; "
color = strtocolor(c + 1, &c); // +1 skips the space
if ((color == RED && color_num > 12) ||
(color == GREEN && color_num > 13) ||
(color == BLUE && color_num > 14)) {
game_num = 0;
}
}
sum += game_num;
}
printf("%d\n", sum);
return 0;
}

40
02/part2.c Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 165
char* skip_color(char* c) {
switch (*c) {
case 'r':
return c + 3;
case 'g':
return c + 5;
case 'b':
return c + 4;
}
abort();
}
int main(void) {
char buf[MAX_LENGTH];
int game_num, color_num, sum, max_r, max_g, max_b;
sum = 0;
while (fgets(buf, MAX_LENGTH, stdin)) {
char* c = buf;
game_num = strtol(c + 5, &c, 10); // +5 skips the "Game "
max_r = max_g = max_b = 0;
while (*c != '\n') {
color_num = strtol(c + 2, &c, 10); // +2 skips ": ", ", ", or "; "
c++; // +1 skips the space
max_r = (*c == 'r' && color_num > max_r) ? color_num : max_r;
max_g = (*c == 'g' && color_num > max_g) ? color_num : max_g;
max_b = (*c == 'b' && color_num > max_b) ? color_num : max_b;
c = skip_color(c);
}
sum += max_r * max_g * max_b;
}
printf("%d\n", sum);
return 0;
}

5
02/test.txt Normal file
View File

@ -0,0 +1,5 @@
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green