Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import namedtuple, defaultdict
- from typing import List, Dict
- import re
- reactorCube = namedtuple('ReactorCube', 'x, y, z')
- reactorCubes: defaultdict[reactorCube: int] = {}
- reactorRule = namedtuple('ReactorRule', 'onOrOff, x1, x2, y1, y2, z1, z2')
- reactorRules:List[reactorRule] = []
- REACTOR_AXIS_LENGTH = 2
- def initReactor(aReactorCubes: List[reactorCubes]):
- for x in range(-REACTOR_AXIS_LENGTH, REACTOR_AXIS_LENGTH + 1):
- for y in range(-REACTOR_AXIS_LENGTH, REACTOR_AXIS_LENGTH + 1):
- for z in range(-REACTOR_AXIS_LENGTH, REACTOR_AXIS_LENGTH + 1):
- aReactorCubes[reactorCube(x, y, z)] = 0
- def getReactorRules(aReactorRules: List[reactorRule]):
- onOrOff = ''
- x1 = x2 = y1 = y2 = z1 = z2 = 0
- with open('AOC2021D22.txt') as INFILE:
- ruleLines = INFILE.read().split('\n')
- for ruleLine in ruleLines:
- ruleLineSplit1 = ruleLine.split()
- onOrOff = ruleLineSplit1[0]
- ruleLineSplit2 = ruleLineSplit1[1].split(',')
- for ruleLineSplit2Elem in ruleLineSplit2:
- m = re.search(r'([xyz])=(-?\d+)..(-?\d+)', ruleLineSplit2Elem)
- if m:
- if m.group(1) == 'x':
- x1 = int(m.group(2))
- x2 = int(m.group(3))
- elif m.group(1) == 'y':
- y1 = int(m.group(2))
- y2 = int(m.group(3))
- elif m.group(1) == 'z':
- z1 = int(m.group(2))
- z2 = int(m.group(3))
- aReactorRules.append(reactorRule(onOrOff, x1, x2, y1, y2, z1, z2))
- def setReactorState(aReactor, aRule: reactorRule):
- newState = 1 if aRule.onOrOff == 'on' else 0
- for x in range(aRule.x1, aRule.x2 + 1):
- for y in range(aRule.y1, aRule.y2 + 1):
- for z in range(aRule.z1, aRule.z2 + 1):
- aReactor[reactorCube(x, y, z)] = newState
- #
- # Main program
- #
- initReactor(reactorCubes)
- print(f'{len(reactorCubes)=}')
- # print(f'{reactorCubes}')
- getReactorRules(reactorRules)
- print(reactorRules)
- for reactorRule in reactorRules:
- setReactorState(reactorCubes, reactorRule)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement