75 lines
2.8 KiB
Python
75 lines
2.8 KiB
Python
# with open("./2021/22.test") as input_file:
|
|
with open("./2021/22.input") as input_file:
|
|
input_data = input_file.read()
|
|
|
|
instructions_raw = input_data.split("\n")
|
|
|
|
print(instructions_raw)
|
|
|
|
instructions_parsed = []
|
|
for one_instruction in instructions_raw:
|
|
one_parsed = {}
|
|
one_parsed['instruction'] = one_instruction.split(" ")[0]
|
|
one_parsed['x_min'] = int(one_instruction.split(" ")[1].split(",")[0].split('=')[1].split('..')[0])
|
|
one_parsed['x_max'] = int(one_instruction.split(" ")[1].split(",")[0].split('=')[1].split('..')[1])
|
|
one_parsed['y_min'] = int(one_instruction.split(" ")[1].split(",")[1].split('=')[1].split('..')[0])
|
|
one_parsed['y_max'] = int(one_instruction.split(" ")[1].split(",")[1].split('=')[1].split('..')[1])
|
|
one_parsed['z_min'] = int(one_instruction.split(" ")[1].split(",")[2].split('=')[1].split('..')[0])
|
|
one_parsed['z_max'] = int(one_instruction.split(" ")[1].split(",")[2].split('=')[1].split('..')[1])
|
|
|
|
print(one_parsed)
|
|
|
|
instructions_parsed.append(one_parsed)
|
|
|
|
on_counter = 0
|
|
on_array = {}
|
|
for one_instruction in instructions_parsed:
|
|
print(one_instruction)
|
|
instruction_counter = 0
|
|
|
|
x_min = one_instruction['x_min']
|
|
x_max = one_instruction['x_max']
|
|
y_min = one_instruction['y_min']
|
|
y_max = one_instruction['y_max']
|
|
z_min = one_instruction['z_min']
|
|
z_max = one_instruction['z_max']
|
|
|
|
# # Constrain to -50..50
|
|
# x_min = max(one_instruction['x_min'], -50)
|
|
# x_max = min(one_instruction['x_max'], 50)
|
|
# y_min = max(one_instruction['y_min'], -50)
|
|
# y_max = min(one_instruction['y_max'], 50)
|
|
# z_min = max(one_instruction['z_min'], -50)
|
|
# z_max = min(one_instruction['z_max'], 50)
|
|
|
|
for x in range(x_min, x_max + 1):
|
|
for y in range(y_min, y_max + 1):
|
|
for z in range(z_min, z_max + 1):
|
|
if x not in on_array.keys():
|
|
on_array[x] = {}
|
|
if y not in on_array[x].keys():
|
|
on_array[x][y] = {}
|
|
if z not in on_array[x][y].keys():
|
|
on_array[x][y][z] = ""
|
|
|
|
if one_instruction['instruction'] == 'on' and on_array[x][y][z] != 'on':
|
|
on_counter += 1
|
|
instruction_counter += 1
|
|
if one_instruction['instruction'] == 'off' and on_array[x][y][z] == 'on':
|
|
on_counter -= 1
|
|
instruction_counter -= 1
|
|
on_array[x][y][z] = one_instruction['instruction']
|
|
print(f'''Processing ({x:6}, {y:6}, {z:6}) {on_counter:6}''', end="\r")
|
|
print("\r", instruction_counter)
|
|
# on_counter = 0
|
|
# for x in on_array.keys():
|
|
# for y in on_array[x].keys():
|
|
# for z in on_array[x][y][z].keys():
|
|
# print(f'''Counting ({x:6}, {y:6}, {z:6})''', end="\r")
|
|
# on_counter += 1
|
|
|
|
print(on_counter)
|
|
|
|
|
|
|