add a perimitive complicated wires module.

This commit is contained in:
randomuser 2023-07-15 01:14:24 -05:00
parent 9603c859bc
commit f92a3f9a40
1 changed files with 108 additions and 0 deletions

108
comp_wires.py Normal file
View File

@ -0,0 +1,108 @@
from enum import Enum, auto
class MappedAction(Enum):
CUT_UNCONDITIONALLY = auto(),
DO_NOT_CUT_UNCONDITIONALLY = auto(),
CUT_IF_DIGIT_EVEN = auto(),
CUT_IF_PARALLEL = auto(),
CUT_IF_BATTERIES = auto()
class FinalAction(Enum):
CUT = auto(),
NO_CUT = auto()
class GlobalState:
def __init__(self, serial, parallel, batteries):
self.serial = serial
self.parallel = parallel
self.batteries = batteries
if int(self.serial[-1]) % 2 == 0:
self.serial_even = True
else:
self.serial_even = False
class ComplicatedWire:
def __init__(self, red, blue, star, led, state):
self.red = red
self.blue = blue
self.star = star
self.led = led
self.state = state
def getAction(self):
red = self.red
blue = self.blue
star = self.star
led = self.led
if (not red and not blue and not led and not star) or \
(not led and not blue and star):
return MappedAction.CUT_UNCONDITIONALLY
if led and not blue and (red or star):
return MappedAction.CUT_IF_BATTERIES
if (not star and not led and (red or blue)) or \
(red and led and blue and not star):
return MappedAction.CUT_IF_DIGIT_EVEN
if (blue and star and red ^ led) or \
(led and blue and not red and not star):
return MappedAction.CUT_IF_PARALLEL
if (blue and star and not red ^ led) or \
(led and not red and not star and not blue):
return MappedAction.DO_NOT_CUT_UNCONDITIONALLY
def custom_bool(a):
if a == "False":
return False
elif a == "True":
return True
def resolveAction(self):
action = self.getAction()
print(action)
if action == MappedAction.CUT_UNCONDITIONALLY:
return FinalAction.CUT
elif action == MappedAction.DO_NOT_CUT_UNCONDITIONALLY:
return FinalAction.NO_CUT
elif action == MappedAction.CUT_IF_DIGIT_EVEN:
if self.state.serial_even:
return FinalAction.CUT
return FinalAction.NO_CUT
elif action == MappedAction.CUT_IF_PARALLEL:
if self.state.parallel:
return FinalAction.CUT
return FinalAction.NO_CUT
elif action == MappedAction.CUT_IF_BATTERIES:
if self.state.batteries >= 2:
return FinalAction.CUT
return FinalAction.NO_CUT
@classmethod
def human_helper(cls):
serial = input("serial (string)? ")
parallel = cls.custom_bool(input("parallel (bool)? "))
batteries = int(input("number of batteries (int)? "))
red = cls.custom_bool(input("is red (bool)? "))
blue = cls.custom_bool(input("is blue (bool)? "))
star = cls.custom_bool(input("is star (bool)? "))
led = cls.custom_bool(input("is led (bool)? "))
print(red, blue, star, led)
state = GlobalState(serial, parallel, batteries)
wire = cls(red, blue, star, led, state)
print(wire.resolveAction())
if __name__ == "__main__":
ComplicatedWire.human_helper()