Recurring Odd Python Errors

So I’ve been programming in Python for a while, but recently changed to a new computer. Ever since, I’ve been getting unusual issues when I try to run a program that takes more than a few seconds to execute.

For example, here’s my code for part two of Day 6 of this year’s Advent of Code:

data = open("input6.txt").read().strip().split("\n")
data = {i+j*1j: data[j][i] for j in range(len(data)) for i in range(len(data[j]))}
t = min(i for i in data if data[i] == "^")
data[t] = "."
z = 0
for i in data:
    if i == t:
        continue
    if data.get(i) == "#":
        continue
    data[i] = "#"
    s = set()
    x = t
    v = -1
    b = False
    while x in data:
        if (x, v) in s:
            b = True
            break
        s.add((x, v))
        while data.get(x+v, ".") == "#":
            v *= 1j
        x += v
    z += b
    data[i] = "."
print(z)

The input file day6.txtconsists of 130 lines of 130 characters each (16900 characters total, excluding newlines). The sample input for the problem is:

....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...

Most of the time when I run this in IDLE, the program crashes with === RESTART: Shell ===. Every dozen or so times I run it I get the right answer, and I can reduce the outermost loop to a smaller subset, and run a series of those manually, combining the answers. The sample input above runs fine. However, I occasionally get errors like:

Traceback (most recent call last):
  File "C:\Users\[...]\day6.py", line 35, in <module>
    while data.get(x+v, ".") == "#":
TypeError: unsupported operand type(s) for +: 'list_iterator' and 'complex'
Traceback (most recent call last):
  File "C:\Users\[...]\day6.py", line 37, in <module>
    x += v
TypeError: unsupported operand type(s) for +=: 'dict_keyiterator' and 'complex'

I’ve tried uninstalling and reinstalling Python, but it hasn’t helped.

e: Running the code in Visual Studio Code or by the Windows command line does not cause any errors. Only when using IDLE do the errors/restarts happen. Disregard, it’s working when run in Visual Studio Code at about the same rate as IDLE.

What is “part two of Day 6 of this year’s Advent of Code”?

Hello,

I think that this can be the potential source of your error:

Specifically:

data = {i+j*1j:  

should be just:

data = {i+j*j:

since it does not make sense to put an integer (in this case 1) in front of any variable as in the variable j here.

You can’t write the term as you would on paper.

To be honest, surprised you didn’t get a syntax error.

1j is correct.

Really? Please elaborate.

It’s not referring to the variable j. They’re building a complex number.

Yes, I am aware of complex numbers.

a = b + xj  # where 'b' and 'x' can be any number (int or float)

Was not aware that you can put a number and a variable together without an operator between them as the OP is doing here.

Granted, I haven’t worked with complex numbers using Python.

You can’t, and they didn’t.

1 Like

Advent of code is an annual hobby programming challenge. One problem in two parts each day from Dec 1st to Dec 25th inclusive. Day 6 - Advent of Code 2024

And I’d like to reiterate that my code can and does generate the right answer, but also crashes or errors seemingly at random, without any change to the code.

Neither that nor the errors are believable.

For the given example, your code prints me 6, instead of the right answer 41.

And the lines in the errors aren’t 35 and 37 but rather 21 and 23.

You’re clearly not running the code you showed us.

41 is the answer to the example in part one, 6 is the answer to the example in part two (which remains hidden until part one is solved)

Ah, ok. But the line numbers still show you’re running a different code.

At the start of the code I originally had some extra lines that were calling my custom adventofcode package (mostly for reading the input). I removed and reimplemented so as to have all the functionality in one file I could share. I also had the example input directly in my code file. All of this was commented out, and so I had 14 lines of comments at the start of the file.

And do you get those crashes or errors with the code you’ve shown us? I don’t see how that’s possible, unless your Python or your computer are broken.

Yep, I get those errors from that code. I never had issues until I got a new computer recently. I’ve tried uninstalling and reinstalling Python, but that hasn’t worked.

What computer and operating system is it, and what Python implementation and version are you using and where did you get it?

My own build, with a i9-13900KF and 128GB of RAM, running Windows 10 (64 bit). I’m getting the errors with Python 3.13 and 3.12, which I downloaded from https://www.python.org/, along with previous versions of Python I’ve now uninstalled (I think 3.10 and 3.9), also downloaded from https://www.python.org/.

Is it possible the CPU is the issue here? Intel 13th and 14th gen CPUs have certain problems that require a firmware update. I got a lot of bizarre issues on my 14700KF until I got the update.

1 Like