diff --git a/2021/day22/README.txt b/2021/day22/README.txt index 1e975bd..afe8cc7 100644 --- a/2021/day22/README.txt +++ b/2021/day22/README.txt @@ -108,3 +108,79 @@ on x=967..23432,y=45373..81175,z=27513..53682 The last two steps are fully outside the initialization procedure area; all other steps are fully within it. After executing these steps in the initialization procedure region, 590784 cubes are on. Execute the reboot steps. Afterward, considering only cubes in the region x=-50..50,y=-50..50,z=-50..50, how many cubes are on? + +Your puzzle answer was 606484. + +The first half of this puzzle is complete! It provides one gold star: * +--- Part Two --- + +Now that the initialization procedure is complete, you can reboot the reactor. + +Starting with all cubes off, run all of the reboot steps for all cubes in the reactor. + +Consider the following reboot steps: + +on x=-5..47,y=-31..22,z=-19..33 +on x=-44..5,y=-27..21,z=-14..35 +on x=-49..-1,y=-11..42,z=-10..38 +on x=-20..34,y=-40..6,z=-44..1 +off x=26..39,y=40..50,z=-2..11 +on x=-41..5,y=-41..6,z=-36..8 +off x=-43..-33,y=-45..-28,z=7..25 +on x=-33..15,y=-32..19,z=-34..11 +off x=35..47,y=-46..-34,z=-11..5 +on x=-14..36,y=-6..44,z=-16..29 +on x=-57795..-6158,y=29564..72030,z=20435..90618 +on x=36731..105352,y=-21140..28532,z=16094..90401 +on x=30999..107136,y=-53464..15513,z=8553..71215 +on x=13528..83982,y=-99403..-27377,z=-24141..23996 +on x=-72682..-12347,y=18159..111354,z=7391..80950 +on x=-1060..80757,y=-65301..-20884,z=-103788..-16709 +on x=-83015..-9461,y=-72160..-8347,z=-81239..-26856 +on x=-52752..22273,y=-49450..9096,z=54442..119054 +on x=-29982..40483,y=-108474..-28371,z=-24328..38471 +on x=-4958..62750,y=40422..118853,z=-7672..65583 +on x=55694..108686,y=-43367..46958,z=-26781..48729 +on x=-98497..-18186,y=-63569..3412,z=1232..88485 +on x=-726..56291,y=-62629..13224,z=18033..85226 +on x=-110886..-34664,y=-81338..-8658,z=8914..63723 +on x=-55829..24974,y=-16897..54165,z=-121762..-28058 +on x=-65152..-11147,y=22489..91432,z=-58782..1780 +on x=-120100..-32970,y=-46592..27473,z=-11695..61039 +on x=-18631..37533,y=-124565..-50804,z=-35667..28308 +on x=-57817..18248,y=49321..117703,z=5745..55881 +on x=14781..98692,y=-1341..70827,z=15753..70151 +on x=-34419..55919,y=-19626..40991,z=39015..114138 +on x=-60785..11593,y=-56135..2999,z=-95368..-26915 +on x=-32178..58085,y=17647..101866,z=-91405..-8878 +on x=-53655..12091,y=50097..105568,z=-75335..-4862 +on x=-111166..-40997,y=-71714..2688,z=5609..50954 +on x=-16602..70118,y=-98693..-44401,z=5197..76897 +on x=16383..101554,y=4615..83635,z=-44907..18747 +off x=-95822..-15171,y=-19987..48940,z=10804..104439 +on x=-89813..-14614,y=16069..88491,z=-3297..45228 +on x=41075..99376,y=-20427..49978,z=-52012..13762 +on x=-21330..50085,y=-17944..62733,z=-112280..-30197 +on x=-16478..35915,y=36008..118594,z=-7885..47086 +off x=-98156..-27851,y=-49952..43171,z=-99005..-8456 +off x=2032..69770,y=-71013..4824,z=7471..94418 +on x=43670..120875,y=-42068..12382,z=-24787..38892 +off x=37514..111226,y=-45862..25743,z=-16714..54663 +off x=25699..97951,y=-30668..59918,z=-15349..69697 +off x=-44271..17935,y=-9516..60759,z=49131..112598 +on x=-61695..-5813,y=40978..94975,z=8655..80240 +off x=-101086..-9439,y=-7088..67543,z=33935..83858 +off x=18020..114017,y=-48931..32606,z=21474..89843 +off x=-77139..10506,y=-89994..-18797,z=-80..59318 +off x=8476..79288,y=-75520..11602,z=-96624..-24783 +on x=-47488..-1262,y=24338..100707,z=16292..72967 +off x=-84341..13987,y=2429..92914,z=-90671..-1318 +off x=-37810..49457,y=-71013..-7894,z=-105357..-13188 +off x=-27365..46395,y=31009..98017,z=15428..76570 +off x=-70369..-16548,y=22648..78696,z=-1892..86821 +on x=-53470..21291,y=-120233..-33476,z=-44150..38147 +off x=-93533..-4276,y=-16170..68771,z=-104985..-24507 + +After running the above reboot steps, 2758514936282235 cubes are on. (Just for fun, 474140 of those are also in the initialization procedure region.) + +Starting again with all cubes off, execute all reboot steps. Afterward, considering all cubes, how many cubes are on? diff --git a/2021/day22/aoc-c.c b/2021/day22/aoc-c.c new file mode 100644 index 0000000..63f4f04 --- /dev/null +++ b/2021/day22/aoc-c.c @@ -0,0 +1,154 @@ +/* aoc-c.c: Advent of Code 2021, day 21 parts 1 & 2 + * + * Copyright (C) 2022 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include +#include +#include + +#include "pool.h" +#include "debug.h" +#include "bits.h" +#include "list.h" + +typedef struct step { + int onoff; + int x[2], y[2], z[2]; + struct list_head list_step; +} step_t; + +LIST_HEAD(list_step); + +pool_t *pool_step; + +#define MAX(x,y) ((x) > (y) ? (x) : (y)) +#define MIN(x,y) ((x) < (y) ? (x) : (y)) + +static void print_steps() +{ + step_t *cur; + int nlines = 1; + + list_for_each_entry(cur, &list_step, list_step) { + log(1, "%d: %s x=(%d,%d) y=(%d,%d) z=(%d,%d)\n", + nlines++, + cur->onoff? "on": "off", + cur->x[0], cur->x[1], + cur->y[0], cur->y[1], + cur->z[0], cur->z[1]); + } +} + +static int read_input() +{ + char onoff[5]; + int nlines = 0; + step_t input, *cur; + + pool_step = pool_create("steps", 512, sizeof(step_t)); + while (scanf("%5s x=%d..%d,y=%d..%d,z=%d..%d\n", onoff, + &input.x[0], &input.x[1], + &input.y[0], &input.y[1], + &input.z[0], &input.z[1]) == 7) { + cur = pool_get(pool_step); + *cur = input; + cur->onoff = onoff[1] == 'n'; + list_add_tail(&cur->list_step, &list_step); + nlines++; + } + return nlines; +} + +static int part1() +{ + step_t *cur; + static char cuboid[101][101][101]; + u64 res = 0; + +/* log(1, "%s x=(%d,%d) y=(%d,%d) z=(%d,%d)\n", + cur->onoff? "on": "off", + cur->x[0], cur->x[1], + cur->y[0], cur->y[1], + cur->z[0], cur->z[1]); +*/ + + list_for_each_entry(cur, &list_step, list_step) { + int x1, x2, y1, y2, z1, z2; + + x1 = MAX(cur->x[0], -50); + x2 = MIN(cur->x[1], 50); + + y1 = MAX(cur->y[0], -50); + y2 = MIN(cur->y[1], 50); + + z1 = MAX(cur->z[0], -50); + z2 = MIN(cur->z[1], 50); + + for (int x = x1; x <= x2; ++x) { + for (int y = y1; y <= y2; ++y) { + for (int z = z1; z <= z2; ++z) { + log(1, "(%d,%d,%d)=%d\n", x, y, z, cur->onoff); + cuboid[x+50][y+50][z+50] = cur->onoff; + } + } + } + } + + for (int x = 0; x < 101; ++x) { + for (int y = 0; y < 101; ++y) { + for (int z = 0; z < 101; ++z) { + res += cuboid[x][y][z]; + } + } + } + return res; +} + +static int part2() +{ + return 1; + +} + +static int usage(char *prg) +{ + fprintf(stderr, "Usage: %s [-d debug_level] [-p part]\n", prg); + return 1; +} + +int main(int ac, char **av) +{ + int opt, part = 1; + + while ((opt = getopt(ac, av, "d:p:")) != -1) { + switch (opt) { + case 'd': + debug_level_set(atoi(optarg)); + break; + case 'p': /* 1 or 2 */ + part = atoi(optarg); + if (part < 1 || part > 2) + return usage(*av); + break; + default: + return usage(*av); + } + } + if (optind < ac) + return usage(*av); + + read_input(); + print_steps(); + printf("%s : res=%d\n", *av, part == 1? part1(): part2()); + + exit(0); +}