simplified d2
This commit is contained in:
parent
3e7812b556
commit
9370f1ac95
15
02/part1.c
15
02/part1.c
@ -2,19 +2,6 @@
|
||||
#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, cubes, sum;
|
||||
@ -32,7 +19,7 @@ int main(void) {
|
||||
(*c == 'b' && cubes > 14)) {
|
||||
game_num = 0;
|
||||
}
|
||||
c = skip_color(c);
|
||||
for (; *c >= 'a' && *c <= 'z'; c++);
|
||||
}
|
||||
sum += game_num;
|
||||
}
|
||||
|
7
02/part1.py
Normal file
7
02/part1.py
Normal 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))))
|
24
02/part2.c
24
02/part2.c
@ -2,35 +2,23 @@
|
||||
#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;
|
||||
int 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;
|
||||
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 "; "
|
||||
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);
|
||||
for (; *c >= 'a' && *c <= 'z'; c++);
|
||||
}
|
||||
sum += max_r * max_g * max_b;
|
||||
}
|
||||
|
4
02/part2.py
Normal file
4
02/part2.py
Normal 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()))
|
Loading…
Reference in New Issue
Block a user