-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3b.py
executable file
·82 lines (76 loc) · 2.44 KB
/
day3b.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
from parse import compile
from itertools import repeat
def strparse(lines):
p = compile("Game {:d}: {}\n")
return map(getattr, map(p.parse, lines), repeat('fixed'))
filename = "input3"
# filename = "test3"
file = open(filename, 'r')
lines = file.readlines() #[l.split(" ") for l in file.readlines()]
file.close()
total = 0
print(lines)
syms = "!@#$%^&*()><?{}[]/|\\=_-+"
for i,l in enumerate(lines):
num = ""
for j,c in enumerate(l):
if c == '*':
print(i, j)
nums = []
max_above = i - 2
max_below = i - 2
# Left
num = ""
k = j - 1
while k >= 0 and lines[i][k].isdigit():
num = lines[i][k] + num
k -= 1
if num != "":
nums.append(int(num))
num = ""
# right
k = j + 1
while k < len(l) and lines[i][k].isdigit():
num += lines[i][k]
k += 1
if num != "":
nums.append(int(num))
num = ""
# above
if i > 0:
num = ""
k = j - 1 if j - 1 > 0 else 0
while k > -1 and lines[i - 1][k].isdigit():
k -= 1
while k <= j or lines[i - 1][k].isdigit():
if lines[i - 1][k] == '.':
if num != "":
nums.append(int(num))
num = ""
else:
num += lines[i - 1][k]
k += 1
if num != "":
nums.append(int(num))
# below
if i < len(lines) - 1:
num = ""
k = j - 1 if j - 1 > 0 else 0
while k > -1 and lines[i + 1][k].isdigit():
k -= 1
while k <= j or lines[i + 1][k].isdigit():
if lines[i + 1][k] == '.':
if num != "":
nums.append(int(num))
num = ""
else:
num += lines[i + 1][k]
k += 1
if num != "":
nums.append(int(num))
print(nums)
if len(nums) == 2:
print("gear", nums)
total += nums[0] * nums[1]
print(total)