from lib import Grid2D from d10 import part_2 as hash def to_bin(x): assert len(x) == 1 xv = int(x, 16) c = 1 r = "" for _ in range(4): if c & xv > 0: r = "1" + r else: r = "0" + r c = (c << 1) return r def to_bits(xs): r = "" for x in xs: r += to_bin(x) return r def part_1(data): r = 0 for i in range(128): s = f"{data}-{i}" x = hash(s) b = to_bits(x) r += b.count("1") print(r) def part_2(data): grid = "" for i in range(128): s = f"{data}-{i}" x = hash(s) grid += to_bits(x) grid += "\n" g = Grid2D(grid) one_group_count = 0 seen_ones = set() all_ones = g.find("1") for c in all_ones: if c in seen_ones: continue one_group_count += 1 current_group = [c] while current_group: c = current_group.pop() if c in seen_ones: continue seen_ones.add(c) for nb in g.neighbors_ort(c): if g[nb] == "1": current_group.append(nb) print(one_group_count) def main(): data = open(0).read().strip() part_1(data) part_2(data) if __name__ == "__main__": main()