44 lines
1.4 KiB
Python
Executable File
44 lines
1.4 KiB
Python
Executable File
def read_from_file(file_name):
|
|
_file = open(file_name, 'r')
|
|
_read = _file.read().split('\n')
|
|
_file.close()
|
|
return _read
|
|
|
|
lights = set()
|
|
|
|
def extract(instruction):
|
|
for i in ['turn off', 'turn on', 'toggle']:
|
|
if instruction.startswith(i + ' '):
|
|
locations = instruction[len(i)+1:].split(' through ')
|
|
a = locations[0].split(',')
|
|
b = locations[1].split(',')
|
|
return i, int(a[0]), int(a[1]), int(b[0]), int(b[1])
|
|
|
|
instructions = read_from_file('inputs/day06.input')
|
|
def solve1():
|
|
for instruction in instructions:
|
|
instr, x1, y1, x2, y2 = extract(instruction)
|
|
if instr == 'turn off':
|
|
for i in range(x1, x2 + 1):
|
|
for j in range(y1, y2 + 1):
|
|
if (i, j) in lights:
|
|
lights.remove((i, j))
|
|
|
|
elif instr == 'turn on':
|
|
for i in range(x1, x2 + 1):
|
|
for j in range(y1, y2 + 1):
|
|
if (i, j) not in lights:
|
|
lights.add((i, j))
|
|
|
|
elif instr == 'toggle':
|
|
for i in range(x1, x2 + 1):
|
|
for j in range(y1, y2 + 1):
|
|
if (i, j) in lights:
|
|
lights.remove((i, j))
|
|
else:
|
|
lights.add((i, j))
|
|
|
|
|
|
return len(lights)
|
|
|
|
print("Pt1:", solve1()) |