def go(inp): rows = [] for i in range(128): rows.append(hash(f"{inp}-{i}")) grid = [] n = 0 for row in rows: binary = f"{int(row,16):0128b}" grid.append(list(binary)) n += len(re.findall("1", binary)) print(n) print(count_regions(grid))
def to_bin_string(hex): scale = 16 ## equals to hexadecimal num_of_bits = 128 return bin(int(hex, scale))[2:].zfill(num_of_bits) total_used_squares = 0 hash_table = [] for i in range(128): hash_string_for_row = ('%s-%d') % (INPUT_STRING, i) h = knothash.hash(hash_string_for_row) binary_expansion = to_bin_string(h) hash_table.append(list(binary_expansion)) total_used_squares += binary_expansion.count('1') # Now we need to count the groups total_groups = 0 def get(i, j): return hash_table[j][i]
from knothash import hash print hash(open('input.txt').read().strip())
""" if i < 0 or j < 0 or i > 127 or j > 127: return False if grid[i][j] != '1': return False grid[i][j] = char regionify(i + 1, j, char) regionify(i, j + 1, char) regionify(i - 1, j, char) regionify(i, j - 1, char) return True def solve(grid): squares_used = sum(row.count('1') for row in grid) return squares_used def solve2(): region_count = 0 seen_char = '.' for i in range(128): for j in range(128): if regionify(i, j, seen_char): region_count += 1 return region_count key = "vbqugkhl" grid = [bits(knothash.hash(key + "-{}".format(i))) for i in range(128)] print(solve(grid)) # 8148 print(solve2()) # 1180
import sys sys.path.append('../day10') import knothash def binary(hex_str): return str(bin(int(hex_str, 16)))[2:] key = 'oundnydw' on_count = 0 for i in range(128): hash = knothash.hash(key + '-' + str(i)) on_count += len([bit for bit in binary(hash) if bit is '1']) print on_count
return str(bin(int(hex_str, 16)))[2:].rjust(128, '0') def clean_region(grid, x, y): grid[y][x] = False if x > 0 and grid[y][x-1]: clean_region(grid, x-1, y) if x < 127 and grid[y][x+1]: clean_region(grid, x+1, y) if y > 0 and grid[y-1][x]: clean_region(grid, x, y-1) if y < 127 and grid[y+1][x]: clean_region(grid, x, y+1) key = 'oundnydw' grid = [] for i in range(128): hash = knothash.hash(key + '-' + str(i)) grid.append([bit is '1' for bit in binary(hash)]) region_count = 0 for cell_y, line in enumerate(grid): for cell_x, cell in enumerate(line): if cell: region_count += 1 clean_region(grid, cell_x, cell_y) print region_count