summaryrefslogtreecommitdiffstats
path: root/prog/aoc/23/3/1.py
blob: fce4aa9b2d65357058d92c3e6cfc2c1ba849ec7d (plain) (blame)
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
#!/usr/bin/python3
parts = {}
numbers = []
ln = 0
try:
	while True:
		l = input() + "."
		i = 0
		num = ""
		for c in l:
			if not c.isdigit():
				if num != "":
					numbers.append((int(num), ln, i-len(num)))
					num = ""
				if not c == ".":
					parts[(ln, i)] = c
			if c.isdigit():
				num += c
			i += 1
		ln += 1
except:
	gears = {}
	sum = 0
	sumgears = 0
	for number in numbers:
		did = False
		for char in range(len(str(number[0]))+2):
			presek = set([(number[1], number[2]-1+char), (number[1]-1, number[2]-1+char), (number[1]+1, number[2]-1+char)]) & set(parts.keys());
			if len(presek):
				if not did:
					sum += number[0]
					did = True
				if len(presek) != 1:
					print("unreachable")
				part = [x for x in presek][0]
				if parts[part] == "*":
					if part not in gears:
						gears[part] = []
					gears[part].append(number[0])
	for pair in gears.values():
		if len(pair) != 2:
			continue
		sumgears += pair[0]*pair[1]
	print(sum)
	print(sumgears)