Skip to content

Instantly share code, notes, and snippets.

@Mu-adventofcode
Created June 23, 2022 17:24
Show Gist options
  • Save Mu-adventofcode/f421b4ab9b6dd5f45623b6cf82ecb051 to your computer and use it in GitHub Desktop.
Save Mu-adventofcode/f421b4ab9b6dd5f45623b6cf82ecb051 to your computer and use it in GitHub Desktop.
Advent of Code 2021 day 20
TRANSLATION = str.maketrans({"#": "1", ".": "0"})
def index(y, x, img):
bitstr = "".join(
img[yy][xx] for yy in (y - 1, y, y + 1) for xx in (x - 1, x, x + 1)
).translate(TRANSLATION)
return int(bitstr, 2)
def enhance(img, ch):
extra = [[ch] * len(img[0])] * 2
img_ext = [[ch, ch] + row + [ch, ch] for row in extra + img + extra]
img_out = [
[algo[index(ir, ic, img_ext)] for ic in range(1, len(img_ext[0]) - 1)]
for ir in range(1, len(img_ext) - 1)
]
ch_out = algo[index(1, 1, [[ch] * 3] * 3)]
return img_out, ch_out
algo, chunk = open("input_20.txt").read().strip().split("\n\n")
image = [list(line) for line in chunk.splitlines()]
image, char = enhance(image, ".")
image, char = enhance(image, char)
print("part 1:", sum(row.count("#") for row in image))
for _ in range(48):
image, char = enhance(image, char)
print("part 2:", sum(row.count("#") for row in image))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment