From c608f6dcded89f23df7d4940a991a06f68b27abe Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Sat, 10 Dec 2022 16:53:33 +0100 Subject: [PATCH] 2022 day 8 init --- 2022/day08/Makefile | 111 +++++++++++++++++++++++++++++++++++ 2022/day08/README.org | 56 ++++++++++++++++++ 2022/day08/aoc.h | 17 ++++++ 2022/day08/common.bash | 68 +++++++++++++++++++++ 2022/day08/common.c | 49 ++++++++++++++++ 2022/day08/input/example.txt | 5 ++ 2022/day08/input/input.txt | 99 +++++++++++++++++++++++++++++++ 7 files changed, 405 insertions(+) create mode 100644 2022/day08/Makefile create mode 100644 2022/day08/README.org create mode 100644 2022/day08/aoc.h create mode 100755 2022/day08/common.bash create mode 100644 2022/day08/common.c create mode 100644 2022/day08/input/example.txt create mode 100644 2022/day08/input/input.txt diff --git a/2022/day08/Makefile b/2022/day08/Makefile new file mode 100644 index 0000000..e0add74 --- /dev/null +++ b/2022/day08/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/day08/README.org b/2022/day08/README.org new file mode 100644 index 0000000..fc6a0f3 --- /dev/null +++ b/2022/day08/README.org @@ -0,0 +1,56 @@ +** --- Day 8: Treetop Tree House --- +The expedition comes across a peculiar patch of tall trees all planted +carefully in a grid. The Elves explain that a previous expedition +planted these trees as a reforestation effort. Now, they're curious if +this would be a good location for a +[[https://en.wikipedia.org/wiki/Tree_house][tree house]]. + +First, determine whether there is enough tree cover here to keep a tree +house /hidden/. To do this, you need to count the number of trees that +are /visible from outside the grid/ when looking directly along a row or +column. + +The Elves have already launched a +[[https://en.wikipedia.org/wiki/Quadcopter][quadcopter]] to generate a +map with the height of each tree (your puzzle input). For example: + +#+begin_example +30373 +25512 +65332 +33549 +35390 +#+end_example + +Each tree is represented as a single digit whose value is its height, +where =0= is the shortest and =9= is the tallest. + +A tree is /visible/ if all of the other trees between it and an edge of +the grid are /shorter/ than it. Only consider trees in the same row or +column; that is, only look up, down, left, or right from any given tree. + +All of the trees around the edge of the grid are /visible/ - since they +are already on the edge, there are no trees to block the view. In this +example, that only leaves the /interior nine trees/ to consider: + +- The top-left =5= is /visible/ from the left and top. (It isn't visible + from the right or bottom since other trees of height =5= are in the + way.) +- The top-middle =5= is /visible/ from the top and right. +- The top-right =1= is not visible from any direction; for it to be + visible, there would need to only be trees of height /0/ between it + and an edge. +- The left-middle =5= is /visible/, but only from the right. +- The center =3= is not visible from any direction; for it to be + visible, there would need to be only trees of at most height =2= + between it and an edge. +- The right-middle =3= is /visible/ from the right. +- In the bottom row, the middle =5= is /visible/, but the =3= and =4= + are not. + +With 16 trees visible on the edge and another 5 visible in the interior, +a total of =21= trees are visible in this arrangement. + +Consider your map; /how many trees are visible from outside the grid?/ + +To begin, [[file:8/input][get your puzzle input]]. diff --git a/2022/day08/aoc.h b/2022/day08/aoc.h new file mode 100644 index 0000000..2ef8361 --- /dev/null +++ b/2022/day08/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); + +#endif /* _AOC_H_ */ diff --git a/2022/day08/common.bash b/2022/day08/common.bash new file mode 100755 index 0000000..5af1e54 --- /dev/null +++ b/2022/day08/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/day08/common.c b/2022/day08/common.c new file mode 100644 index 0000000..a3827b6 --- /dev/null +++ b/2022/day08/common.c @@ -0,0 +1,49 @@ +/* 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 usage(char *prg) +{ + fprintf(stderr, "Usage: %s [-d debug_level] [-p part] [-i input]\n", prg); + return 1; +} + +int parseargs(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; + case 'i': + + default: + return usage(*av); + } + } + if (optind < ac) + return usage(*av); + return part; +} diff --git a/2022/day08/input/example.txt b/2022/day08/input/example.txt new file mode 100644 index 0000000..16d6fbd --- /dev/null +++ b/2022/day08/input/example.txt @@ -0,0 +1,5 @@ +30373 +25512 +65332 +33549 +35390 diff --git a/2022/day08/input/input.txt b/2022/day08/input/input.txt new file mode 100644 index 0000000..a531013 --- /dev/null +++ b/2022/day08/input/input.txt @@ -0,0 +1,99 @@ +002100030222341303232115221524311502200403234452204125251345024423544443305142410110142101223120110 +102030322341143330244233013052025453422005415304655426206452251310214055314024034341233110302320003 +003012203404011430120134554314552441635001641050211406204621114102021543144514102510421243314102033 +330313423002034140035001210441156554545625042262651356556544202500446121455112434243313013430141203 +331203121131230002055113430244610062654034520106363462155201210355601153542301323442223310134324300 +232201231021003254355411402310326206625011564114125202263504451451212600155351424154310240411213431 +033033333301011523544023206136145505304224325606020360006206455564513553512054354124523232201230441 +132201032234334044251334220345360402456541662253556375754662323265201641514121105025300453041111234 +314322341322104402235020206565044225363527617767574524475421763504341061551154250344125514014234344 +101323043023430543342440532324326413655657527215661116623455613664321005134300223542510253102211310 +320404242514122111525043444530625417656773117247512712444315767422412130351505513231104322032421303 +221204123205133142044056245654205231734371546616753217321556472223654044043612554152141554333102034 +340310155225452152216635634352712541366312326272262143522347111166646736001360102010220214000133002 +012404003515002416422500163244271366252711531615333437352461454777311142132000140205630042305222013 +332434552302203445503543424523327464362526657251687634745536521746523154717531643250132300541053000 +020130312425316362611130462217125425435545265467725256332777233137141524314631100051153314314343032 +112115124512002346011342433146124542356845483862756322785545283652723747276776403131622540014001524 +332354013101003031053022245431274627654677343638672643442563448782737464337546541546630431125021330 +220315320142064161406534365262377268373743527784575246325564676238816733767331345114363612344051535 +102041153526000601402325625766534543527882476653366727573448727775677146365732431425225562043130034 +350054114310533002426654125166575285757827422257872378766383267543484865317252532765440216543555512 +222440404353016144747347457368562668586285847553578732435887584463446862116134751240360645225435344 +554234546163262630313431762248876668663352436888374587856263343842526635237314231451062036605442520 +525531324662660356256614344438373575732699988366775989976447384338336827686445577222030363631240512 +055541261042533424177375756738443482846986979788978997559377679255463858237757327464422026230510130 +514123221240250442457744728347386637684953574546455344754548333793233865436751231314653541223102202 +355454250661114645144551785724753237378345375493453967559796675839837444334776625552412552302431302 +202445234333206767744653874724276586973668878949653473947686567565487325864733735677672241416513452 +445415223211061633767433865252483574744685974593334963668983745643354885658423263536531213102544202 +201405464251164654177385783224666473975674643767946648464678958969336335638578275154116223152222102 +401423140356441654745572266848965593553995667644648964747884544439493545472454437715336645303346422 +154440323240741427732867467564957336394985859766644495658985963583496399736552632162756445212536404 +441663663062661372122867364584655388977474857585778686889578799644949733463423826715534215154065204 +422036122016243651174663588693898583374968974778579585977478599643389537573534354622377452555411315 +430412403362272513348763673566578945645997454758694486758844958569786758359475575577532661126532300 +503653020441267422765353469867998339764449596776968996449777464644846938866332635583727771665263026 +006252631541331635768754753357384754988659585857956567666645975599653466655333342485345762600032403 +215205542775713732868842389885585646884858449858579777975997584997463698339932263363162431251615251 +342224450731115175762626546538955447796468885776778879697695699945446388444478355347827635732006646 +045011110226211633623658548365544498466468766878699597655897586488555435934935526883851537736604133 +440352212243431376382869634434568447654566895897796875679759667598898849334873844583742271767024403 +566632367121414762272448784755746799985759966559657989697977677955455754549393784586875275341562621 +004131167135757426882464486773699786789959957795985595559587775554479945637849747667752242212324244 +022411031432724262533554794664597446479869568767598899669969588984864457535986725263473577457413134 +036415005331162527346577487593798458668976888776888766856699598847776567686579476836656723623111342 +504001302331545267684439554776894957999788577876688969865558955547965687556849786463457244355044511 +464266074264126575276833864784546697885788878967966988678696686685576789843676844576623242451411411 +660130573642326366362654785549676574587876888769689696798799988678648554635745724364273772474666502 +413511521313437652422664948645649566979966676679778669968777776786958464356797545247763267365643645 +455033174473274564484285387995879595555686569997977698868769855686564847593489668858847744131410225 +560316675532344635443349663994499869866598568889798868967976679655597494679336978524332246567265361 +121053163342234352453837754569968465557968697766766888969966667796479457384444468264837376474515455 +604062313527642634768255846899648545559665886668966887999959659769978495794795825775373713613231451 +041031112622754332842439776736966994677888699898679988967779696758849589365579766676242256227420364 +630161363126476778455475799658545955598576568877768698668579988799596866465635488284555252726211044 +455563641427127283364758796475975894658677688789777769876667978747955677573575525588444241765111511 +256420063532645225542863884958844496596759999769769687678888788875449888945958948378756476127155054 +523256165217547362636325547686549767698957967985968987897777685866576744344388256886854362647342110 +205224606764556763544234874676659474468968576879668558856675855668779847849738247387744421321145610 +022651355615653552468486676459694744747575877656886776677858584988944678787576556747724125467333305 +526313424324717126486746644643397589664468865599858886996669788566489473798387676766873216657521426 +426530404522411762572567697375959747975669667958685679878659587764977658398697353283563215312244014 +023510245741131775436864579684699874979976567776557765866775469587757498544476554753722641656303654 +211341433442225414565332887899363678699495567888998675657659688744458493387343842346747477252626250 +406543152154335614758277748595836896786654948759999978569588555764838686653647362854723163226456101 +333640633617315535456525364849458558744656899855554897897568844446533866457545654847562377442214226 +040003053246534354438748723933365934878485997458955574785966996653353969666867276761661111124104550 +405602516206264365157378627495546935658594587869579958969686766986836687348285865231616336232000100 +304131030254233641578272475868953446545577766644956989679964489688554398956223433417637732661500540 +131164662225325441733366324345477954639475997658684766465487974699579665344326248136151472051130445 +452312154206174654138684277376889449945746954689646875777596486949365968888456226732332263263420000 +003010154551246223274282454345355673349967689986885795449989359633496728553885221437611730454554455 +131435126460017635544785754344874379695379366384573934357795398633967475353836864461424146264354142 +350154544045013352533173566257757787385448697484997454578735937499665628425542376452563635450224022 +302420224365163651765264446875287359533637498445639443969586466433384346337722662774436566624454214 +415242456321105173655733744886343826548944666338683876383973497998744488687446541256724244661531431 +155303206134314124372553618623857237346567857564464797593393675563536467777317174625640132203003240 +335521544652523624641575451785635352333587957647598535633948867838652636686573137515062356442045153 +235314500244636615342325346277256685653838226755493858683465683736283367654226655456541546644313125 +252520511531061305614364671212856473625642477732845278766573642425286572562373137446456005011330433 +241525455120116046543737642325543677623243884242534423327827446383376441727657314343125403632434002 +301045223551220356167117621755325772252845222546246588464784874685774676163655131632622146412415515 +011150133311336614251435416453632383454327534367288242328746537673543714263674263262001132354544412 +314104033000306256531666256433553535833538337463847277453476563227672715617272601302551120422305504 +104211442013141664402363645617163435245556228235337864656673626354151416721375243323635625100210424 +413422313502535362615054171511431775335574748526574728747453724472157632635263350024552502052340024 +131225301023304456424150404525765416565572272321835377167235317312551645334432406536160545434122120 +342131052232413325301451041516716541737346252413667115612642227312572433104525003114240240141232002 +244421101354005430232434420344323273421374225635551363463245561271743411452116466311034334101231020 +232404402304311415164151401553463731147111147752141732625152553615361046133341313265525232544143442 +201321033520133414312301326330253041267251614477322566645472656414545035321106361434351501123314104 +211232034442524242313453325046331353174352335422752364775746537513333534355120322320404054014303124 +224032233240430510135541652065052415154147565656231651653327511443424113100543020223335130010141400 +004110101133543440511523265424013341433043655022512172622140322434116104552043413352211021312421433 +000304433232402410151205333526613043463556021413402141634154404530112303306244402221312522221004042 +200204441020135232033032541516232550445221141520665016336536052063115335040344025054213324400312303 +300211022440310123055510053251505226452265556365100101014044645236505213550201425252404100302413231 +310312413210032422053242024243343060135340422313312403505631104430120233112324204414021331012401232 +132020341330304022053503522313342026211133610000020554213662244142551253124302245242110111033101203