-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay3GearRatios.py
More file actions
56 lines (48 loc) · 1.75 KB
/
Day3GearRatios.py
File metadata and controls
56 lines (48 loc) · 1.75 KB
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
import sys, os
import numpy as np
if __name__ == '__main__':
input_file = 'Day3Input.txt'
# input_file = 'Day3Input_test.txt'
engine_schematic = []
with open(input_file, 'r') as f:
curr_line = f.readline()
while curr_line:
es_list = [*curr_line]
if es_list[-1] == '\n':
es_list = es_list[:-1]
engine_schematic.append(es_list)
curr_line = f.readline()
engine_schematic = np.array(engine_schematic)
symbols = set(['*','/','$','%','@','#','=','-','+','&'])
numbers = set()
m,n = engine_schematic.shape
def get_number(k,l):
r = 0
while l+r < n and engine_schematic[k,l+r].isnumeric():
r += 1
left = 0
while l-left >= 0 and engine_schematic[k,l-left].isnumeric():
left += 1
if left > 0: left -= 1
number = int(''.join(engine_schematic[k,l-left:l+r]))
for i in range(l-left,l+r):
engine_schematic[k,i] = '.'
return number
def check_neighbours(i,j):
neighbours = [[i+1,j],[i-1,j],[i,j+1],[i,j-1],[i-1,j-1],[i+1,j+1],[i+1,j-1],[i-1,j+1]]
number = 0
for k,l in neighbours:
if 0<= k < m and 0<= l < n:
if engine_schematic[k,l].isnumeric():
number += get_number(k,l)
return number
sum_parts = 0
for i in range(m):
for j in range(n):
if engine_schematic[i,j] in symbols:
number = check_neighbours(i,j)
sum_parts += number
print(sum_parts)
with open('Day3Output.txt', 'w') as file:
for i in range(m):
file.write(''.join(engine_schematic[i,:]) + '\n')