Example #1
0
def ApplyConf(client):
    file = open('shrun.txt')
    configurations = file.readlines()
    common.Print("Getting Information from Device")
    common.SendCommands(client, configurations)
    common.Loading(5, '', True)
    return client.recv(100000)
Example #2
0
def ApplyConf(client):
    file = open('conf.txt')
    configurations = file.readlines()
    common.SendCommand(client, "conf t")
    common.Print("Apply Configuration")
    common.SendCommands(client, configurations)
    common.Loading(3, '', True)
    common.SaveConfig(client)
Example #3
0
def ApplyConf(client, ports):
    common.SendCommand(client, "conf t")
    file = open('switchport.txt')
    configurations = file.readlines()
    for port in ports:
        common.Print("Configuring " + port.Name)
        common.SendCommand(client, "default int " + port.Name)
        common.Loading(1)
        common.SendCommand(client, "int " + port.Name)
        common.SendCommands(client, configurations)
        common.Loading(2)
        print('')
    common.SaveConfig(client)
Example #4
0
def SolveIteratively(rows, columns):
  work_rows = [
      set(Expand(row_clues, Legals(row_clues, 0, len(columns)), len(columns)))
      for row_clues in rows
  ]
  work_cols = [
      set(Expand(col_clues, Legals(col_clues, 0, len(rows)), len(rows)))
      for col_clues in columns
  ]

  truth_stack = []
  truth_values = [[None] * len(columns) for _ in range(len(rows))]
  while True:
    old_count = sum(map(len, work_cols)) + sum(map(len, work_rows))
    old_truth = copy.deepcopy(truth_values)
    intermediate_truth = copy.deepcopy(truth_values)
    for r, wr in enumerate(work_rows):
      # Filter rows that conflict with truth values.
      for c, truth in enumerate(truth_values[r]):
        if truth is not None:
          smaller_wr = wr.copy()
          for cand in wr:
            if cand[c] != truth:
              smaller_wr.remove(cand)
          work_rows[r] = smaller_wr
      new_count = sum(map(len, work_cols)) + sum(map(len, work_rows))
    # Find squares that have only a single possible truth value.
    for r, wr in enumerate(work_rows):
      if not wr:
        break
      for c in range(len(columns)):
        seen = set()
        for cand in wr:
          seen.add(cand[c])
          if len(seen) > 1:
            break
        else:
          truth_values[r][c] = next(iter(seen))
      if truth_values != intermediate_truth:
        intermediate_truth = copy.deepcopy(truth_values)
        common.Print(truth_values)
        print(f'\x1b[A\x1b[KConsidering {new_count} configurations.')
        time.sleep(.18)

    for c, wc in enumerate(work_cols):
      # Filter cols that conflict with truth values.
      for r in range(len(rows)):
        truth = truth_values[r][c]
        if truth is not None:
          smaller_wc = wc.copy()
          for cand in wc:
            if cand[r] != truth:
              smaller_wc.remove(cand)
          work_cols[c] = smaller_wc
      new_count = sum(map(len, work_cols)) + sum(map(len, work_rows))
    # Find squares that have only a single possible truth value.
    for c, wc in enumerate(work_cols):
      if not wc:
        break
      for r in range(len(rows)):
        seen = set()
        for cand in wc:
          seen.add(cand[r])
          if len(seen) > 1:
            break
        else:
          truth_values[r][c] = next(iter(seen))
      if truth_values != intermediate_truth:
        intermediate_truth = copy.deepcopy(truth_values)
        common.Print(truth_values)
        print(f'\x1b[A\x1b[KConsidering {new_count} configurations.')
        time.sleep(.18)
    new_count = sum(map(len, work_cols)) + sum(map(len, work_rows))
    if old_count == new_count and old_truth == truth_values:
      if new_count == len(rows) + len(columns):
        break
      if any(len(work) == 0 for work in work_rows + work_cols):
        # We messed up. Restore the previous savestate and guess again.
        if not truth_stack:
          print('No solution.')
          break
        _, truth_values, work_rows, work_cols = truth_stack.pop()
      # Push the current state and make a guess.
      while True:
        r, worlds = random.choice(list(enumerate(work_rows)))
        if len(worlds) > 1:
          break
      new_truth = random.choice(list(worlds))
      c, val = random.choice(list(enumerate(new_truth)))
      truth_stack.append(((r,c,val), copy.deepcopy(truth_values), copy.deepcopy(work_rows), copy.deepcopy(work_cols)))
      truth_values[r][c] = val
    old_count = new_count
    old_truth = copy.deepcopy(truth_values)