-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
192 lines (172 loc) · 6.35 KB
/
main.py
File metadata and controls
192 lines (172 loc) · 6.35 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
In this schematic, two numbers are not part numbers because they are not adjacent to a symbol:
114 (top right) and 58 (middle right).
Every other number is adjacent to a symbol and so is a part number; their sum is 4361.
"""
test_input = """
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
"""
games = {}
# for each_line in test_input.splitlines():
# print(each_line)
# schematic = test_input
schematic = []
part_number = []
total = 0
symbols = ["*", "#", "+", "$", "%"]
with open("../inputs/day03.txt", mode="r") as f:
for rows in f:
schematic.append(rows)
lists = []
special_list = []
gears_list = []
# Split the schematic into rows
rows = schematic # .split()
gears = 0
# Initialize the sum of part numbers to 0
part_sum = 0
for i in range(len(rows)):
# Loop through each character in the row
j = 0
while j < len(rows[i]):
# Check if the character is a number
if rows[i][j].isdigit():
# Check if the number is adjacent to a symbol in any of the eight directions
gear_adjacent = False
adjacent = False
for x in range(-1, 2):
for y in range(-1, 2):
if x == 0 and y == 0:
continue
if (
i + x < 0
or i + x >= len(rows)
or j + y < 0
or j + y >= len(rows[i])
):
continue
if rows[i + x][j + y] in [
"#",
"+",
"$",
"@",
"%",
"/",
"-",
"=",
"!",
"^",
"&",
]:
adjacent = True
break
if rows[i + x][j + y] in ["*"]:
gear_adjacent = True
break
if adjacent or gear_adjacent:
break
# Add the number to the sum of part numbers if it is adjacent to a symbol
if adjacent or gear_adjacent:
# Find the end of the part number in the forward direction
end = j + 1
while end < len(rows[i]) and rows[i][end].isdigit():
end += 1
# Find the end of the part number in the backward direction
start = j - 1
while start >= 0 and rows[i][start].isdigit():
start -= 1
# Add the part number to the sum
lists.append(int(rows[i][start + 1 : end]))
part_sum += int(rows[i][start + 1 : end])
if gear_adjacent:
special_list.append(rows[i][start + 1 : end])
# print(i, start + 1, end)
gears_list.append(
{
"i": i,
"start": start + 1,
"end": end,
"value": rows[i][start + 1 : end],
}
)
if len(special_list) >= 2:
if special_list[0].endswith("*") or special_list[1].endswith(
"*"
):
part_sum += sum(special_list)
special_list = []
gear_adjacent = False # founded and remove
else:
first_gear = int(special_list[0].replace(".", ""))
second_gear = int(special_list[1])
first_info = next(
item
for item in gears_list
if item["value"] == str(first_gear)
)
second_info = next(
item
for item in gears_list
if item["value"] == str(second_gear)
)
if abs(first_info["i"] - second_info["i"]) > 2:
# remove first and sum
special_list = []
part_sum += first_gear
first_gear = 1
continue
gears += first_gear * second_gear
special_list = []
gear_adjacent = False # founded and remove
# gears.append(rows[i][start + 1:en
# Move the index to the end of the part number
j = end
else:
j += 1
# Add the number to the sum of part numbers if it is adjacent to a symbol
# elif gear_adjacent:
# end = j + 1
# while end < len(rows[i]) and rows[i][end].isdigit():
# end += 1
# start = j - 1
# while start >= 0 and rows[i][start].isdigit():
# start -= 1
# Add the part number to the sum
# special_list.append(rows[i][start + 1:end])
# gears_list.append(rows[i][start + 1:end])
# if len(special_list) >=2:
# if special_list[0].endswith('*') or special_list[1].endswith('*'):
# part_sum += sum(special_list)
# special_list = []
# else:
# gears += int(special_list[0].replace('.','')) * int(special_list[1])
# special_list = []
# # gears.append(rows[i][start + 1:end])
# part_sum += int(rows[i][start + 1:end])
# Move the index to the end of the part number
# j = end
# else:
# j += 1
else:
j += 1
# Print the sum of part numbers
print(f"{lists=}")
print(f"{gears_list=}")
# print(gears)
# print(part_sum)
print(f"{gears=}")
print(f"{part_sum=}")
print(gears + part_sum)
# 436155 too low
# 515997 too low
# 535351 -correct
# 87287096 should be correct