Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1a13b02

Browse filesBrowse files
committed
Add day 15
1 parent e7b61a9 commit 1a13b02
Copy full SHA for 1a13b02

File tree

3 files changed

+111
-2
lines changed
Filter options

3 files changed

+111
-2
lines changed

‎.aoc_tiles/tiles/2024/15.png

Copy file name to clipboard
8.28 KB
Loading

‎2024/15/15.py

Copy file name to clipboard
+106Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from collections import *
2+
from itertools import *
3+
from functools import *
4+
# import numpy as np
5+
# import networkx as nx
6+
# import z3
7+
import re
8+
import sys
9+
sys.setrecursionlimit(1000000)
10+
11+
s1 = s2 = 0
12+
13+
# d4 = [1, 1j, -1, -1j]
14+
d4 = {'>': 1, 'v':1j, '<':-1, '^':-1j}
15+
# d8 = d4 + [1+1j, 1-1j, -1+1j, -1-1j]
16+
# d4half = [i/2 for i in d4]
17+
# d8half = [i/2 for i in d8]
18+
def adjacent(coord, dirs=d4):
19+
return [coord + d for d in dirs]
20+
21+
22+
field, moves = open(0).read().split("\n\n")
23+
24+
25+
if True:
26+
for r, r2 in [("#", "##"), ("O", "[]"), (".", ".."), ("@", "@.")]:
27+
field = field.replace(r, r2)
28+
29+
coords = {x+1j*y: c for y, r in enumerate(field.strip().splitlines()) for x, c in enumerate(r.strip())}
30+
31+
# field = [list(l) for l in field.strip().split("\n")]
32+
s = 0
33+
for c, char in coords.items():
34+
if char == "@":
35+
s = c
36+
coords[c] = "."
37+
38+
M = max(map(lambda c: c.real, coords))
39+
for c, char in coords.items():
40+
print(end=char)
41+
if c.real == M:
42+
print()
43+
44+
45+
def collect(s, d):
46+
if coords[s+d] in "[]":
47+
dn = (1 if coords[s+d] == '[' else -1) + d
48+
base = {s+d: coords[s+d], s+dn: coords[s+dn]} | collect(s+d, d)
49+
if d not in [1, -1]:
50+
base |= collect(s+dn, d)
51+
return base
52+
return {}
53+
54+
for m in moves.strip():
55+
if m not in d4:
56+
continue
57+
# print(s)
58+
d = d4[m]
59+
n = s + d
60+
i = 1
61+
62+
63+
ok = True
64+
if coords[s + d] in '[]':
65+
boxes = collect(s, d)
66+
# print(boxes)
67+
for x, char in boxes.items():
68+
if coords[x+d] == '#':
69+
ok = False
70+
break
71+
if ok:
72+
for x, char in boxes.items():
73+
coords[x] = '.'
74+
for x, char in boxes.items():
75+
coords[x+d] = char
76+
s = n
77+
elif coords[s+d] == '.':
78+
s = n
79+
80+
# while coords[s + d * i] in '[]':
81+
# i += 1
82+
# if coords[s+d*i] == '.':
83+
# coords[s+d*i] = 'O'
84+
# coords[n] = '.'
85+
# s = n
86+
87+
# print("\033[0;0H")
88+
# for c, char in coords.items():
89+
# print(end=char if c != s else '@')
90+
# if c.real == M:
91+
# print()
92+
# import time
93+
# time.sleep(0.02)
94+
95+
for c, char in coords.items():
96+
if char == '[':
97+
s1 += c.imag * 100 + c.real
98+
99+
100+
# for line in open(0):
101+
# n = [int(a) for a in line.split()]
102+
# re.findall(r"\d+", line)
103+
104+
105+
print(int(s1), s2, sep="\n")
106+
# not 6186250.0

‎README.md

Copy file name to clipboardExpand all lines: README.md
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<!-- AOC TILES BEGIN -->
22
<h1 align="center">
3-
Advent of Code - 238/478
3+
Advent of Code - 240/480
44
</h1>
55
<h1 align="center">
6-
2024 - 28 ⭐ - Python
6+
2024 - 30 ⭐ - Python
77
</h1>
88
<a href="2024/01/01.py">
99
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
@@ -47,6 +47,9 @@
4747
<a href="2024/14/14.py">
4848
<img src=".aoc_tiles/tiles/2024/14.png" width="161px">
4949
</a>
50+
<a href="2024/15/15.py">
51+
<img src=".aoc_tiles/tiles/2024/15.png" width="161px">
52+
</a>
5053
<h1 align="center">
5154
2023 - 50 ⭐ - Python
5255
</h1>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.