Solve 2024 day 8
This commit is contained in:
parent
e9a4ce6414
commit
b622ca92b9
21
2024/d7.py
21
2024/d7.py
@ -1,23 +1,10 @@
|
||||
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):
|
||||
def combs(acc, rest, part_2=False):
|
||||
if rest == []:
|
||||
return acc
|
||||
|
||||
@ -27,16 +14,16 @@ def comp(acc, rest, part_2=False):
|
||||
nacc.append(a * rest[0])
|
||||
if part_2:
|
||||
nacc.append(int(str(a) + str(rest[0])))
|
||||
return comp(nacc, rest[1:], part_2)
|
||||
return combs(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:]):
|
||||
if xs[0] in combs([xs[1]], xs[2:]):
|
||||
t1 += xs[0]
|
||||
if xs[0] in comp([xs[1]], xs[2:], True):
|
||||
if xs[0] in combs([xs[1]], xs[2:], True):
|
||||
t2 += xs[0]
|
||||
|
||||
print(t1)
|
||||
|
34
2024/d8.py
Normal file
34
2024/d8.py
Normal file
@ -0,0 +1,34 @@
|
||||
from lib import get_data
|
||||
from lib import Grid2D
|
||||
from itertools import combinations
|
||||
from collections import defaultdict
|
||||
|
||||
data = get_data(__file__)
|
||||
g = Grid2D(data)
|
||||
|
||||
d = defaultdict(list)
|
||||
|
||||
for row in range(g.n_rows):
|
||||
for col in range(g.n_cols):
|
||||
p = (row, col)
|
||||
if g[p] != ".":
|
||||
d[g[p]].append((row, col))
|
||||
|
||||
aa = set()
|
||||
bb = set()
|
||||
for coords in d.values():
|
||||
for c, d in combinations(coords, 2):
|
||||
bb.add(c)
|
||||
bb.add(d)
|
||||
for a, b in [(c, d), (d, c)]:
|
||||
d = b[0] - a[0], b[1] - a[1]
|
||||
a = b[0] + d[0], b[1] + d[1]
|
||||
if g.contains(a):
|
||||
aa.add(a)
|
||||
a = b[0] + d[0], b[1] + d[1]
|
||||
while g.contains(a):
|
||||
bb.add(a)
|
||||
a = a[0] + d[0], a[1] + d[1]
|
||||
|
||||
print(len(aa))
|
||||
print(len(bb))
|
Loading…
Reference in New Issue
Block a user