From a622a2e25b820637361e8c04741089f8156d5b19 Mon Sep 17 00:00:00 2001 From: AKP Date: Wed, 22 Dec 2021 19:10:53 +0000 Subject: [PATCH] Add 2021-22 in Python (part one only) --- challenges/2021/22-reactorReboot/README.md | 2 + challenges/2021/22-reactorReboot/info.json | 21 ++++ .../22-reactorReboot/openscad-generate.py | 102 ++++++++++++++++++ .../2021/22-reactorReboot/py/__init__.py | 77 +++++++++++++ challenges/2021/README.md | 1 + 5 files changed, 203 insertions(+) create mode 100644 challenges/2021/22-reactorReboot/README.md create mode 100644 challenges/2021/22-reactorReboot/info.json create mode 100644 challenges/2021/22-reactorReboot/openscad-generate.py create mode 100644 challenges/2021/22-reactorReboot/py/__init__.py diff --git a/challenges/2021/22-reactorReboot/README.md b/challenges/2021/22-reactorReboot/README.md new file mode 100644 index 0000000..0665562 --- /dev/null +++ b/challenges/2021/22-reactorReboot/README.md @@ -0,0 +1,2 @@ +# [Day 22: Reactor Reboot](https://adventofcode.com/2021/day/22) + diff --git a/challenges/2021/22-reactorReboot/info.json b/challenges/2021/22-reactorReboot/info.json new file mode 100644 index 0000000..686d850 --- /dev/null +++ b/challenges/2021/22-reactorReboot/info.json @@ -0,0 +1,21 @@ +{ + "inputFile": "input.txt", + "testCases": { + "one": [ + { + "input": "on x=10..12,y=10..12,z=10..12\non x=11..13,y=11..13,z=11..13\noff x=9..11,y=9..11,z=9..11\non x=10..10,y=10..10,z=10..10", + "expected": "39" + }, + { + "input": "on x=-20..26,y=-36..17,z=-47..7\non x=-20..33,y=-21..23,z=-26..28\non x=-22..28,y=-29..23,z=-38..16\non x=-46..7,y=-6..46,z=-50..-1\non x=-49..1,y=-3..46,z=-24..28\non x=2..47,y=-22..22,z=-23..27\non x=-27..23,y=-28..26,z=-21..29\non x=-39..5,y=-6..47,z=-3..44\non x=-30..21,y=-8..43,z=-13..34\non x=-22..26,y=-27..20,z=-29..19\noff x=-48..-32,y=26..41,z=-47..-37\non x=-12..35,y=6..50,z=-50..-2\noff x=-48..-32,y=-32..-16,z=-15..-5\non x=-18..26,y=-33..15,z=-7..46\noff x=-40..-22,y=-38..-28,z=23..41\non x=-16..35,y=-41..10,z=-47..6\noff x=-32..-23,y=11..30,z=-14..3\non x=-49..-5,y=-3..45,z=-29..18\noff x=18..30,y=-20..-8,z=-3..13\non x=-41..9,y=-7..43,z=-33..15\non x=-54112..-39298,y=-85059..-49293,z=-27449..7877\non x=967..23432,y=45373..81175,z=27513..53682\n", + "expected": "590784" + } + ], + "two": [ + { + "input": "on x=-5..47,y=-31..22,z=-19..33\non x=-44..5,y=-27..21,z=-14..35\non x=-49..-1,y=-11..42,z=-10..38\non x=-20..34,y=-40..6,z=-44..1\noff x=26..39,y=40..50,z=-2..11\non x=-41..5,y=-41..6,z=-36..8\noff x=-43..-33,y=-45..-28,z=7..25\non x=-33..15,y=-32..19,z=-34..11\noff x=35..47,y=-46..-34,z=-11..5\non x=-14..36,y=-6..44,z=-16..29\non x=-57795..-6158,y=29564..72030,z=20435..90618\non x=36731..105352,y=-21140..28532,z=16094..90401\non x=30999..107136,y=-53464..15513,z=8553..71215\non x=13528..83982,y=-99403..-27377,z=-24141..23996\non x=-72682..-12347,y=18159..111354,z=7391..80950\non x=-1060..80757,y=-65301..-20884,z=-103788..-16709\non x=-83015..-9461,y=-72160..-8347,z=-81239..-26856\non x=-52752..22273,y=-49450..9096,z=54442..119054\non x=-29982..40483,y=-108474..-28371,z=-24328..38471\non x=-4958..62750,y=40422..118853,z=-7672..65583\non x=55694..108686,y=-43367..46958,z=-26781..48729\non x=-98497..-18186,y=-63569..3412,z=1232..88485\non x=-726..56291,y=-62629..13224,z=18033..85226\non x=-110886..-34664,y=-81338..-8658,z=8914..63723\non x=-55829..24974,y=-16897..54165,z=-121762..-28058\non x=-65152..-11147,y=22489..91432,z=-58782..1780\non x=-120100..-32970,y=-46592..27473,z=-11695..61039\non x=-18631..37533,y=-124565..-50804,z=-35667..28308\non x=-57817..18248,y=49321..117703,z=5745..55881\non x=14781..98692,y=-1341..70827,z=15753..70151\non x=-34419..55919,y=-19626..40991,z=39015..114138\non x=-60785..11593,y=-56135..2999,z=-95368..-26915\non x=-32178..58085,y=17647..101866,z=-91405..-8878\non x=-53655..12091,y=50097..105568,z=-75335..-4862\non x=-111166..-40997,y=-71714..2688,z=5609..50954\non x=-16602..70118,y=-98693..-44401,z=5197..76897\non x=16383..101554,y=4615..83635,z=-44907..18747\noff x=-95822..-15171,y=-19987..48940,z=10804..104439\non x=-89813..-14614,y=16069..88491,z=-3297..45228\non x=41075..99376,y=-20427..49978,z=-52012..13762\non x=-21330..50085,y=-17944..62733,z=-112280..-30197\non x=-16478..35915,y=36008..118594,z=-7885..47086\noff x=-98156..-27851,y=-49952..43171,z=-99005..-8456\noff x=2032..69770,y=-71013..4824,z=7471..94418\non x=43670..120875,y=-42068..12382,z=-24787..38892\noff x=37514..111226,y=-45862..25743,z=-16714..54663\noff x=25699..97951,y=-30668..59918,z=-15349..69697\noff x=-44271..17935,y=-9516..60759,z=49131..112598\non x=-61695..-5813,y=40978..94975,z=8655..80240\noff x=-101086..-9439,y=-7088..67543,z=33935..83858\noff x=18020..114017,y=-48931..32606,z=21474..89843\noff x=-77139..10506,y=-89994..-18797,z=-80..59318\noff x=8476..79288,y=-75520..11602,z=-96624..-24783\non x=-47488..-1262,y=24338..100707,z=16292..72967\noff x=-84341..13987,y=2429..92914,z=-90671..-1318\noff x=-37810..49457,y=-71013..-7894,z=-105357..-13188\noff x=-27365..46395,y=31009..98017,z=15428..76570\noff x=-70369..-16548,y=22648..78696,z=-1892..86821\non x=-53470..21291,y=-120233..-33476,z=-44150..38147\noff x=-93533..-4276,y=-16170..68771,z=-104985..-24507\n", + "expected": "2758514936282235" + } + ] + } +} \ No newline at end of file diff --git a/challenges/2021/22-reactorReboot/openscad-generate.py b/challenges/2021/22-reactorReboot/openscad-generate.py new file mode 100644 index 0000000..4872bea --- /dev/null +++ b/challenges/2021/22-reactorReboot/openscad-generate.py @@ -0,0 +1,102 @@ +from dataclasses import dataclass +from typing import List, Tuple, NamedTuple, Generator + + +# This script exists to generate OpenSCAD code to represent challenge inputs. + + +MODE_ON = "on" +MODE_OFF = "off" + + +class Point(NamedTuple): + x: int + y: int + z: int + + def to_list(self) -> List[int]: + return [self.x, self.y, self.z] + +def apply_point_shift(point: List[int], shift: Tuple[int, int, int]) -> List[int]: + x, y, z = point + xs, ys, zs = shift + return [x+abs(xs), y+abs(ys), z+abs(zs)] + +@dataclass +class Shape: + mode: str + p1: Point + p2: Point + + def __init__(self, mode, x, y, z: str): + self.mode = mode + + x1, x2 = [int(a) for a in x.split("..")] + y1, y2 = [int(a) for a in y.split("..")] + z1, z2 = [int(a) for a in z.split("..")] + + self.p1 = Point(x1, y1, z1) + self.p2 = Point(x2, y2, z2) + + def get_contained_points(self) -> Generator[Point, None, None]: + for x in range(self.p1.x, self.p2.x+1): + for y in range(self.p1.y, self.p2.y+1): + for z in range(self.p1.z, self.p2.z+1): + yield Point(x, y, z) + + def verticies(self) -> List[Point]: + p1 = self.p1 + p2 = Point(self.p2.x + 1, self.p2.y + 1, self.p2.z + 1) + return [ + p1, + Point(p2.x, p1.y, p1.x), + Point(p2.x, p2.y, p1.x), + Point(p1.x, p2.y, p1.x), + Point(p1.x, p1.y, p2.x), + Point(p2.x, p1.y, p2.x), + p2, + Point(p1.x, p2.y, p2.x), + ] + + def openscad(self) -> str: + shift = self.p1.to_list() + o = "cube(" + o += str(Point(self.p2.x+1-self.p1.x, self.p2.y+1-self.p1.y, self.p2.z+1-self.p1.z).to_list()) + o += ", center=false);" + if shift[0] != 0 and shift[1] != 0 and shift[2] != 0: + o = "translate(" + str(shift) + ") {\n" + o + "\n};" + return o + + +def parse(instr: str) -> List[Shape]: + o = [] + for line in instr.strip().splitlines(): + sp = line.split(" ") + x, y, z = [x.split("=")[-1] for x in sp[-1].split(",")] + o.append(Shape(sp[0], x, y, z)) + return o + +def is_point_out_of_bounds(p: Point) -> bool: + return not (-50 <= p.x <= 50 and -50 <= p.y <= 50 and -50 <= p.z <= 50) + +def main(): + shapes = parse(open("input.txt").read()) + + current_program = shapes[0].openscad() + for shape in shapes[1:]: + + # if is_point_out_of_bounds(shape.p1) and is_point_out_of_bounds(shape.p2): + # continue + + function = "" + if shape.mode == MODE_ON: + function = "union" + else: + function = "difference" + + current_program = function + "() {\n" + current_program + "\n" + shape.openscad() + "\n};" + + print(current_program) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/challenges/2021/22-reactorReboot/py/__init__.py b/challenges/2021/22-reactorReboot/py/__init__.py new file mode 100644 index 0000000..09ce226 --- /dev/null +++ b/challenges/2021/22-reactorReboot/py/__init__.py @@ -0,0 +1,77 @@ +from dataclasses import dataclass +from typing import Any, List, NamedTuple, Generator +from aocpy import BaseChallenge + + +MODE_ON = "on" +MODE_OFF = "off" + + +class Point(NamedTuple): + x: int + y: int + z: int + + +@dataclass +class Shape: + mode: str + p1: Point + p2: Point + + def __init__(self, mode, x, y, z: str): + self.mode = mode + + x1, x2 = [int(a) for a in x.split("..")] + y1, y2 = [int(a) for a in y.split("..")] + z1, z2 = [int(a) for a in z.split("..")] + + self.p1 = Point(x1, y1, z1) + self.p2 = Point(x2, y2, z2) + + def get_contained_points(self) -> Generator[Point, None, None]: + for x in range(self.p1.x, self.p2.x+1): + for y in range(self.p1.y, self.p2.y+1): + for z in range(self.p1.z, self.p2.z+1): + yield Point(x, y, z) + + +def parse(instr: str) -> List[Shape]: + o = [] + for line in instr.strip().splitlines(): + sp = line.split(" ") + x, y, z = [x.split("=")[-1] for x in sp[-1].split(",")] + o.append(Shape(sp[0], x, y, z)) + return o + + +def is_point_out_of_bounds(p: Point) -> bool: + return not (-50 <= p.x <= 50 and -50 <= p.y <= 50 and -50 <= p.z <= 50) + + +class Challenge(BaseChallenge): + + @staticmethod + def one(instr: str) -> int: + shapes = parse(instr) + points = {} + for shape in shapes: + + if is_point_out_of_bounds(shape.p1) and is_point_out_of_bounds(shape.p2): + continue + + for p in shape.get_contained_points(): + + if is_point_out_of_bounds(p): + continue + + if (p in points) and (shape.mode == MODE_OFF): + del points[p] + elif (p not in points) and (shape.mode == MODE_ON): + points[p] = True + + return len(points) + + @staticmethod + def two(instr: str) -> Any: + raise NotImplementedError diff --git a/challenges/2021/README.md b/challenges/2021/README.md index 772ff27..9ffaeef 100644 --- a/challenges/2021/README.md +++ b/challenges/2021/README.md @@ -33,6 +33,7 @@ Solutions to the [2021 Advent of Code](https://adventofcode.com/2021). | 19 - Beacon Scanner | Unattempted | | | | 20 - Trench Map \* | ★ ★ | [Python](20-trenchMap/py), [Nim](20-trenchMap/nim) | Took a moment to realise that the infinite grid alternates between lit and unlit, and even then I had to look at someone else's solution to realise it. | | 21 - Dirac Dice | ★ ☆ | [Python](21-diracDice/py) | Couldn't be bothered with part two - I've got too much going on at the moment to spend a lot of time thinking about it. | +| 22 - Reactor Reboot | ★ ☆ | [Python](22-reactorReboot/py) | Once again - couldn't be bothered with part two. |