Skip to content

Commit 9bdf982

Browse files
committed
fixes
1 parent 53608d1 commit 9bdf982

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+13284
-4356
lines changed

2023/01/input.txt

+999-999
Large diffs are not rendered by default.

2023/01/main.py

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import re
2-
import string
3-
4-
with open('input.txt') as file:
5-
lines = file.read().strip().split()
6-
7-
DIGITS = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', *string.digits[1:]]
8-
RES = {DIGITS[i]: DIGITS[(i + 9) if i < 9 else i] for i in range(len(DIGITS))}
9-
PATTERN = re.compile(rf'(?=({"|".join(RES.keys())}))')
10-
11-
part1 = 0
12-
part2 = 0
13-
for line in lines:
14-
p1 = re.compile(r'\d').findall(line)
15-
p2 = PATTERN.findall(line)
16-
17-
part1 += int(p1[0] + p1[-1])
18-
part2 += int(RES[p2[0]] + RES[p2[-1]])
19-
20-
print(part1)
21-
print(part2)
1+
import re
2+
import string
3+
4+
with open('input.txt') as file:
5+
lines = file.read().strip().split()
6+
7+
DIGITS = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', *string.digits[1:]]
8+
RES = {DIGITS[i]: DIGITS[(i + 9) if i < 9 else i] for i in range(len(DIGITS))}
9+
PATTERN = re.compile(rf'(?=({"|".join(RES.keys())}))')
10+
11+
part1 = 0
12+
part2 = 0
13+
for line in lines:
14+
p1 = re.compile(r'\d').findall(line)
15+
p2 = PATTERN.findall(line)
16+
17+
part1 += int(p1[0] + p1[-1])
18+
part2 += int(RES[p2[0]] + RES[p2[-1]])
19+
20+
print(part1)
21+
print(part2)

2023/01/test.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
two1nine
2-
eightwothree
3-
abcone2threexyz
4-
xtwone3four
5-
4nineeightseven2
6-
zoneight234
1+
two1nine
2+
eightwothree
3+
abcone2threexyz
4+
xtwone3four
5+
4nineeightseven2
6+
zoneight234
77
7pqrstsixteen

2023/02/input.txt

+99-99
Large diffs are not rendered by default.

2023/02/main.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import re
2-
from math import prod
3-
4-
with open('input.txt') as file:
5-
lines = file.read().strip().splitlines()
6-
7-
COLOR_MAP = {'red': (0, 12), 'green': (1, 13), 'blue': (2, 14)}
8-
9-
10-
def calculate_scores(line):
11-
game_id = int(re.search(r'\d+', line.split(':')[0]).group(0))
12-
cubes = [x.strip() for x in re.split(', |;', line.split(':')[1])]
13-
14-
maxes = [0] * 3
15-
game_possible = all(int(n) <= COLOR_MAP[color][1] for n, color in (c.split(' ') for c in cubes))
16-
17-
for c in cubes:
18-
n, color = c.split(' ')
19-
maxes[COLOR_MAP[color][0]] = max(maxes[COLOR_MAP[color][0]], int(n))
20-
21-
return game_id if game_possible else 0, prod(maxes)
22-
23-
24-
scores = [calculate_scores(line) for line in lines]
25-
26-
print(sum(x[0] for x in scores))
27-
print(sum(x[1] for x in scores))
1+
import re
2+
from math import prod
3+
4+
with open('input.txt') as file:
5+
lines = file.read().strip().splitlines()
6+
7+
COLOR_MAP = {'red': (0, 12), 'green': (1, 13), 'blue': (2, 14)}
8+
9+
10+
def calculate_scores(line):
11+
game_id = int(re.search(r'\d+', line.split(':')[0]).group(0))
12+
cubes = [x.strip() for x in re.split(', |;', line.split(':')[1])]
13+
14+
maxes = [0] * 3
15+
game_possible = all(int(n) <= COLOR_MAP[color][1] for n, color in (c.split(' ') for c in cubes))
16+
17+
for c in cubes:
18+
n, color = c.split(' ')
19+
maxes[COLOR_MAP[color][0]] = max(maxes[COLOR_MAP[color][0]], int(n))
20+
21+
return game_id if game_possible else 0, prod(maxes)
22+
23+
24+
scores = [calculate_scores(line) for line in lines]
25+
26+
print(sum(x[0] for x in scores))
27+
print(sum(x[1] for x in scores))

2023/02/test.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
2-
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
3-
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
4-
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
1+
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
2+
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
3+
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
4+
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
55
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green

2023/03/input.txt

+139-139
Large diffs are not rendered by default.

2023/03/main.py

+59-59
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
1-
import math
2-
import string
3-
import re
4-
5-
with open('input.txt') as file:
6-
lines = file.read().strip().splitlines()
7-
CHARS = string.punctuation.replace('.', '')
8-
9-
NEIGHBOURS = [(0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]
10-
11-
combined_indexes = []
12-
for line_i, line in enumerate(lines):
13-
n_indexes = [[*list(range(match.start(), match.end())), match.group()] for match in re.finditer(r'\d+', line)]
14-
for *n_indexes, n in n_indexes:
15-
for n_index in n_indexes:
16-
combined_indexes.append((line_i, n_index, n))
17-
18-
part1 = 0
19-
part2 = 0
20-
for line_i, line in enumerate(lines):
21-
matches = re.finditer(r'\d+', line)
22-
start_i, end_i = 0, 0
23-
number_indexes = [[*list(range(match.start(), match.end())), match.group()] for match in matches]
24-
25-
for *n_indexes, n in number_indexes:
26-
n_valid = False
27-
for index in n_indexes:
28-
for dx, dy in NEIGHBOURS:
29-
try:
30-
if lines[dy + line_i][dx + index] in CHARS:
31-
n_valid = True
32-
break
33-
except IndexError:
34-
continue
35-
36-
if n_valid:
37-
part1 += int(n)
38-
39-
star_indexes = [i for i, char in enumerate(line) if char == '*']
40-
for index in star_indexes:
41-
neighbours = []
42-
for dx, dy in NEIGHBOURS:
43-
try:
44-
x, y = dy + line_i, dx + index
45-
if lines[x][y] in string.digits:
46-
neighbours.append((x, y))
47-
except IndexError:
48-
continue
49-
50-
real_neighbours = set()
51-
for nx, ny in neighbours:
52-
for x, y, n in combined_indexes:
53-
if (nx, ny) == (x, y):
54-
real_neighbours.add(int(n))
55-
if len(real_neighbours) == 2:
56-
part2 += math.prod(real_neighbours)
57-
58-
print(part1)
59-
print(part2)
1+
import math
2+
import string
3+
import re
4+
5+
with open('input.txt') as file:
6+
lines = file.read().strip().splitlines()
7+
CHARS = string.punctuation.replace('.', '')
8+
9+
NEIGHBOURS = [(0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]
10+
11+
combined_indexes = []
12+
for line_i, line in enumerate(lines):
13+
n_indexes = [[*list(range(match.start(), match.end())), match.group()] for match in re.finditer(r'\d+', line)]
14+
for *n_indexes, n in n_indexes:
15+
for n_index in n_indexes:
16+
combined_indexes.append((line_i, n_index, n))
17+
18+
part1 = 0
19+
part2 = 0
20+
for line_i, line in enumerate(lines):
21+
matches = re.finditer(r'\d+', line)
22+
start_i, end_i = 0, 0
23+
number_indexes = [[*list(range(match.start(), match.end())), match.group()] for match in matches]
24+
25+
for *n_indexes, n in number_indexes:
26+
n_valid = False
27+
for index in n_indexes:
28+
for dx, dy in NEIGHBOURS:
29+
try:
30+
if lines[dy + line_i][dx + index] in CHARS:
31+
n_valid = True
32+
break
33+
except IndexError:
34+
continue
35+
36+
if n_valid:
37+
part1 += int(n)
38+
39+
star_indexes = [i for i, char in enumerate(line) if char == '*']
40+
for index in star_indexes:
41+
neighbours = []
42+
for dx, dy in NEIGHBOURS:
43+
try:
44+
x, y = dy + line_i, dx + index
45+
if lines[x][y] in string.digits:
46+
neighbours.append((x, y))
47+
except IndexError:
48+
continue
49+
50+
real_neighbours = set()
51+
for nx, ny in neighbours:
52+
for x, y, n in combined_indexes:
53+
if (nx, ny) == (x, y):
54+
real_neighbours.add(int(n))
55+
if len(real_neighbours) == 2:
56+
part2 += math.prod(real_neighbours)
57+
58+
print(part1)
59+
print(part2)

2023/03/test.txt

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
467..114..
2-
...*......
3-
..35..633.
4-
......#...
5-
617*......
6-
.....+.58.
7-
..592.....
8-
......755.
9-
...$.*....
1+
467..114..
2+
...*......
3+
..35..633.
4+
......#...
5+
617*......
6+
.....+.58.
7+
..592.....
8+
......755.
9+
...$.*....
1010
.664.598..

0 commit comments

Comments
 (0)