committing everything squashed

This commit is contained in:
Dory 2023-12-03 01:12:13 -08:00
commit 92caa0f5c4
15 changed files with 1587 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

29
02/part1.c Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 165
int main(void) {
char buf[MAX_LENGTH];
int game_num, cubes, 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') {
cubes = strtol(c + 2, &c, 10); // +2 skips ": ", ", ", or "; "
c++; // +1 skips the space
if ((*c == 'r' && cubes > 12) ||
(*c == 'g' && cubes > 13) ||
(*c == 'b' && cubes > 14)) {
game_num = 0;
}
for (; *c >= 'a' && *c <= 'z'; c++);
}
sum += game_num;
}
printf("%d\n", sum);
return 0;
}

7
02/part1.py Normal file
View File

@ -0,0 +1,7 @@
import re, sys
print(sum(
int(re.search("[0-9]+", line).group())
for line in sys.stdin.readlines()
if all(int(r) <= 12 for r in re.findall(r"[0-9]+(?= red)", line))
and all(int(g) <= 13 for g in re.findall(r"[0-9]+(?= green)", line))
and all(int(b) <= 14 for b in re.findall(r"[0-9]+(?= blue)", line))))

28
02/part2.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 165
int main(void) {
char buf[MAX_LENGTH];
int color_num, sum, max_r, max_g, max_b;
sum = 0;
while (fgets(buf, MAX_LENGTH, stdin)) {
char* c = buf + 5; // +5 skips the "Game "
for (; *c != ':'; c++);
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;
for (; *c >= 'a' && *c <= 'z'; c++);
}
sum += max_r * max_g * max_b;
}
printf("%d\n", sum);
return 0;
}

4
02/part2.py Normal file
View File

@ -0,0 +1,4 @@
import math, re, sys
print(sum(math.prod(max(int(n) for n in re.findall(f"[0-9]+(?= {color})", line))
for color in ("red", "green", "blue"))
for line in open("input.txt", "rt").readlines()))

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

140
03/input.txt Normal file
View File

@ -0,0 +1,140 @@
............................................411.....................363..134.........463.775..........................506...................
......429...836..$............../..960........*.............+..........*...=....381.....*........67......426.....=..../...304...............
.........*...&...641..........924..*.........855....492..495.......476.927.......*.........680...../.&....*.....713......*..................
.........900......................239.325..............*.............*.....953%.771...........#......808...329..........214........846%.....
.............707...233...................*.....*........774...445.....107.........................................@927......775.............
........721........*...17................790...670.............$..........893.93&../...129$.651.696.......131*99.............=......446*781.
...........*717.868...+.............252....................................*......461.........*......................350....................
....911..................*...454.............161.........875........209....695..............926.983.........758.592....*..........$.562.....
...*............569#..496........888.............227......*..67......*..................877........*...#.......*.......716......975....@....
...730........................$...#..112............*..509..*.......858..710.......@567..%..610..821...918..................................
.........794.....701@..456-...505.....*............884.....298...............................&...............742=.....95....................
............+........................891.165.......................=.............175...............$..693.............+...........127.......
....*..........554...855.................*...............505.262.110..764............953@...+.....343.../..............................817..
..449.464.148.........*.............1=.853.......232.356*....../.......&....................217................992..935.527............*....
..........*........931...44.........................................&..........553*..............*....945......$.......*............655.....
........735..718............16.....650.....944...................525.....*........................491.+...662..................27.......397.
...273........*.........563...+.......*418.%.......-.....#265..........43.92.....791*211..+................*..&...507@....84...*..543.......
..............562...=...........................627....*.....................*............492..*86......456..837............*........*181...
.....829.233.........454..................351.......232..........47........44.401.............8....876...........965*151..284...............
........*....................647..........*.....$.................#................................*....+788...*.....................-......
...49.....588........174......*....111....604....358......863..61.........................663.......549......442.271&.................493...
...........+..446.......*...638.......*..........................*..881................#.*....*.........................720....168%.........
.....504.........*..43..245........975.................91........28..*.....966......600..18...245.......480............*....................
.......*....+...57..*....................@412......812...*............729......61...................268*.............706...#................
.......307..107......937.108..................453......50......778........&......*440....................4......361.........888.=256........
..........................*..889*....89............675..........%.......29..427...................508..&........&...641..................455
..........897...960......403.....971...*......806.....@.363................*......9+..............*.....464...................586....282*...
.....316...*...............................9...#........./.............317..614........362..905..27................670.............$........
......*...606....................+...........................-............*........292.*.....@..........%.........=.......164.470.902..549..
..473.603.........733..482*423....276...419...../166.........297........401.......$....313......306/.353.....................*..........*...
...*....................................%...............291......997.........385..............................617%..470...............618...
....577...*903...%.-343.....790.................143.....*.........*...30........*................/759..$.....................@..............
...............387.........*.......$.............*.....210....417.482..........86.....760...............626........503.+990..299..137..-....
....*14....896.....=...922..937...188........553..63....................*150..........*........*...208...............@................338...
.........#....*..774..*.......................*......................987..........737.959......665.#....329...+.../....#.....966............
.......221....8........629...........521.94....706.....356...+............636........................=...*..253.655..505..............613...
............................649............................270....468.......*.....................420...848..................846.....-......
....43..718..678.............=.....*906....593.-26.....268...................810...401......+.........$.........842.........@..........&....
826*...*......*...430+..572.....436..........*.................954.....732........*.......391..999..348.662......*.....................771..
.....61.....535............*........977.......34....672.......*.........*.........693............*.............=.860.....*790.....611.......
........930.......&................*................=.......596.........590....*................903.....985.639.......984...........@.......
.......*.......691...379.......226..971.....549*............................438.466........./.......307.*.........75......151..594....631...
........761..........*..........................236....................534..........611......835..%...............&.........*.*.........*...
...275.........58.970............660-..605..........689.191...32...+..*....287$......%...........160........*625..........413......*133.885.
....*..................................*...........*......*.......873........................./.............................................
.....388..518.........313*820...666/.321.520...392..12....455.................165=...+.#....345........557..........293............+........
.............*.........................../......*..................................267..841...........@........49......*762.863...123.......
.............41......301.....247*37..........836........631*32..241&..........999.................+.....476&.....*.896.........*............
.../..................*..................794...........................950...*.....................436.......$.240.+.........108............
.917....$..+........72.....&....333.8*2...-.............35...809..416..-...654...750...166.................419.......=................884...
.....442..537...........865..................927*114.$........*...=...............*....*...725.....................228......385...214*......
..................@.........701.501..204.............320................/.......+..820.514....*.......520...../.............................
................121....$....*...+......@...725...@.....................305....698..........287..877......*....7................$.%..........
....920..790........468.......=............*.....584..........-546...............................*.....870............954....811.407........
....*.....*....................405..........724...............................143.................256.......668.............................
..877......381..........=.................................4.........../...........=495......617..................519........................
..................855....59..............$63..............*............73..773.=...........*.......595.......353*....937.......226..........
.....456.............@............966..........990....282..19.749....=....../..273.......860.......$...233..........*....+........*.464.....
......*..........*.......206..131.$...110.861.*........*...........159....................................&............624......566.........
...181...349......198....................*.....484.....989.....181............457...........................................................
........*...............942@..345....................+........................*......142..86..............282...........73..................
.....319..804*656.............*....................330..261......236.......263......*.............158.......*...........*........551..314...
...................343.......957.175=.....492.245*.....*............*306.......745.90.........165*..........910.......818..201..*...........
.............%....-.........................*.....186.188......306........*96..+......................837.......945.........@..129..........
..............742..........+.....619.........889..............#...................465.....542%..44....*.../418...*..........................
..................753-....973....*...$575.................883...../.....*....242..&.............*....63...........72..........*732....702...
.........640....................724.........63*844...........*...234.690.48....*.....+........612..............*.......483.............$....
...225.......@......................452..89........435.....754.................886....514...................209.282...*.....................
.....*.....346........................=.*..........................................................357*88............473...........712..+...
...84..&.......%.....616.......605&......380.....773..865.254.433......*...1*..-............33+.............283...................$......538
........416.....408.......744......................&.....*....*......595........586.....428..........523......*..............901............
.820@.......59*...................%625.....................560..........................*......705....*.....713..../.745........&...........
.......%.......849...$.......................*...........*.....574.....#526..............304......@..241.........241..../..........975..635.
....754.............269........327$..906/.133.33........400.....*...-.........540.........................299..................451*.....=...
...........409............589.......................533.........564.211........*.....592.....%..............*...............................
.............*...............*..915......321.........+......................938......#....809............756..396.......980.403..813.725*200
...916=.%223.354...227....488...*...........%.....@.....907.659...192.$866.......874.............737.........*...............*....*.........
.....................*...........966.257.......515.........*......*..........645....*..169......#.....*792...369......909....36..172.%119...
..779.................669............$...318......../.309+.....546..........*....817......*........755............634*......................
......441........%..........*40....6.............514.......323............464............501.84........*379....*........+.....339......$....
............641...735....355........&................14...*...........*.............53.......$..............386.577....697..............520.
................................................929+....288........408................*..417...192...............................899$.......
.....871..................736............749*.......................................24......*.*.....$67...............616.765.........@783..
......*........24........*.....$.............147...258*605.65*807......253.............450.....912......454.895.............*..#............
...612....957.*........245..790.....................................@.....*............*............@....@...=..91*..554....60.15...........
..........@....229..........................481.......542*650....779................520..........139..................*.....................
....*304.............815....................*...................................16.................................@.772......*506....%.....
.975........+....794.#........773*.......292..........708*......475............-............*100................600........862........439...
.........500......................884...............#.....477...........+...........*894.368.....480..-..552*...............................
.......................912..=............../......227............199..822........478.............*...742.....572.......625..................
.117.........*146..529...*.718.........121..974.....................*....................201.....47........+..............*.500.............
.......296.97......@...345................*......................976.......-..............%................851..........196.*....383....$...
...182......................39.....#600...973....*606.................950...448...............&.....&9.215...................849........303.
.......824...864*973.....%....................973......................*...........581...772..167.......+.......598.........................
..829+....*..............953.............................764..424......200....788....*....@........169......280....*..........373...711.....
.......147.........348.......#..744.730........355*..............=...............*..183.&...............808..*......147..........-.*........
....*........836...*......807..*....$...815........235.172/...............640..93........900..............=...291...................249.....
.452.213........*....698......569......%.......................320.......-.........%................................./......................
..........522....406.*............959...........383.......533.......706....47&......33.960......960....&...241.....620.......-764....906#...
..........*..........932............*..594.......*...711.&.....+794....................-..............487...............285.................
..139...737.....*541......658.248.79...........589.....-...460...........................-..145............696.........+......645.....=640..
.....*........24..........*....+......971*.................................397..&86.....95..*.................*..................*..........
...55...75.............258..4.....+.......183..........349.....&...169=....@................207..187.....929..601.746.....$...269...-.......
........*.......%577.........*543..149.71........337....*.....40.....................434..........%.........+......-.....277......43...638..
...230%..609......................................./.580.......................+.......*....+...........640............................*....
..............269..999................618...636..............311....109...314.586.....162...747..........*........56..967...............291.
...../...........#.=......217............*......................*..*........*......%......................596........%......629..901$.......
....103...................*....157.......427.................101..686..134.909....550...@.........911........./..579....167.............*...
.............181......-.822.......*................623...................*.............39..644.......*.....841.........&.............562.45.
.....+469...........89.........190...653...939.89..*......................898..............*......660..........272..@........%..614.........
............&.......................*.........*...563....215....................612*882..740.&................*......173...834....$....182..
......437...856........626..159...266.299...............*.............830$...................268.......467-.738.............................
.........%............*................$....@...........891..291................649.................................387...938.850*..........
...............922.610....*....676...........494.161%............%.................*154..741....*............326.....*............88........
....702.................223........+......$.......................224.......133.............@....648..890....../..540..745*............573..
.........&842....$..849........513..210.288............35.....758........../........................./................................./....
..............135..+............*...........607.........*........*...............790..#.....552/.............374......*496.....253..........
..........*..............804/.369....419..............22..576.....335..723.686........347..............-327...*....180......................
.......685.829.790@...................*....437............................*....................392.............352...............599........
.......................944..........667....*...321..764@.746.665.......+......890.......104................./..........746..668.-...........
96.+....=.............#.................662....*..........*...@..961...877...*.....942+..*......290...655.303.....467.....*..*........57....
....927.857........+................791........545..67......................152...........345..*.....*...............*...541..846...........
................325....966....480..................@...............462..468.....352.............96.371...............593................220.
........=.970.........*......*..........943.....+......*875..362.%....*....*...-.......................550*25...%191.........462............
.....984...+......&..618.39.493.289..21....*....379.600...........16.642..162....256........................................*....403........
...............168........*........*...*....326...............*...............*...+..............413.*.....+293.769*620....674..............
647.................949..........502...748..............692...208.......271..903..................=..132.........................506$..832..
..............&.........983....+................503....#.........................*350..239.....................581.......372...........*....
.....45........693.........*...192.....7./......+.............774.............338....................@81...337*...................249..105..
.........................598.......905....899......#..........*......-.............*31..........+......................751#......*..........
...../...............56..............*..............110....594.....517..300.679.150.......64.919......287.........................5...801...
....783....498..321.&...297.514.......8.......+18..........................*...............*....../.....*.....&.............................
..............$..*......*....-....................*................=...961.....534*791......526....499.648.....349..&.......................
........767......48....40.............524.28...369.417..432.......421..$...........................................748......40..............
..........%..........+............820....*.................#...................707.....378.........801.................52..*.....102%.......
..............822...364.551/......./.................118&.....922.....785........./.......*610........$..........%268..=...529..............
........820..*.........................../......753.......................................................499...................435.........
..232......&.676...738....#......839....876.45........866...555...664......+..68.......941.........51*585..............937.......*...917*691
.........@........*.......8...31*...........+.....577*.........*....*...399....*.=....@......./...................................59........
.......740.......781..........................................105.353........791.579...........900.463..............909.....................

71
03/part1.c Normal file
View File

@ -0,0 +1,71 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define LINELENGTH 150
#define NEXT(X) ((X + 3 + 1) % 3)
#define PREV(X) ((X + 3 - 1) % 3)
/* I wish I had python generator or Go coroutine... */
#define STATE_BEGIN 0
#define STATE_READING 1
#define STATE_END 2
int read_state = STATE_BEGIN;
/* Return at least 3 lines; first and last lines and first chars are all '.' */
char* readline(char* buf, int max_size) {
switch (read_state) {
case STATE_BEGIN:
memset(buf, '.', max_size);
buf[max_size - 2] = '\n';
buf[max_size - 1] = '\0';
read_state = STATE_READING;
return buf;
case STATE_READING:
buf[0] = '.';
if (!fgets(buf + 1, max_size, stdin)) {
memset(buf, '.', max_size);
buf[max_size - 2] = '\n';
buf[max_size - 1] = '\0';
read_state = STATE_END;
}
return buf;
case STATE_END:
return NULL;
}
}
#define IS_SYMBOL(X) ((X < '0' || X > '9') && X != '.' && X != '\n')
#define IS_ADJACENT_SYMBOL(buf, i, j) ( \
IS_SYMBOL(buf[i][j - 1]) || IS_SYMBOL(buf[i][j + 1]) || \
IS_SYMBOL(buf[PREV(i)][j]) || IS_SYMBOL(buf[NEXT(i)][j]) || \
IS_SYMBOL(buf[PREV(i)][j - 1]) || IS_SYMBOL(buf[PREV(i)][j + 1]) || \
IS_SYMBOL(buf[NEXT(i)][j - 1]) || IS_SYMBOL(buf[NEXT(i)][j + 1]))
int main(void) {
char buf[3][LINELENGTH];
int sum = 0;
readline(buf[0], LINELENGTH);
readline(buf[1], LINELENGTH);
for (int i = 1; readline(buf[NEXT(i)], LINELENGTH); i = NEXT(i)) {
bool is_part = false;
int num = 0;
for (int j = 1; buf[i][j]; j++) { // j = 0 is '.' guard
if (buf[i][j] >= '0' && buf[i][j] <= '9') {
num = num * 10 + (buf[i][j] - '0');
is_part = is_part || IS_ADJACENT_SYMBOL(buf, i, j);
} else {
sum += is_part ? num : 0;
is_part = false;
num = 0;
}
}
}
printf("%d\n", sum);
return 0;
}

91
03/part2.c Normal file
View File

@ -0,0 +1,91 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define LINELENGTH 150
#define NEXT(X) ((X + 3 + 1) % 3)
#define PREV(X) ((X + 3 - 1) % 3)
/* I wish I had python generator or Go coroutine... */
#define STATE_BEGIN 0
#define STATE_READING 1
#define STATE_END 2
int read_state = STATE_BEGIN;
/* Return at least 3 lines; first and last lines and first chars are all '.' */
char* readline(char* buf, int max_size) {
switch (read_state) {
case STATE_BEGIN:
memset(buf, '.', max_size);
buf[max_size - 2] = '\n';
buf[max_size - 1] = '\0';
read_state = STATE_READING;
return buf;
case STATE_READING:
buf[0] = '.';
if (!fgets(buf + 1, max_size, stdin)) {
memset(buf, '.', max_size);
buf[max_size - 2] = '\n';
buf[max_size - 1] = '\0';
read_state = STATE_END;
}
return buf;
case STATE_END:
return NULL;
}
}
#define IS_STAR(buf, i, j) (buf[i][j] == '*')
#define CHECK_STAR(buf, i, j, gear1, gear2, num) \
if (IS_STAR(buf, i, j)) { \
if (gear1[i][j] && gear1[i][j] != num) { \
gear2[i][j] = num; \
} else { \
gear1[i][j] = num; \
} \
}
int main(void) {
char buf[3][LINELENGTH];
int* gear1[3][LINELENGTH];
int* gear2[3][LINELENGTH];
memset(gear1, 0, sizeof(gear1));
memset(gear2, 0, sizeof(gear2));
int sum = 0;
int* num;
readline(buf[0], LINELENGTH);
readline(buf[1], LINELENGTH);
for (int i = 1; readline(buf[NEXT(i)], LINELENGTH); i = NEXT(i)) {
for (int j = 1; buf[i][j]; j++) { // j = 0 is '.' guard
if (buf[i][j] >= '0' && buf[i][j] <= '9') {
num = num ? num : (int*) malloc(sizeof(int));
*num = *num * 10 + (buf[i][j] - '0');
CHECK_STAR(buf, i, j - 1, gear1, gear2, num);
CHECK_STAR(buf, i, j + 1, gear1, gear2, num);
CHECK_STAR(buf, PREV(i), j, gear1, gear2, num);
CHECK_STAR(buf, NEXT(i), j, gear1, gear2, num);
CHECK_STAR(buf, PREV(i), j - 1, gear1, gear2, num);
CHECK_STAR(buf, PREV(i), j + 1, gear1, gear2, num);
CHECK_STAR(buf, NEXT(i), j - 1, gear1, gear2, num);
CHECK_STAR(buf, NEXT(i), j + 1, gear1, gear2, num);
} else {
num = NULL;
}
}
for (int j = 1; buf[PREV(i)][j]; j++) {
if (gear1[PREV(i)][j] && gear2[PREV(i)][j]) {
sum += (*gear1[PREV(i)][j]) * (*gear2[PREV(i)][j]);
}
}
memset(gear1[PREV(i)], 0, sizeof(gear1[PREV(i)]));
memset(gear2[PREV(i)], 0, sizeof(gear2[PREV(i)]));
}
printf("%d\n", sum);
return 0;
}

10
03/test.txt Normal file
View File

@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..