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.txt
consists 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.