1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#advent of code 2024
#day 25
#I'm not sure if my solution is general enough to work on modified inputs
#like bigboys but I guess it's good enough to work on original AOC one.
#parse schematics, decide if they are a key or a lock based on first row
#generate the heights of each pin
#compare each key with each lock column by column
data = open("25.in","r").read().strip().split("\n\n");
def getDimensions(gridraw):
Category = None;
Schematics = {};
xMax = 0;
yMax = 0;
for y, line in enumerate(gridraw.strip().split("\n")):
yMax = max(yMax,y);
if y == 0:
if line == "."*len(line): Category = "key";
else: Category = "loc";
for x, c in enumerate(line):
xMax = max(xMax,x);
Schematics[(y,x)] = c;
heights = [];
if Category == "key":
for x in range(xMax+1):
h = sum( Schematics[(y,x)]=="#" for y in range(1,yMax) );
heights.append(h);
else:
for x in range(xMax+1):
h = sum( Schematics[(y,x)]=="#" for y in range(yMax-1,0,-1) );
heights.append(h);
return Category, heights;
Keyed = [];
Locked = [];
XMAX = 0;
YMAX = 0;
XMAX = len(data[0].split("\n")[0])
YMAX = len(data[0].split("\n")) -2;
for d in data:
cat, dims = getDimensions(d);
if cat == "key": Keyed.append(dims);
if cat == "loc": Locked.append(dims);
part1 = 0;
for Key in Keyed:
for Lock in Locked:
fit = True;
for col in range(XMAX):
if Key[col] + Lock[col] > YMAX: fit = False;
part1 += 1*fit;
print("part 1 =", part1);
|