def __eq__(self, other): def __getitem__(self, index): def __repr__(self):class PolarizedList:
def __init__(self, polarity=True, content=0):
self.polarity = polarity
if type(content) is int:
self.list = []
for _ in range(content):
self.list.append(PolarizedList())
else:
self.list = content
return self.polarity is other.polarity and self.list == other.list
def __len__(self):
return len(self.list)
def __iter__(self):
return iter(self.list)
return self.list[index]
def __setitem__(self, index, value):
self.list[index] = value
return (
f"{'+' if self.polarity else '-'}"
f"[{''.join([repr(i) for i in self])}]"
)
def __str__(self):
return f"{'+' if self.polarity else '-'}" + (
f"{len(self)}" if self.is_numeric()
else f"[{''.join([str(i) for i in self])}]"
)
def __neg__(self):
return PolarizedList(not self.polarity, self.list)
def is_nil(self):
return self.polarity is True and self.list == []
def is_numeric(self):
return all([i.is_nil() for i in self])
def fuse(self):
pre_fuse = []
for i in self:
separator = i.polarity
for j in i:
pre_fuse.append((separator, j))
separator = None
if separator is not None:
pre_fuse.append((separator, None))
new_list = []
previous_polarity = None
for separator, element in pre_fuse:
if element is None:
new_list.append(PolarizedList(separator))
previous_polarity = None
continue
if previous_polarity is True and element.polarity is False:
separator = {True: None, None: False, False: True}[separator]
previous_polarity = element.polarity
if separator is not None:
new_list.append(PolarizedList(separator))
new_list[-1].list.append(-element)
self.list[:] = new_list
def defuse(self):
pre_fuse = []
for i in self:
separator = i.polarity
for j in i:
pre_fuse.append((separator, j))
separator = None
if separator is not None:
pre_fuse.append((separator, None))
new_list = []
previous_polarity = None
for separator, element in pre_fuse:
if element is None:
new_list.append(PolarizedList(separator))
previous_polarity = None
continue
if previous_polarity is False and element.polarity is True:
separator = {False: None, None: True, True: False}[separator]
previous_polarity = element.polarity
if separator is not None:
new_list.append(PolarizedList(separator))
new_list[-1].list.append(-element)
self.list[:] = new_list
def segmented_transposition(self):
segments = []
segment_polarity, segment_length = None, None
for i in self:
if segment_polarity != i.polarity or segment_length != len(i):
segments.append([])
segment_polarity, segment_length = i.polarity, len(i)
segments[-1].append(i)
print(segments)
new_list = []
previous_polarity, previous_length = None, None
for i in segments:
if (
i[0].polarity is previous_polarity and
len(i) == previous_length
):
return
previous_polarity, previous_length = i[0].polarity, len(i)
transposed_segment = [
PolarizedList(i[0].polarity, [*j]) for j in zip(*i)
]
if len(transposed_segment) == 0:
return
new_list += transposed_segment
self.list[:] = new_list
def ignorant_reversal(self):
subelements = [sub_i for i in self for sub_i in i]
re_insert = iter(reversed(subelements))
for i in self:
for sub_i, _ in enumerate(i):
i[sub_i] = next(re_insert)
def deepcopy(self):
# Not implementing copy.deepcopy since this implementation does not
# have to support recursive polarized lists. If that happens,
# that's a problem with this interpreter.
return PolarizedList(self.polarity, [i.deepcopy() for i in self])
# This assumes correct syntax
def read_polarized_list(written):
stack = [PolarizedList()]
for i in written:
match i:
case "+":
polarity = True
case "-":
polarity = False
case "[":
new_list = PolarizedList(polarity)
stack[-1].list.append(new_list)
stack.append(new_list)
case "]":
stack.pop()
case _:
stack[-1].list.append(PolarizedList(polarity, int(i)))
return stack[-1][0]
class Thread: def literal(self, content): def fuse(self): def summon(self): def fork(self): def hokey(self): def kitten(self):from polarized_list import PolarizedList
def __init__(self, stack, shiny=True, parent=None):
self.stack = stack
self.shiny = shiny
self.__parent = parent
def parent(self):
if self.__parent is None:
self.__parent = Thread(
[PolarizedList(True, [PolarizedList(True, self.stack)])]
)
return self.__parent
if len(self.stack) == 0 or self.stack[0] == -content:
self.stack.pop(0)
else:
self.stack.insert(0, content)
def illiteral(self, content):
self.literal(-content)
if self.stack == []:
return
self.stack[0].fuse()
def defuse(self):
if self.stack == []:
return
self.stack[0].defuse()
if len(self.stack) < 3:
return
self.stack.insert(0, self.stack.pop(2))
def banish(self):
if len(self.stack) < 3:
return
self.stack.insert(2, self.stack.pop(0))
if self.stack == [] or (substacks := self.stack[0].list) == []:
return [Thread([], False, self)]
new_threads = []
for _, stack in substacks:
new_threads.append(Thread(stack, True, self))
return new_threads
# spoon is implemented separately
if self.stack == []:
return
self.stack[0].segmented_transposition()
self.stack[0].ignorant_reversal()
def co*key(self):
if self.stack == []:
return
self.stack[0].ignorant_reversal()
self.stack[0].segmented_transposition()
if self.stack == []:
return
top = self.stack[0]
try:
if top.polarity:
top.list.insert(0, self.stack.pop(1))
else:
self.stack.insert(1, top.list.pop(0))
except IndexError:
self.stack[0] = -top
def antikitten(self):
if self.stack == []:
return
top = self.stack[0]
try:
if not top.polarity:
top.list.insert(0, self.stack.pop(1))
else:
self.stack.insert(1, top.list.pop(0))
except IndexError:
self.stack[0] = -top
def one_cycle(program):import polarized_list, threads
Probably better if I stop it here for today, I'll have plenty of time tomorrow anyway
active_threads = [threads.Thread([
PolarizedList(True, program).deepcopy()
])]
for instruction in program:
shiny_threads = [thread for threads in active_threads if thread.shiny]
match instruction:
case PolarizedList(polarity=True, list=[x]):
for thread in shiny_threads:
thread.literal(x)
case PolarizedList(polarity=False, list=[x]):
for thread in shiny_threads:
thread.illiteral(x)
case PolarizedList(polarity=True, list=[_, _]):
for thread in shiny_threads:
thread.fuse(x)
case PolarizedList(polarity=False, list=[_, _]):
for thread in shiny_threads:
thread.defuse(x)
case PolarizedList(polarity=True, list=[_, _, _]):
for thread in shiny_threads:
thread.summon(x)
case PolarizedList(polarity=False, list=[_, _, _]):
for thread in shiny_threads:
thread.banish(x)
case PolarizedList(polarity=True, list=[_, _, _, _]):
new_active_threads = []
for thread in active_threads:
new_active_threads += thread.fork()
active_threads[:] = new_active_threads
case PolarizedList(polarity=False, list=[_, _, _, _]):
new_active_threads = set()
for thread in active_threads:
new_active_threads.add(thread.parent())
active_threads[:] = list(new_active_threads)
case PolarizedList(polarity=True, list=[_, _, _, _, _]):
for thread in shiny_threads:
thread.hokey(x)
case PolarizedList(polarity=False, list=[_, _, _, _, _]):
for thread in shiny_threads:
thread.co*key(x)
case PolarizedList(polarity=True, list=[_, _, _, _, _, _]):
for thread in shiny_threads:
thread.kitten(x)
case PolarizedList(polarity=False, list=[_, _, _, _, _, _]):
for thread in shiny_threads:
thread.antikitten(x)
case _:
pass
return active_threads