#!/usr/bin/env python3
#Advent of Code
#2021 Day 20, Part 1
#Solution by James C. (https://github.com/JamesMCo)
importos,syssys.path.append(os.path.abspath("../.."))importunittest,util.readfromutil.testsimportrunfromutil.two_d_worldimportWorlddefsolve(puzzle_input):rules=puzzle_input[0]image=World(".",True)image.load_from_lists(puzzle_input[2:])# If rule 0 is a lit pixel, and rule 511 is a dark pixel,
# then the infinite space will alternate between all on
# and all off. To simulate this, we can have the default
# value for an unexplored cell in the World represent
# whichever state the infinite space should contain. As
# a result, any values pulled from the World within the
# unexplored infinite space will contain the correct value.
# Whether we should do this is determined by the following
# condition:
alternates=rules[0]=="#"andrules[-1]=="."forstepinrange(1,3):new_image=[]foryinrange(image.min_y-1,image.max_y+2):new_row=""forxinrange(image.min_x-1,image.max_x+2):points=["1"ifimage[(x+dx,y+dy)]=="#"else"0"fordyinrange(-1,2)fordxinrange(-1,2)]new_row+=rules[int("".join(points),2)]new_image.append(new_row)image=World(".#"[alternatesandstep%2],True)image.load_from_lists(new_image)returnlen([vforvinimage.values()ifv=="#"])defmain():puzzle_input=util.read.as_lines()pixels=solve(puzzle_input)print("The number of pixels that are lit in the resulting image is "+str(pixels)+".")classAOC_Tests(unittest.TestCase):deftest_ex1(self):returnself.assertEqual(solve(["..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#","","#..#.","#....","##..#","..#..","..###"]),35)if__name__=="__main__":run(main)
#!/usr/bin/env python3
#Advent of Code
#2021 Day 20, Part 2
#Solution by James C. (https://github.com/JamesMCo)
importos,syssys.path.append(os.path.abspath("../.."))importunittest,util.readfromutil.testsimportrunfromutil.two_d_worldimportWorlddefsolve(puzzle_input):rules=puzzle_input[0]image=World(".",True)image.load_from_lists(puzzle_input[2:])# If rule 0 is a lit pixel, and rule 511 is a dark pixel,
# then the infinite space will alternate between all on
# and all off. To simulate this, we can have the default
# value for an unexplored cell in the World represent
# whichever state the infinite space should contain. As
# a result, any values pulled from the World within the
# unexplored infinite space will contain the correct value.
# Whether we should do this is determined by the following
# condition:
alternates=rules[0]=="#"andrules[-1]=="."forstepinrange(1,51):new_image=[]foryinrange(image.min_y-1,image.max_y+2):new_row=""forxinrange(image.min_x-1,image.max_x+2):points=["1"ifimage[(x+dx,y+dy)]=="#"else"0"fordyinrange(-1,2)fordxinrange(-1,2)]new_row+=rules[int("".join(points),2)]new_image.append(new_row)image=World(".#"[alternatesandstep%2],True)image.load_from_lists(new_image)returnlen([vforvinimage.values()ifv=="#"])defmain():puzzle_input=util.read.as_lines()pixels=solve(puzzle_input)print("The number of pixels that are lit in the resulting image is "+str(pixels)+".")classAOC_Tests(unittest.TestCase):deftest_ex1(self):returnself.assertEqual(solve(["..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#","","#..#.","#....","##..#","..#..","..###"]),3351)if__name__=="__main__":run(main)