Skip to content

Commit

Permalink
figured out problem with day3, incomplete
Browse files Browse the repository at this point in the history
not catching any part numbers that end with the line like

```
...123
```
  • Loading branch information
mattnworb committed Dec 4, 2023
1 parent 2255854 commit 95855ce
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
23 changes: 19 additions & 4 deletions y2023/problem03/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@ def is_symbol(x, y):
x_range = range(len(grid[0]))
y_range = range(len(grid))

def print_surrounding(x_start, x_end, y):
for py in range(y - 1, y + 2):
if py not in y_range:
continue
for px in range(x_start - 1, x_end + 2):
if px not in x_range:
continue
print(grid[py][px], end="")
print()

for y, line in enumerate(grid):
at_number = False
current_number = ""
for x, ch in enumerate(line):
if ch.isdigit():
at_number = True
current_number += ch
elif at_number:
if (at_number and not ch.isdigit()) or (
ch.isdigit() and x == (len(line) - 1)
):
# number is done
part_number = int(current_number)
all_numbers[part_number] += 1
Expand Down Expand Up @@ -52,6 +61,12 @@ def is_symbol(x, y):
if touches_symbol:
part_numbers.add(part_number)

print(f"{part_number} at ({x_start}, {y}), touches={touches_symbol}")
print_surrounding(x_start, x_end, y)
print()
elif ch.isdigit():
at_number = True
current_number += ch
# numbers might be repeated in the input but not always touching a symbol
# the sum should include ALL of them
# "There are lots of numbers and symbols you don't really understand, but
Expand Down
8 changes: 7 additions & 1 deletion y2023/problem03/test_problem03.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@


def test_part1_example():
assert part1(example) == 4361
# assert part1(example) == 4361

ex2 = """..*.
.123
...."""

assert part1(ex2) == 123


def test_part2_example():
Expand Down

0 comments on commit 95855ce

Please sign in to comment.