From 5ad5c87fd8b2a14b81715e48badf12b9ebe7254c Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Sat, 1 Apr 2023 13:54:28 +0200 Subject: [PATCH] 2022: init day 16 --- 2022/day16/Makefile | 111 +++++++++++++++++++++ 2022/day16/README.org | 183 +++++++++++++++++++++++++++++++++++ 2022/day16/aoc.h | 17 ++++ 2022/day16/common.bash | 68 +++++++++++++ 2022/day16/common.c | 59 +++++++++++ 2022/day16/input/example.txt | 10 ++ 2022/day16/input/input.txt | 54 +++++++++++ 7 files changed, 502 insertions(+) create mode 100644 2022/day16/Makefile create mode 100644 2022/day16/README.org create mode 100644 2022/day16/aoc.h create mode 100644 2022/day16/common.bash create mode 100644 2022/day16/common.c create mode 100644 2022/day16/input/example.txt create mode 100644 2022/day16/input/input.txt diff --git a/2022/day16/Makefile b/2022/day16/Makefile new file mode 100644 index 0000000..9091528 --- /dev/null +++ b/2022/day16/Makefile @@ -0,0 +1,111 @@ +# AOC daily Makefile - GNU make only. +# +# Copyright (C) 2021-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 +# + +INPUT := input/input.txt +SHELL := /bin/bash + +CC := gcc +BEAR := bear +CCLSFILE:= compile_commands.json + +LIB := aoc_$(shell uname -m) +INCDIR := ../include +LIBDIR := ../lib +LDFLAGS := -L$(LIBDIR) +#LDLIB := -l$(LIB) -lm +LDLIB := -l$(LIB) + +export LD_LIBRARY_PATH = $(LIBDIR) + +CFLAGS += -std=gnu11 +CFLAGS += -O2 +CFLAGS += -g +# for gprof +# CFLAGS += -pg +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -march=native +# Next one may be useful for valgrind (some invalid instructions) +# CFLAGS += -mno-tbm +CFLAGS += -Wmissing-declarations +CFLAGS += -Wno-unused-result + +CFLAGS += -DDEBUG_DEBUG # activate general debug (debug.c) +CFLAGS += -DDEBUG_POOL # memory pools management + +VALGRIND := valgrind +VALGRINDFLAGS := --leak-check=full --show-leak-kinds=all --track-origins=yes \ + --sigill-diagnostics=yes --quiet --show-error-list=yes + + +TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n" +export PATH := .:$(PATH) + +.PHONY: clean all compile assembly memcheck memcheck1 memcheck2 part1 part2 ccls bear org + +all: README.org ccls part1 part2 + +memcheck: memcheck1 memcheck2 + +memcheck1: aoc-c + @$(VALGRIND) $(VALGRINDFLAGS) aoc-c -p 1 < $(INPUT) + +memcheck2: aoc-c + @$(VALGRIND) $(VALGRINDFLAGS) aoc-c -p 2 < $(INPUT) + @#@valgrind -s --track-origins=yes aoc-c -p 2 < $(INPUT) + +compile: aoc-c + +cpp: aoc-c.i + +assembly: aoc-c.s + +part1: aoc-c + @#$(TIME) aoc.bash -p 1 < $(INPUT) 2>&1 + @$(TIME) aoc-c -p 1 < $(INPUT) + +part2: aoc-c + @#$(TIME) aoc.bash -p 2 < $(INPUT) 2>&1 + @$(TIME) aoc-c -p 2 < $(INPUT) + +ccls: $(CCLSFILE) + +clean: + @rm -f aoc-c core* vgcore* gmon.out aoc-c.s aoc-c.i README.html compile_commands.json + +aoc-c: aoc-c.c common.c + @echo compiling $< + $(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $^ $(LDLIB) -o $@ + +# generate pre-processed file (.i) and assembler (.s) +%.i: %.c + @echo generating $@ + @$(CC) -E $(CFLAGS) -I $(INCDIR) $< -o $@ + +%.s: %.c + @echo generating $@ + @$(CC) -S -fverbose-asm $(CFLAGS) -I $(INCDIR) $< -o $@ + +# generate README.org from README.html (must cleanup !) +org: README.org + +%.org: %.html + @echo generating $@. Cleanup before commit ! + @pandoc $< -o $@ + +# generate compile_commands.json +$(CCLSFILE): aoc-c.c Makefile + $(BEAR) -- make clean compile + +bear: clean + @touch .ccls-root + @$(BEAR) -- make compile diff --git a/2022/day16/README.org b/2022/day16/README.org new file mode 100644 index 0000000..25a4aec --- /dev/null +++ b/2022/day16/README.org @@ -0,0 +1,183 @@ +** --- Day 16: Proboscidea Volcanium --- +The sensors have led you to the origin of the distress signal: yet +another handheld device, just like the one the Elves gave you. However, +you don't see any Elves around; instead, the device is surrounded by +elephants! They must have gotten lost in these tunnels, and one of the +elephants apparently figured out how to turn on the distress signal. + +The ground rumbles again, much stronger this time. What kind of cave is +this, exactly? You scan the cave with your handheld device; it reports +mostly igneous rock, some ash, pockets of pressurized gas, magma... this +isn't just a cave, it's a volcano! + +You need to get the elephants out of here, quickly. Your device +estimates that you have /30 minutes/ before the volcano erupts, so you +don't have time to go back out the way you came in. + +You scan the cave for other options and discover a network of pipes and +pressure-release /valves/. You aren't sure how such a system got into a +volcano, but you don't have time to complain; your device produces a +report (your puzzle input) of each valve's /flow rate/ if it were opened +(in pressure per minute) and the tunnels you could use to move between +the valves. + +There's even a valve in the room you and the elephants are currently +standing in labeled =AA=. You estimate it will take you one minute to +open a single valve and one minute to follow any tunnel from one valve +to another. What is the most pressure you could release? + +For example, suppose you had the following scan output: + +#+begin_example +Valve AA has flow rate=0; tunnels lead to valves DD, II, BB +Valve BB has flow rate=13; tunnels lead to valves CC, AA +Valve CC has flow rate=2; tunnels lead to valves DD, BB +Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE +Valve EE has flow rate=3; tunnels lead to valves FF, DD +Valve FF has flow rate=0; tunnels lead to valves EE, GG +Valve GG has flow rate=0; tunnels lead to valves FF, HH +Valve HH has flow rate=22; tunnel leads to valve GG +Valve II has flow rate=0; tunnels lead to valves AA, JJ +Valve JJ has flow rate=21; tunnel leads to valve II +#+end_example + +All of the valves begin /closed/. You start at valve =AA=, but it must +be damaged or jammed or something: its flow rate is =0=, so there's no +point in opening it. However, you could spend one minute moving to valve +=BB= and another minute opening it; doing so would release pressure +during the remaining /28 minutes/ at a flow rate of =13=, a total +eventual pressure release of =28 * 13 = 364=. Then, you could spend your +third minute moving to valve =CC= and your fourth minute opening it, +providing an additional /26 minutes/ of eventual pressure release at a +flow rate of =2=, or =52= total pressure released by valve =CC=. + +Making your way through the tunnels like this, you could probably open +many or all of the valves by the time 30 minutes have elapsed. However, +you need to release as much pressure as possible, so you'll need to be +methodical. Instead, consider this approach: + +#+begin_example +== Minute 1 == +No valves are open. +You move to valve DD. + +== Minute 2 == +No valves are open. +You open valve DD. + +== Minute 3 == +Valve DD is open, releasing 20 pressure. +You move to valve CC. + +== Minute 4 == +Valve DD is open, releasing 20 pressure. +You move to valve BB. + +== Minute 5 == +Valve DD is open, releasing 20 pressure. +You open valve BB. + +== Minute 6 == +Valves BB and DD are open, releasing 33 pressure. +You move to valve AA. + +== Minute 7 == +Valves BB and DD are open, releasing 33 pressure. +You move to valve II. + +== Minute 8 == +Valves BB and DD are open, releasing 33 pressure. +You move to valve JJ. + +== Minute 9 == +Valves BB and DD are open, releasing 33 pressure. +You open valve JJ. + +== Minute 10 == +Valves BB, DD, and JJ are open, releasing 54 pressure. +You move to valve II. + +== Minute 11 == +Valves BB, DD, and JJ are open, releasing 54 pressure. +You move to valve AA. + +== Minute 12 == +Valves BB, DD, and JJ are open, releasing 54 pressure. +You move to valve DD. + +== Minute 13 == +Valves BB, DD, and JJ are open, releasing 54 pressure. +You move to valve EE. + +== Minute 14 == +Valves BB, DD, and JJ are open, releasing 54 pressure. +You move to valve FF. + +== Minute 15 == +Valves BB, DD, and JJ are open, releasing 54 pressure. +You move to valve GG. + +== Minute 16 == +Valves BB, DD, and JJ are open, releasing 54 pressure. +You move to valve HH. + +== Minute 17 == +Valves BB, DD, and JJ are open, releasing 54 pressure. +You open valve HH. + +== Minute 18 == +Valves BB, DD, HH, and JJ are open, releasing 76 pressure. +You move to valve GG. + +== Minute 19 == +Valves BB, DD, HH, and JJ are open, releasing 76 pressure. +You move to valve FF. + +== Minute 20 == +Valves BB, DD, HH, and JJ are open, releasing 76 pressure. +You move to valve EE. + +== Minute 21 == +Valves BB, DD, HH, and JJ are open, releasing 76 pressure. +You open valve EE. + +== Minute 22 == +Valves BB, DD, EE, HH, and JJ are open, releasing 79 pressure. +You move to valve DD. + +== Minute 23 == +Valves BB, DD, EE, HH, and JJ are open, releasing 79 pressure. +You move to valve CC. + +== Minute 24 == +Valves BB, DD, EE, HH, and JJ are open, releasing 79 pressure. +You open valve CC. + +== Minute 25 == +Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. + +== Minute 26 == +Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. + +== Minute 27 == +Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. + +== Minute 28 == +Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. + +== Minute 29 == +Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. + +== Minute 30 == +Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. +#+end_example + +This approach lets you release the most pressure possible in 30 minutes +with this valve layout, =1651=. + +Work out the steps to release the most pressure in 30 minutes. /What is +the most pressure you can release?/ + +To begin, [[file:16/input][get your puzzle input]]. + +Answer: diff --git a/2022/day16/aoc.h b/2022/day16/aoc.h new file mode 100644 index 0000000..fb6f19a --- /dev/null +++ b/2022/day16/aoc.h @@ -0,0 +1,17 @@ +/* aoc.c: Advent of Code 2022 + * + * 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 + */ +#ifndef _AOC_H_ +#define _AOC_H_ + +extern int parseargs(int ac, char**av); +extern int testmode(void); +#endif /* _AOC_H_ */ diff --git a/2022/day16/common.bash b/2022/day16/common.bash new file mode 100644 index 0000000..5af1e54 --- /dev/null +++ b/2022/day16/common.bash @@ -0,0 +1,68 @@ +#!/usr/bin/env bash +# +# common.bash: Advent of Code 2022, common bash functions +# +# 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 + +# shellcheck disable=2034 +export cmdname=${0##*/} +export debug=0 +export res +export LANG=C + +shopt -s extglob +set -o noglob + +usage() { + printf "usage: %s [-d DEBUG] [-p PART]\n" "$cmdname" + exit 1 +} + +checkargs() { + local part=1 + while getopts p:d: todo; do + case "$todo" in + d) + if [[ "$OPTARG" =~ ^[[:digit:]+]$ ]]; then + debug="$OPTARG" + else + printf "%s: illegal [%s] debug level.\n" "$CMD" "$OPTARG" + exit 1 + fi + ;; + p) + if [[ "$OPTARG" =~ ^[12]$ ]]; then + part="$OPTARG" + else + printf "%s: illegal [%s] part.\n" "$CMD" "$OPTARG" + exit 1 + fi + ;; + *) + usage + ;; + esac + done + # Now check remaining argument (backup directory) + shift $((OPTIND - 1)) + + (( $# > 1 )) && usage + return "$part" +} + +main() { + local -i part + + checkargs "$@" + part=$? + parse "$part" + solve "$part" + printf "%s: res=%s\n" "$cmdname" "$res" +} diff --git a/2022/day16/common.c b/2022/day16/common.c new file mode 100644 index 0000000..d8ac20b --- /dev/null +++ b/2022/day16/common.c @@ -0,0 +1,59 @@ +/* common.c: Advent of Code 2022, common functions + * + * 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 "aoc.h" +#include "debug.h" + +static int _testmode = 0; + +static int usage(char *prg) +{ + fprintf(stderr, "Usage: %s [-t][-d debug_level] [-p part] [-i input]\n", prg); + return 1; +} + +int testmode(void) +{ + return _testmode; +} + +int parseargs(int ac, char **av) +{ + int opt, part = 1; + + while ((opt = getopt(ac, av, "td:p:")) != -1) { + switch (opt) { + case 't': + _testmode = 1; + break; + 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; + case 'i': + + default: + return usage(*av); + } + } + if (optind < ac) + return usage(*av); + return part; +} diff --git a/2022/day16/input/example.txt b/2022/day16/input/example.txt new file mode 100644 index 0000000..9f30acc --- /dev/null +++ b/2022/day16/input/example.txt @@ -0,0 +1,10 @@ +Valve AA has flow rate=0; tunnels lead to valves DD, II, BB +Valve BB has flow rate=13; tunnels lead to valves CC, AA +Valve CC has flow rate=2; tunnels lead to valves DD, BB +Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE +Valve EE has flow rate=3; tunnels lead to valves FF, DD +Valve FF has flow rate=0; tunnels lead to valves EE, GG +Valve GG has flow rate=0; tunnels lead to valves FF, HH +Valve HH has flow rate=22; tunnel leads to valve GG +Valve II has flow rate=0; tunnels lead to valves AA, JJ +Valve JJ has flow rate=21; tunnel leads to valve II diff --git a/2022/day16/input/input.txt b/2022/day16/input/input.txt new file mode 100644 index 0000000..145a5f4 --- /dev/null +++ b/2022/day16/input/input.txt @@ -0,0 +1,54 @@ +Valve EJ has flow rate=25; tunnel leads to valve MC +Valve WC has flow rate=0; tunnels lead to valves OW, RU +Valve NP has flow rate=0; tunnels lead to valves VR, KL +Valve AA has flow rate=0; tunnels lead to valves QT, AP, EZ, AK, XV +Valve VO has flow rate=6; tunnels lead to valves KM, RF, HS, LJ, IA +Valve CB has flow rate=0; tunnels lead to valves UI, UP +Valve TE has flow rate=18; tunnel leads to valve JT +Valve CZ has flow rate=0; tunnels lead to valves UP, OW +Valve LJ has flow rate=0; tunnels lead to valves DV, VO +Valve UP has flow rate=7; tunnels lead to valves SK, CB, CZ +Valve FP has flow rate=0; tunnels lead to valves OW, RE +Valve KM has flow rate=0; tunnels lead to valves SE, VO +Valve DV has flow rate=0; tunnels lead to valves LJ, UM +Valve FL has flow rate=0; tunnels lead to valves AH, TS +Valve VR has flow rate=24; tunnels lead to valves DM, TF, NP +Valve IA has flow rate=0; tunnels lead to valves VS, VO +Valve RF has flow rate=0; tunnels lead to valves VO, JF +Valve RT has flow rate=0; tunnels lead to valves UM, SE +Valve RU has flow rate=0; tunnels lead to valves AR, WC +Valve SE has flow rate=4; tunnels lead to valves GU, KM, CX, RT +Valve MC has flow rate=0; tunnels lead to valves EJ, AR +Valve TF has flow rate=0; tunnels lead to valves AH, VR +Valve CX has flow rate=0; tunnels lead to valves SE, TO +Valve GL has flow rate=11; tunnels lead to valves UY, KL, CY +Valve GU has flow rate=0; tunnels lead to valves SE, EZ +Valve VS has flow rate=0; tunnels lead to valves XN, IA +Valve EZ has flow rate=0; tunnels lead to valves AA, GU +Valve GK has flow rate=0; tunnels lead to valves FI, HZ +Valve JT has flow rate=0; tunnels lead to valves TE, XN +Valve DM has flow rate=0; tunnels lead to valves VR, HZ +Valve AR has flow rate=16; tunnels lead to valves UI, RU, MC +Valve XN has flow rate=9; tunnels lead to valves XP, JT, VS, GT, CY +Valve CY has flow rate=0; tunnels lead to valves XN, GL +Valve QT has flow rate=0; tunnels lead to valves UM, AA +Valve KL has flow rate=0; tunnels lead to valves GL, NP +Valve SK has flow rate=0; tunnels lead to valves XV, UP +Valve OW has flow rate=12; tunnels lead to valves CZ, WC, FP +Valve AK has flow rate=0; tunnels lead to valves AA, HS +Valve XV has flow rate=0; tunnels lead to valves AA, SK +Valve GT has flow rate=0; tunnels lead to valves XN, UM +Valve FI has flow rate=0; tunnels lead to valves JF, GK +Valve UY has flow rate=0; tunnels lead to valves JF, GL +Valve UM has flow rate=5; tunnels lead to valves DV, GT, RT, QT +Valve IQ has flow rate=0; tunnels lead to valves HZ, AH +Valve JF has flow rate=10; tunnels lead to valves RF, FI, UY, RE, TS +Valve TS has flow rate=0; tunnels lead to valves JF, FL +Valve AH has flow rate=23; tunnels lead to valves IQ, FL, TF +Valve HS has flow rate=0; tunnels lead to valves AK, VO +Valve HZ has flow rate=20; tunnels lead to valves IQ, DM, GK +Valve TO has flow rate=15; tunnel leads to valve CX +Valve XP has flow rate=0; tunnels lead to valves AP, XN +Valve AP has flow rate=0; tunnels lead to valves XP, AA +Valve RE has flow rate=0; tunnels lead to valves JF, FP +Valve UI has flow rate=0; tunnels lead to valves AR, CB