Skip to content

Commit

Permalink
add another algorithm c from copilot
Browse files Browse the repository at this point in the history
  • Loading branch information
morefreeze committed Dec 22, 2024
1 parent 70aa177 commit cdae6ea
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 172 deletions.
87 changes: 23 additions & 64 deletions _code/algorithm_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,25 @@ def __init__(self, x: int, m: int):
self.m = m
self._s = None

def __eq__(self, value):
if not isinstance(value, Alpha):
return False
return self.to_int() == value.to_int()

@staticmethod
def from_str(s, m):
return Alpha(int(s, m), m)

@staticmethod
def from_hex(hex_str, m):
hex_int = int(hex_str, 16)
x, p = 0, 1
while hex_int > 0:
x += p * (hex_int % 16)
hex_int = hex_int // 16
p *= m
return Alpha(x, m)

def to_str(self):
'''convert to m bit string'''
if self._s is None:
Expand Down Expand Up @@ -165,8 +180,8 @@ def __init__(self, m, g):
self.poison_pointer = self.poison_value - 1
self.free = [0] * self.code_length
self.ifree = [0] * self.code_length
self.level = 0
self.x = 0 # x is trial word
self.level = 1
self.x = Alpha.from_hex('0001', m)
self.trial_class = 0
self.solution = []
self.initialize()
Expand Down Expand Up @@ -235,71 +250,15 @@ def suffix2(self, alpha):
def suffix3(self, alpha):
return (alpha & 0x0FFF) << 4

def search(self):
self.level = 1
while True:
if self.level > self.code_length:
self.visit_solution()
self.backtrack()
else:
self.enter_level()
if self.trial_word < 0:
self.try_again()
else:
self.make_move()

def enter_level(self):
if self.level > self.code_length:
self.visit_solution()
else:
self.level += 1
self.try_candidate()
self.level -= 1
self.backtrack()

def try_candidate(self):
self.trial_word = self.free_list[self.free_index]
self.trial_class = self.index_free_list[self.free_index]
self.free_index -= 1
self.slack = self.current_state[self.level]
self.undo_pointer = self.undo_stack_pointer[self.level]
self.bump_stamp()
self.make_red(self.trial_word)

def try_again(self):
if self.slack == 0 or self.level == 1:
self.backtrack()
else:
self.slack -= 1

def make_move(self):
self.make_green(self.trial_word)
self.solution_vector[self.level] = self.trial_word
self.current_index[self.level] = self.trial_class
self.current_state[self.level] = self.slack
p = self.index_free_list[self.trial_class]
self.free_index -= 1
if p != self.free_index:
y = self.free_list[self.free_index]
self.free_list[p] = y
self.index_free_list[y] = p
self.free_list[self.free_index] = self.trial_class
self.index_free_list[self.trial_class] = self.free_index
self.level += 1

def backtrack(self):
self.level -= 1
if self.level == 0:
return
self.trial_word = self.solution_vector[self.level]
self.trial_class = self.current_index[self.level]
self.free_index += 1
if self.trial_word < 0:
return
self.slack = self.current_state[self.level]
self.undo_to(self.undo_stack_pointer[self.level])
self.bump_stamp()
self.make_red(self.trial_word)
for cur_candidate in self.try_candidate():
self.make_move()
self.enter_level()
self.backtrack()


def visit_solution(self):
# Process the solution found
Expand Down Expand Up @@ -335,4 +294,4 @@ def bump_stamp(self):
# Example usage
code = CommaFreeCode(m=4, g=57)
code.initialize()
# code.search()
code.enter_level()
75 changes: 75 additions & 0 deletions _code/algorithm_c2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class CommaFreeCodeFinder:
def __init__(self, m):
self.m = m
self.m4 = m ** 4
self.m2 = m ** 2
self.L = (self.m4 - self.m2) // 4
self.MEM = [0] * (int(23.5 * self.m4))
self.UNDO = []
self.POISON = 22 * self.m4
self.PP = self.POISON - 1
self.FREE = list(range(self.L))
self.IFREE = list(range(self.L))
self.X = [0] * (self.L + 1)
self.C = [0] * (self.L + 1)
self.S = [0] * (self.L + 1)
self.U = [0] * (self.L + 1)
self.level = 1
self.x = 0x0001
self.c = 0
self.s = self.L - self.goal
self.f = self.L
self.u = 0
self.sigma = 0

def store(self, a, v):
if self.MEM[a] != v:
self.UNDO.append((a, self.MEM[a]))
self.MEM[a] = v

def unstore(self, u0):
while len(self.UNDO) > u0:
a, v = self.UNDO.pop()
self.MEM[a] = v

def enter_level(self):
if self.level > self.L:
self.visit_solution()
return
for cur_candidate in self.try_candidate():
self.make_move(cur_candidate)
self.enter_level()
self.backtrack()

def visit_solution(self):
# Implement the logic to handle a valid solution
print(f"Solution found: {self.X[1:self.level]}")

def try_candidate(self):
# Generate candidate words based on the current state
candidates = []
for i in range(self.m4):
if self.MEM[i] == 1: # Assuming 1 represents a blue word
candidates.append(i)
return candidates

def make_move(self, candidate):
# Update the state to reflect the selection of a candidate word
self.store(self.level, candidate)
self.MEM[candidate] = 2 # Assuming 2 represents a green word
self.X[self.level] = candidate
self.level += 1

def backtrack(self):
# Implement the logic to backtrack
self.level -= 1
candidate = self.X[self.level]
self.MEM[candidate] = 1 # Revert the candidate to blue
self.unstore(self.U[self.level])

def run(self):
self.enter_level()

# Example usage
finder = CommaFreeCodeFinder(m=3)
finder.run()
Loading

0 comments on commit cdae6ea

Please sign in to comment.