Advertisement
bob_f

AOC2021D22.py

Oct 4th, 2023
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1.  
  2. from collections import namedtuple, defaultdict
  3. from typing import List, Dict
  4. import re
  5.  
  6. reactorCube = namedtuple('ReactorCube', 'x, y, z')
  7. reactorCubes: defaultdict[reactorCube: int] = {}
  8.  
  9. reactorRule = namedtuple('ReactorRule', 'onOrOff, x1, x2, y1, y2, z1, z2')
  10. reactorRules:List[reactorRule] = []
  11.  
  12. REACTOR_AXIS_LENGTH = 2
  13.  
  14. def initReactor(aReactorCubes: List[reactorCubes]):
  15.     for x in range(-REACTOR_AXIS_LENGTH, REACTOR_AXIS_LENGTH + 1):
  16.         for y in range(-REACTOR_AXIS_LENGTH, REACTOR_AXIS_LENGTH + 1):
  17.             for z in range(-REACTOR_AXIS_LENGTH, REACTOR_AXIS_LENGTH + 1):
  18.                 aReactorCubes[reactorCube(x, y, z)] = 0
  19.  
  20. def getReactorRules(aReactorRules: List[reactorRule]):
  21.     onOrOff = ''
  22.     x1 = x2 = y1 = y2 = z1 = z2 = 0
  23.  
  24.     with open('AOC2021D22.txt') as INFILE:
  25.         ruleLines = INFILE.read().split('\n')
  26.    
  27.     for ruleLine in ruleLines:
  28.         ruleLineSplit1 = ruleLine.split()
  29.         onOrOff = ruleLineSplit1[0]
  30.  
  31.         ruleLineSplit2 = ruleLineSplit1[1].split(',')
  32.  
  33.         for ruleLineSplit2Elem in ruleLineSplit2:
  34.             m = re.search(r'([xyz])=(-?\d+)..(-?\d+)', ruleLineSplit2Elem)
  35.             if m:
  36.                 if m.group(1) == 'x':
  37.                     x1 = int(m.group(2))
  38.                     x2 = int(m.group(3))
  39.                 elif m.group(1) == 'y':
  40.                     y1 = int(m.group(2))
  41.                     y2 = int(m.group(3))
  42.                 elif m.group(1) == 'z':
  43.                     z1 = int(m.group(2))
  44.                     z2 = int(m.group(3))
  45.        
  46.         aReactorRules.append(reactorRule(onOrOff, x1, x2, y1, y2, z1, z2))
  47.  
  48. def setReactorState(aReactor, aRule: reactorRule):
  49.     newState = 1 if aRule.onOrOff == 'on' else 0
  50.  
  51.     for x in range(aRule.x1, aRule.x2 + 1):
  52.         for y in range(aRule.y1, aRule.y2 + 1):
  53.             for z in range(aRule.z1, aRule.z2 + 1):
  54.                 aReactor[reactorCube(x, y, z)] = newState
  55.  
  56. #
  57. # Main program
  58. #
  59.  
  60. initReactor(reactorCubes)
  61. print(f'{len(reactorCubes)=}')
  62. # print(f'{reactorCubes}')
  63.  
  64. getReactorRules(reactorRules)
  65. print(reactorRules)
  66.  
  67. for reactorRule in reactorRules:
  68.     setReactorState(reactorCubes, reactorRule)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement