-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.py
140 lines (124 loc) · 5.32 KB
/
day03.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
class EnginePart:
def __init__(self, part_number, part_location_idx, part_location_line, part_symbol, part_sym_line, part_sym_idx):
self.part_number = part_number
self.part_location_idx = part_location_idx
self.part_location_line = part_location_line
self.part_symbol = part_symbol
self.part_sym_line = part_sym_line
self.part_sym_idx = part_sym_idx
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def part01(day_input):
# Working!
# read each digit of a part number, until next '.', storing indexes of numbers in the line
# store the number, the starting location, the part location and the part symbol.
engine_parts = []
line_number = 0
for line in day_input:
current_part = EnginePart(part_number=None, part_location_idx=None, part_location_line=None,
part_symbol=None, part_sym_line=None, part_sym_idx=None)
found_part = False
for idx in range(len(line)):
if line[idx].isdigit() and line[idx] in '0123456789':
if not found_part:
current_part.part_number = line[idx]
current_part.part_location_idx = idx
current_part.part_location_line = line_number
else:
current_part.part_number += line[idx]
found_part = True
else:
if found_part:
engine_parts.append(current_part)
current_part = EnginePart(part_number=None, part_location_idx=None, part_location_line=None,
part_symbol=None, part_sym_line=None, part_sym_idx=None)
found_part = False
if found_part:
print(f'{bcolors.OKBLUE}{line[idx]}{bcolors.ENDC}', end='')
else:
if line[idx] != '.':
print(f'{bcolors.OKGREEN}{line[idx]}{bcolors.ENDC}', end='')
else:
print(f'{line[idx]}', end='')
if current_part.part_number is not None:
engine_parts.append(current_part)
print('')
line_number += 1
ep_index = 0
# last_line=0
for ep in engine_parts:
# having found all engine parts, search for symbols around the part number
starting_line = ep.part_location_line
starting_idx = ep.part_location_idx
# up, down, and all around...
line_above = starting_line - 1
if line_above < 0:
line_above = 0
line_below = starting_line + 1
if line_below >= len(day_input):
line_below = starting_line
col_before = starting_idx - 1
if col_before < 0:
col_before = 0
col_after = starting_idx + len(ep.part_number) + 1
if col_after >= len(day_input[starting_line]):
col_after = len(day_input[starting_line])
print(f'{ep.part_location_line}: {ep.part_number}: {col_after}')
for curr_col in range(col_before, col_after):
line_above_ch = day_input[line_above][curr_col]
line_curr_ch = day_input[starting_line][curr_col]
line_below_ch = day_input[line_below][curr_col]
if not line_above_ch.isdigit() and line_above_ch != '.':
engine_parts[ep_index].part_symbol = line_above_ch
engine_parts[ep_index].part_sym_line = line_above
engine_parts[ep_index].part_sym_idx = curr_col
break
if not line_curr_ch.isdigit() and line_curr_ch != '.':
engine_parts[ep_index].part_symbol = line_curr_ch
engine_parts[ep_index].part_sym_line = starting_line
engine_parts[ep_index].part_sym_idx = curr_col
break
if not line_below_ch.isdigit() and line_below_ch != '.':
engine_parts[ep_index].part_symbol = line_below_ch
engine_parts[ep_index].part_sym_line = line_below
engine_parts[ep_index].part_sym_idx = curr_col
break
ep_index += 1
part_sum = 0
for ep in engine_parts:
if ep.part_symbol is not None:
# print(f'{ep.part_number}: {ep.part_symbol}')
part_sum += int(ep.part_number)
# else:
# print(f'{ep.part_number}: {ep.part_location_line}: ...')
print(part_sum)
return engine_parts
def part02(day_input):
# WIP
engine_parts = part01(day_input)
found_parts = []
part_sum = 0
for ep in engine_parts:
if ep.part_symbol == '*':
# find ep with same part_sym_line & part_sym_idx & multiply...
if ep in found_parts:
continue
found_parts.append(ep)
# O(n^2) is cruise control for cool
for epl in engine_parts:
if epl.part_sym_idx == ep.part_sym_idx \
and epl.part_sym_line == ep.part_sym_line \
and epl != ep \
and epl not in found_parts:
found_parts.append(epl)
part_sum += int(ep.part_number) * int(epl.part_number)
break
print(part_sum)