1
- # wrong answer, but seems to work as expected up to line 3 (line_idx=2) number: 667 (number_idx: 57)
2
- # every line in the "engine schematic" (-> the puzzle input) has 141 characters
3
- LINE_LENGTH = 141
4
- # the "engine schematic" has 140 lines
5
- NUMBER_OF_LINES = 140
6
-
7
1
input_lines = []
8
2
9
3
10
4
def is_part_number (line_idx : int , number_idx : int , number_length : int ):
11
- if number_idx != 0 :
12
- if is_part_marker (input_lines [line_idx ][number_idx - 1 ]):
13
- return True
14
- if line_idx != 0 :
15
- if is_part_marker (input_lines [line_idx - 1 ][number_idx - 1 ]):
16
- return True
17
- if line_idx != NUMBER_OF_LINES - 1 :
18
- if is_part_marker (input_lines [line_idx + 1 ][number_idx - 1 ]):
19
- return True
20
- if number_idx + number_length != LINE_LENGTH - 1 :
21
- if is_part_marker (input_lines [line_idx ][number_idx + number_length ]):
22
- return True
23
- if line_idx != 0 :
24
- if is_part_marker (input_lines [line_idx - 1 ][number_idx + number_length ]):
25
- return True
26
- if line_idx != NUMBER_OF_LINES - 1 :
27
- if is_part_marker (input_lines [line_idx + 1 ][number_idx + number_length ]):
28
- return True
29
- if line_idx != 0 :
30
- for idx in range (number_idx , number_idx + number_length ):
31
- if is_part_marker (input_lines [line_idx - 1 ][idx ]):
32
- return True
33
- if line_idx != NUMBER_OF_LINES - 1 :
34
- for idx in range (number_idx , number_idx + number_length ):
35
- if is_part_marker (input_lines [line_idx + 1 ][idx ]):
36
- return True
37
-
38
- return False
5
+ for x in range (line_idx - 1 , line_idx + 2 ):
6
+ for y in range (number_idx - 1 , number_idx + number_length + 1 ):
7
+ try :
8
+ if not input_lines [x ][y ].isdigit () and input_lines [x ][y ] != "." :
9
+ return True
10
+ except IndexError :
11
+ pass
39
12
13
+ # if number_idx != 0:
14
+ # if is_part_marker(input_lines[line_idx][number_idx - 1]):
15
+ # return True
16
+ # if line_idx != 0:
17
+ # if is_part_marker(input_lines[line_idx - 1][number_idx - 1]):
18
+ # return True
19
+ # if line_idx != NUMBER_OF_LINES - 1:
20
+ # if is_part_marker(input_lines[line_idx + 1][number_idx - 1]):
21
+ # return True
22
+ # if number_idx + number_length != LINE_LENGTH - 1:
23
+ # if is_part_marker(input_lines[line_idx][number_idx + number_length]):
24
+ # return True
25
+ # if line_idx != 0:
26
+ # if is_part_marker(input_lines[line_idx - 1][number_idx + number_length]):
27
+ # return True
28
+ # if line_idx != NUMBER_OF_LINES - 1:
29
+ # if is_part_marker(input_lines[line_idx + 1][number_idx + number_length]):
30
+ # return True
31
+ # if line_idx != 0:
32
+ # for idx in range(number_idx, number_idx + number_length):
33
+ # if is_part_marker(input_lines[line_idx - 1][idx]):
34
+ # return True
35
+ # if line_idx != NUMBER_OF_LINES - 1:
36
+ # for idx in range(number_idx, number_idx + number_length):
37
+ # if is_part_marker(input_lines[line_idx + 1][idx]):
38
+ # return True
40
39
41
- # Part Marker refers all symbols except the '.'.
42
- # As stated in the puzzle any number that is in any way adjacent to one of these symbols is a part number.
43
- def is_part_marker (adjacent_char : str ):
44
- return not adjacent_char .isdigit () and adjacent_char != "."
40
+ return False
45
41
46
42
47
43
def main ():
@@ -58,13 +54,16 @@ def main():
58
54
for char_idx , char in enumerate (line ):
59
55
if char .isdigit ():
60
56
number += char
61
- if not number_idx :
57
+ if number_idx is None :
62
58
number_idx = char_idx
63
- elif number_idx :
59
+ elif number_idx is not None :
64
60
if is_part_number (line_idx , number_idx , len (number )):
65
61
part_numbers_sum += int (number )
66
62
number = ""
67
63
number_idx = None
64
+ if number_idx is not None :
65
+ if is_part_number (line_idx , number_idx , len (number )):
66
+ part_numbers_sum += int (number )
68
67
69
68
print ("Sum of part numbers:" , part_numbers_sum )
70
69
0 commit comments