vs = set() for anz in range(1, anz_kugeln // 2 + 1): for v2l in combinations(range(anz_kugeln), anz): for v2r in combinations(range(anz_kugeln), anz): if set(v2l) & set(v2r): continue if (v2r, v2l) in vs: continue vs.add((v2l, v2r)) e, text = prüfung(v1, v2m, v2l + v2r) if e: anz_lösungen += 1 if modus > 0: print( f'Lösung Nr. {anz_lösungen} für V2lr {v2l} <-> {v2r}' ) if modus > 1: print(text + '\n\n') if modus > 2: return print(f'Anzahl Lösungen für V2lr: {anz_lösungen}') start = pfc() stati = {True: {'?': '+', '-': '='}, False: {'?': '-', '+': '='}} anz_kugeln = 12 kugeln = [[nr, '?'] for nr in range(anz_kugeln)] v1 = [0, 1, 2, 3, 4, 5, 6, 7] v2m = [8, 9, 10, 0, 1, 2] prüfe_varianten(0) print(f'{pfc()-start:.2f} Sek.')
def load(file): with open(file) as f: return f.read() def cipher(c, sector_ID): return chr(97 + (ord(c) - 97 + sector_ID % 26) % 26) def solve(p, part1=0): pattern = r'([a-z-]+)(\d+)\[(\w+)\]' for encrypted_name, sector_ID, checksum in re.findall(pattern, p): encrypted_name = encrypted_name.replace('-', '') sector_ID = int(sector_ID) tops = [(-n, c) for c, n in Counter(encrypted_name).most_common()] ranked = ''.join([c for n, c in sorted(tops)]) if checksum == ranked[:5]: part1 += sector_ID decrypted_name = ''.join( [cipher(c, sector_ID) for c in encrypted_name]) if 'northpole' in decrypted_name: part2 = sector_ID return part1, part2 puzzle = load('Tag_04.txt') start = pfc() print(solve(puzzle), pfc() - start)
def main(data): start = pfc() print(solve(data.split('\n'))) print(pfc() - start)
from time import perf_counter as pfc #as dc dung nhu ham thay the start_time=pfc() # thay the perf_counter=pfc input('Moi nhap bat ki:') elapsed=pfc()-start_time print('Tgian ban dung:',elapsed,'giay')
MOVE_BB = bitboards.gen_move_bitboards() SLIDING_MOVE_BB = bitboards.gen_sliding_move_bb(MOVE_BB) SQUARE_BB = bitboards.gen_square_bb() ONLY1POS = {mask: i for i, mask in enumerate(SQUARE_BB)} ROCH_BB = { 'K': [1 << 61 | 1 << 62, (61, 62), (63, 61)], 'Q': [1 << 57 | 1 << 58 | 1 << 59, (59, 58), (56, 59)], 'k': [1 << 5 | 1 << 6, (5, 6), (7, 5)], 'q': [1 << 1 | 1 << 2 | 1 << 3, (3, 2), (0, 3)] } pieces_bb, all_pieces_bb, occupied_bb = bitboards.gen_pieces_bb(position) save_occ = occupied_bb save_all = all_pieces_bb.copy() start = pfc() for i in range(10_000): züge = zuggenerator(weiss, position) print(pfc() - start) züge = zuggenerator(weiss, position) for fig, von, zu, rochade, capture in züge: zug = '' if rochade and rochade in 'qQ': zug += 'O-O-O' elif rochade and rochade in 'kK': zug += 'O-O' else: zug += fig.upper() if fig not in 'pP' else '' zug += 'x' if capture else '' zug += i2A8(zu)
from time import perf_counter as pfc import heapq def read_puzzle(file): with open(file) as f: return {(x, y): int(n) for y, row in enumerate(f.read().split('\n')) for x, n in enumerate(row)} from heapq import heappush, heappop def dijkstra(grid, target, start=(0, 0), risk=0): queue = [(risk, start)] min_risk = {start: risk} visited = {start} while queue: risk, (x, y) = heapq.heappop(queue) if (x, y) == target: return risk for neigh in ((x + 1, y), (x, y + 1), (x - 1, y), (x, y - 1)): if neigh not in grid or neigh in visited: continue visited.add(neigh) new_risk = risk + grid[neigh] if new_risk < min_risk.get(neigh, 999999): min_risk[neigh] = new_risk heapq.heappush(queue, (new_risk, neigh)) def solve(puzzle): maxX, maxY = map(max, zip(*puzzle)) part1 = dijkstra(puzzle, (maxX, maxY))
keypad = { (2, 0): '1', (1, 1): '2', (2, 1): '3', (3, 1): '4', (0, 2): '5', (1, 2): '6', (2, 2): '7', (3, 2): '8', (4, 2): '9', (1, 3): 'A', (2, 3): 'B', (3, 3): 'C', (2, 4): 'D' } code = '' for zeile in puzzle: for c in zeile: if (pos2 := tuple(a + b for a, b in zip(dirs[c], pos))) in keypad: pos = pos2 code += keypad[pos] return code puzzle = load('Tag_02.txt') start = pfc() print(solve(puzzle), pfc() - start) start = pfc() print(solve(puzzle, False), pfc() - start)