Skip to content

Commit f64b5d2

Browse files
committed
2024 11-13
1 parent 9ff2b7a commit f64b5d2

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed

2024/11/main.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import sys
2+
3+
data = open(sys.argv[1]).read().strip()
4+
lines = data.split('\n')
5+
6+
stones = [int(n) for n in lines[0].split()]
7+
8+
for i in range(25):
9+
new_stones = []
10+
for j, stone in enumerate(stones):
11+
if stone == 0:
12+
new_stones.append(1)
13+
elif len(str(stone)) % 2 == 0:
14+
left_half = int(str(stone)[:len(str(stone))//2])
15+
right_half = int(str(stone)[len(str(stone))//2:])
16+
new_stones.append(left_half)
17+
new_stones.append(right_half)
18+
else:
19+
new_stones.append(stone*2024)
20+
stones = new_stones
21+
print(len(stones))
22+
23+
stones = [(int(n), 0) for n in lines[0].split()]
24+
print(stones)
25+
ans = 0
26+
blinks = 75
27+
while len(stones) > 0:
28+
stone, depth = stones.pop(0)
29+
assert (depth <= blinks)
30+
if depth == blinks:
31+
ans += 1
32+
continue
33+
if stone == 0:
34+
if depth + 4 > blinks:
35+
stones.append((1, depth+1))
36+
else:
37+
new_num = str(2024)
38+
for n in new_num:
39+
stones.append((int(n), depth+4))
40+
elif len(str(stone)) % 2 == 0:
41+
curcount = 0
42+
curlen = len(str(stone))
43+
while curlen % 2 == 0 and curcount+depth < blinks:
44+
curlen = curlen//2
45+
curcount += 1
46+
new_nums_len = len(str(stone))//(2**curcount)
47+
new_nums = [str(stone)[i:i+new_nums_len] for i in range(0, len(str(stone)), new_nums_len)]
48+
for new_num in new_nums:
49+
stones.append((int(new_num), depth+curcount))
50+
else:
51+
if (1 <= stone <= 4) and depth <= blinks-3:
52+
new_num = str(2024*stone)
53+
assert (len(new_num) == 4)
54+
for n in new_num:
55+
stones.append((int(n), depth+3))
56+
elif stone in [5, 6, 7, 9] and depth <= blinks-5:
57+
new_num = str(stone*2024*2024)
58+
assert (len(new_num) == 8)
59+
for n in new_num:
60+
stones.append((int(n), depth+5))
61+
elif stone == 8 and depth <= blinks-5:
62+
new_stones = [3, 2, 7, 7, 2, 6]
63+
for n in new_stones:
64+
stones.append((n, depth+5))
65+
stones.append((8, depth+4))
66+
else:
67+
new_num = str(stone*2024)
68+
new_depth = depth+1
69+
70+
stones.append((stone*2024, depth+1))
71+
72+
print(ans)

2024/12/main.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import sys
2+
3+
data = open(sys.argv[1]).read().strip()
4+
lines = data.split('\n')
5+
6+
grid = [list(line) for line in lines]
7+
8+
seen = set()
9+
regions = []
10+
11+
nbrs = [(-1, 0), (0, 1), (1, 0), (0, -1)]
12+
13+
for y, row in enumerate(grid):
14+
for x, cell in enumerate(row):
15+
if (y, x) in seen:
16+
continue
17+
seen.add((y, x))
18+
region = []
19+
queue = [(y, x)]
20+
while queue:
21+
cury, curx = queue.pop(0)
22+
region.append((cury, curx))
23+
for dy, dx in nbrs:
24+
nbry, nbrx = cury+dy, curx+dx
25+
if 0 <= nbry < len(grid) and 0 <= nbrx < len(grid[0]) and \
26+
grid[nbry][nbrx] == cell and \
27+
(nbry, nbrx) not in seen:
28+
queue.append((nbry, nbrx))
29+
seen.add((nbry, nbrx))
30+
regions.append(region)
31+
32+
33+
def calc_area_perim(region):
34+
area = len(region)
35+
miny, maxy = 10**10, 0
36+
minx, maxx = 10**10, 0
37+
for y, x in region:
38+
miny = min(miny, y)
39+
maxy = max(maxy, y)
40+
minx = min(minx, x)
41+
maxx = max(maxx, x)
42+
region_id = grid[region[0][0]][region[0][1]]
43+
perim = 0
44+
for y in range(miny, maxy+1):
45+
have_seen_cell = False
46+
for x in range(minx, maxx+1):
47+
if grid[y][x] == region_id and (y, x) in region:
48+
if not have_seen_cell:
49+
perim += 2
50+
have_seen_cell = True
51+
else:
52+
have_seen_cell = False
53+
for x in range(minx, maxx+1):
54+
have_seen_cell = False
55+
for y in range(miny, maxy+1):
56+
if grid[y][x] == region_id and (y, x) in region:
57+
if not have_seen_cell:
58+
perim += 2
59+
have_seen_cell = True
60+
else:
61+
have_seen_cell = False
62+
return area, perim
63+
64+
65+
ans = 0
66+
for region in regions:
67+
area, perim = calc_area_perim(region)
68+
ans += (area * perim)
69+
70+
print(ans)
71+
72+
73+
def calc_area_sides(region):
74+
region_id = grid[region[0][0]][region[0][1]]
75+
region = sorted(region)
76+
print(region_id, region)
77+
return 0, 0
78+
79+
80+
ans2 = 0
81+
for region in regions:
82+
area, sides = calc_area_sides(region)
83+
ans += (area * sides)
84+
85+
print(ans2)

2024/13/main.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
from math import lcm
3+
4+
data = open(sys.argv[1]).read().strip()
5+
groups = data.split('\n\n')
6+
7+
ans, ans2 = 0, 0
8+
for group in groups:
9+
lines = group.split('\n')
10+
ax = int(lines[0].split()[2][2:][:-1])
11+
ay = int(lines[0].split()[3][2:])
12+
bx = int(lines[1].split()[2][2:][:-1])
13+
by = int(lines[1].split()[3][2:])
14+
px = int(lines[2].split()[1][2:][:-1])
15+
py = int(lines[2].split()[2][2:])
16+
px2 = px + 10000000000000
17+
py2 = py + 10000000000000
18+
19+
a_lcm = lcm(ax, ay)
20+
x_mult, y_mult = a_lcm//ax, a_lcm//ay
21+
22+
if (x_mult*px - y_mult*py) % (bx*x_mult - by*y_mult) == 0:
23+
b = (x_mult*px - y_mult*py)//(bx*x_mult - by*y_mult)
24+
if (px-bx*b) % ax == 0:
25+
a = (px-bx*b)//ax
26+
if a <= 100 and b <= 100:
27+
ans += 3*a+b
28+
29+
if (x_mult*px2 - y_mult*py2) % (bx*x_mult - by*y_mult) == 0:
30+
b = (x_mult*px2 - y_mult*py2)//(bx*x_mult - by*y_mult)
31+
if (px2-bx*b) % ax == 0:
32+
a = (px2-bx*b)//ax
33+
ans2 += 3*a+b
34+
35+
print(ans)
36+
print(ans2)

0 commit comments

Comments
 (0)