Compare commits
3 Commits
63166ddce8
...
e9a4ce6414
| Author | SHA1 | Date | |
|---|---|---|---|
| e9a4ce6414 | |||
| 36f8f709e1 | |||
| fae61ae6db |
51
2024/d6.py
Normal file
51
2024/d6.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from lib import get_data
|
||||
from lib import Grid2D
|
||||
|
||||
data = get_data(__file__)
|
||||
|
||||
|
||||
DIRS = [
|
||||
(-1, 0),
|
||||
(0, 1),
|
||||
(1, 0),
|
||||
(0, -1),
|
||||
]
|
||||
|
||||
g = Grid2D(data)
|
||||
|
||||
t = 0
|
||||
for rr in range(g.n_rows):
|
||||
for cc in range(g.n_cols):
|
||||
(p,), dir = g.find("^"), 0
|
||||
seen = set()
|
||||
loop = False
|
||||
|
||||
if g[(rr, cc)] == "#":
|
||||
continue
|
||||
|
||||
if g[(rr, cc)] != "^":
|
||||
g[(rr, cc)] = "#"
|
||||
|
||||
while True:
|
||||
if (p, dir) in seen:
|
||||
loop = True
|
||||
break
|
||||
seen.add((p, dir))
|
||||
r, c = p[0] + DIRS[dir][0], p[1] + DIRS[dir][1]
|
||||
if not (0 <= r < g.n_rows and 0 <= c < g.n_cols):
|
||||
break
|
||||
|
||||
if g[(r, c)] == "#":
|
||||
dir = (dir + 1) % 4
|
||||
else:
|
||||
p = (r, c)
|
||||
|
||||
if g[(rr, cc)] == "^":
|
||||
print(len(set(pos for pos, _ in seen)))
|
||||
else:
|
||||
g[(rr, cc)] = "."
|
||||
|
||||
if loop:
|
||||
t += 1
|
||||
|
||||
print(t)
|
||||
43
2024/d7.py
Normal file
43
2024/d7.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from lib import get_data
|
||||
from lib import Grid2D
|
||||
from lib import ints
|
||||
from collections import defaultdict
|
||||
|
||||
data = """ 190: 10 19
|
||||
3267: 81 40 27
|
||||
83: 17 5
|
||||
156: 15 6
|
||||
7290: 6 8 6 15
|
||||
161011: 16 10 13
|
||||
192: 17 8 14
|
||||
21037: 9 7 18 13
|
||||
292: 11 6 16 20
|
||||
"""
|
||||
|
||||
data = get_data(__file__)
|
||||
|
||||
|
||||
def comp(acc, rest, part_2=False):
|
||||
if rest == []:
|
||||
return acc
|
||||
|
||||
nacc = []
|
||||
for a in acc:
|
||||
nacc.append(a + rest[0])
|
||||
nacc.append(a * rest[0])
|
||||
if part_2:
|
||||
nacc.append(int(str(a) + str(rest[0])))
|
||||
return comp(nacc, rest[1:], part_2)
|
||||
|
||||
|
||||
t1, t2 = 0, 0
|
||||
for line in data.splitlines():
|
||||
xs = ints(line)
|
||||
expected = xs[0]
|
||||
if xs[0] in comp([xs[1]], xs[2:]):
|
||||
t1 += xs[0]
|
||||
if xs[0] in comp([xs[1]], xs[2:], True):
|
||||
t2 += xs[0]
|
||||
|
||||
print(t1)
|
||||
print(t2)
|
||||
@@ -296,3 +296,6 @@ and focus. Of course, learning more algorithms and techniques helps.
|
||||
- Day 3: `00:01:48 165 0 00:02:40 56 45`
|
||||
- Day 4: `00:07:47 1101 0 00:24:07 2410 0`
|
||||
- Day 5: `00:07:07 679 0 00:23:02 1998 0`
|
||||
- Day 6: `00:09:43 1082 0 00:16:29 464 0`
|
||||
- Day 7: `00:12:29 2058 0 00:13:08 1170 0`
|
||||
- Day 8:
|
||||
|
||||
Reference in New Issue
Block a user