Compare commits

...

2 Commits

Author SHA1 Message Date
befb71ed22 day 15 part 1 2021-12-19 14:59:52 +01:00
fde80b124b day 15 init 2021-12-17 17:54:54 +01:00
5 changed files with 549 additions and 0 deletions

10
2021/day15/EXAMPLE.txt Normal file
View File

@@ -0,0 +1,10 @@
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581

100
2021/day15/INPUT.txt Normal file
View File

@@ -0,0 +1,100 @@
7173312811251922158911469918987552198919812111716172146671617245481829877994222545642918723611761161
4646216972193253589269976231119311134928199248898455211321437231815174172121741121975282198233991494
3289492215191791411967453954334211769221445712443155649188599429873192222251651618191431193771913161
4515378885673214677819994531796439912658321147432167119218169567397836391632659222129836129869511916
2712221811579451126675135153463233218127758873121423588977247994131262217141894124191532919392181221
2178721375452418378151119191462965597273934829892424135142928116384159551744519391881971747591122391
5989829351931297973698946772289195611999114116457994829931431852246618418421865212115519163272761923
6593812111961641351113628479353219414419586755618193231529719388229163911675197467437421311312239179
1367332414491292869836537117999611111221111953549366768254717911485954325698192656364326199224393931
3823194741411958284195341318764321126955567853592981221895781591822131731123421219346319199699661811
2768392277233761123433164969394413212112469222229798171712192846251349511611257231733341931718783244
6211686449674219169788158851614122686169727933982119884512121119558991113337122163499339173141629831
8964588262473118893646161133718696168932118613823336981243112241379921491861914992225224246756182418
2163359261942752538197461382831351939177272236845211918187697144693874631793929811184394969994511798
7989114898193161218935379299733232825223621812115731415952869113232982123295392574119379211945415319
2981541411882383967359943494764147677122681664351614692114199243764398471277125818246414966145121199
5137573281125648957552155112919221115194983216967212771366843828993746973731199925338771326696133114
3766559511919239237173557181961523331983888219226659121522293237112764226926452246137293192651819113
3682758681999816113831312836469969232193734111621424211857181274988592869111461771921513621211243412
1948613863321211959111219591667512152147132817872211112132781958772886144935937148416627115359861937
5344911181727365389869235886885188325146145531191367511892525913156626271219699181915745829815424589
1921768161912919191122421721152779172441236981211235365113916763299169389182841162652365191929621634
7426456282867924726354983131829724281948296112363261845399134721793117999834413631475537141926412639
9239218892837834121193535318983411257861171711911122649161941873915268198422451274213142821191181149
2142233113938879859911716118232136567249199181221211458588429128553241944195818216437253326115516261
4263229279237581194769911939818975846746332753192816433171243142193222961892988199519393411318675131
5931431844789494579714797652192131161119133814229515928519858118391991289576164912398122913213397599
2721961279391263971273951243381713261657382114473995987479128836442222129299433186828222392379946598
5721194922655214111113638224216129848113795111212977322119719244642824136299146894543411699272447653
1721283151419121619299916438767182692133635934419762683731479187219469718126125942443152124252117214
1191318114125371538282471992772631716712152159418533811911319228328921272115954283291193338159541897
1992998717991939712294189151345342119816611271962276482851232831211636198338141352118191443674663948
1161696145282521595951182122631527918224737193572179344636436218858337162961132231795163829188381192
6941211141653838595642461762596975428188729157141581868193713799899662916781294793123132681958118616
1819477158632417189919213535853313238553111151416298436598333519285362224819522881471912121385128664
4131994851618912997621891835224828464921118721544139921463917432371971621819421564868949338392959375
3133242592422467724638515112598929161738561343962999211758244193964415258861531512181917956372914815
2851732889555112914338252135996931333699122198624171123479362627153221311234119682211133193122826311
8572333787948173113891239811786852446329671165322978635241133123367111997212895127886592793919272615
4491459926325229881291911822161139341194221969828485936191892191452693598169844422764559177311791991
7711224386721991421971479559514658827234215923612982938336455918196228323274887266866828172793318129
1792133811393571193248372723564225172181126791174377121318932411884213621147116123831958827815214339
2155469978392417716111522211533192455439119614121568579123143651567222761914274317316466323996119924
8623177814111391292499597672365634732111139269561125392732981323319126423198138385128561991559391782
1842517821951178317281681455259131499922139421927127818952311122613122361391261889385551741415311174
1121489791429687666926699327995141292912715883217747843259918621498111261621125443139314125515579392
4926852919113151918334999277973512249327141773526658912259778215269924712911844817319595442111992296
4121922141952419619152691739416242743191911121434241927499148486191311291548232399983325412914177521
2729193162333518598231521726292747161811711199291367239129432321128819725987611419151129841314223329
5161319319642221756114718511892119842126129917614211814292897914584213663525125888918911841344441167
1218919131819647671213811726979395328283925417535819381416121438921296118284911421324239118621797149
1296189848121155113349984582165659294526124822124196245979513919841416218999219691561172279151111152
8131333157919249442912528253243598392791124337511444713511932179243517538421849512824282112125754398
1889681196198926571218233416129912265342816929472192326228618815919519181119971199965864884926329684
4494347982618719134522341621218316919443481761161413736159296282296287392981826492616258178597149171
3372889451184198132111171777814111179543243756738721486438829911265492817181811258219678819715934783
2616818924444129774824882961271546911121478523975641935416133917974684665993259291354189455115256112
2811839229428511712146911429195623518752949851188111438115675381894374219416116769619292646212281281
1119763626283549911491359216384192428413381423111124499192126173788341749798821599693991125531415822
2349891181211194888159436111111772211361273821434343227381149832784862518216691117651763161816911119
8418725583117989441352169137523561277849413638828989992112444397932486713918531213173217496612196229
8194221916977995111288941528855138698421192518849711165396995252187534931798892359817432158363564921
4868199762329594579418795281999591295951216119125346219151394998351623911582981973134399941972155729
2472125294499111714693695173281491912974791153411681134725132321246194826113275721861259118519171112
1633713781533969985751914138835182737446812176394116926162181989316432892346283723213927824377271732
7727749711573911939746322419297737232517397289293189731312622299196183811592993841253139241811194299
3444125976332938779126885711113586551233924262213299924415281989123291391581652215852716859551746751
1719317631143843451943995721353922513913657532736116317191133165811217264495991377397112229124311117
5861535147284127128962317613425858155751422182951141984197973311438338913558511976717312956251819479
9299184959412999345164229713288931729114929419973869591951391199698548124823539329547718426332534959
9851246238111471391482375191872321911276443744269832118973631471681521191762795722912611411731819333
9869224453198466342944911731614713469959619233517151937535427336961129724161969599182912551574172352
6371551729394919281768279284119868777896992246419593152391148181591759131189212591171199419129198313
2295927349268467493318173971873622191313441346343628139715519647853238244192929217593451153811319811
1211231819479781274911189539311996147632231799793892194619218459139118323385691136152122391381938288
9492197194223986366157191556366723812164435115416454484879391237714149926888136741911511279936168912
2111411993964171277723732569599285124872178711536341321575124391377998587214881369916299311339162115
1857186715221591921589199519221499976826598488872332119919989134621681411613762118296121212833167613
9718337129818367133629146122228911964852312941892555957139253694142728519112114864536965987514966211
2415331399185119912149918119982246814444727537971643769252248161761394618613421452811825842399129131
2637271813357222157145794211314329984964767141817213542216811653152351899121891897921939593213111314
1342912217283425272931147739959428495933619188311493691141954225917482937193916388217813352129619863
6171111767238611133869357659223281188142241123851329951559176773195191456535911253118971695714976355
1585271618186833837341918341979188526816145718783214959195393118172461327399931821617411511911199863
6461447221181217136723211129799371134424983996611256271122958541942582422342539318328941531711964915
1634222839756824796924611186112289121315141467251822858181833282112958592898298119557131913669565746
8413516892412482311827331191357514129984673942271927712113191241311815321828498499757481515425571313
9292913153714142122372452413224612721993331697395348762817819282291954241689949412128759187143413229
8114111293919599241719615591117821715267715772911268593138158693924671111148633916249731251464216222
1611972412142482865862148181257292295187211989739221122114612118449211946324743944111224717432567371
4426979492592797511999213492912687199638947476153533611299113561131993655419139311131141486691318421
4819559316111356226292342978981271723115518319562529917291119531537967212778289141272129186123618729
1322122459351831389234143421535699946127862425916492541436671171929936117393814918199114528361615475
1832241129859195291112852541592681571461425225854732933536919122531988823161646159389214184462152416
3537959443491836554998141843397447943981721116171172126461321895151415435116233545219833813151222693
1611963219749418211652174418113582118994319912999813714577885544917887217951819321971873298986543979
2483836259212216629932122153213826317397734611722254411988417882585921553111163857875961521399991758
7519842459152519311265791212932251261948342231857165828993185221622421253711999122289324849113714118
2278835219311786262197322199149957832597951398143478923177181437824396993412962115952333951727514718
1812819933161952113937239491658431421595957151868228264138181469731616171293123193688276811119911925

58
2021/day15/Makefile Normal file
View File

@@ -0,0 +1,58 @@
# AOC daily Makefile - GNU make only.
#
# Copyright (C) 2021 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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
#
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
#
INPUT := INPUT.txt
SHELL := /bin/bash
CC := gcc
LIB := aoc_$(shell uname -m)
INCDIR := ../include
LIBDIR := ../lib
LDFLAGS := -L$(LIBDIR)
LDLIB := -l$(LIB)
export LD_LIBRARY_PATH = $(LIBDIR)
CFLAGS += -std=gnu99
CFLAGS += -O2
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -march=native
CFLAGS += -Wmissing-declarations
CFLAGS += -Wno-unused-result
CFLAGS += -DDEBUG_DEBUG # activate general debug (debug.c)
CFLAGS += -DDEBUG_POOL # memory pools management
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 ex1 ex2
all: ex1 ex2
compile: aoc-c
ex1: aoc-c
@$(TIME) aoc-c -p 1 < $(INPUT)
ex2: aoc-c
@$(TIME) aoc-c -p 2 < $(INPUT)
clean:
@rm -f aoc-c core*
.c:
@echo compiling $<
@$(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $< $(LDLIB) -o $@

162
2021/day15/README.txt Normal file
View File

@@ -0,0 +1,162 @@
--- Day 15: Chiton ---
You've almost reached the exit of the cave, but the walls are getting closer together. Your submarine can barely still fit, though; the main problem is that the walls of the cave are covered in chitons, and it would be best not to bump any of them.
The cavern is large, but has a very low ceiling, restricting your motion to two dimensions. The shape of the cavern resembles a square; a quick scan of chiton density produces a map of risk level throughout the cave (your puzzle input). For example:
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581
You start in the top left position, your destination is the bottom right position, and you cannot move diagonally. The number at each position is its risk level; to determine the total risk of an entire path, add up the risk levels of each position you enter (that is, don't count the risk level of your starting position unless you enter it; leaving it adds no risk to your total).
Your goal is to find a path with the lowest total risk. In this example, a path with the lowest total risk is highlighted here:
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581
The total risk of this path is 40 (the starting position is never entered, so its risk is not counted).
What is the lowest total risk of any path from the top left to the bottom right?
Your puzzle answer was 435.
The first half of this puzzle is complete! It provides one gold star: *
--- Part Two ---
Now that you know how to find low-risk paths in the cave, you can try to find your way out.
The entire cave is actually five times larger in both dimensions than you thought; the area you originally scanned is just one tile in a 5x5 tile area that forms the full map. Your original map tile repeats to the right and downward; each time the tile repeats to the right or downward, all of its risk levels are 1 higher than the tile immediately up or left of it. However, risk levels above 9 wrap back around to 1. So, if your original map had some position with a risk level of 8, then that same position on each of the 25 total tiles would be as follows:
8 9 1 2 3
9 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
Each single digit above corresponds to the example position with a value of 8 on the top-left tile. Because the full map is actually five times larger in both dimensions, that position appears a total of 25 times, once in each duplicated tile, with the values shown above.
Here is the full five-times-as-large version of the first example above, with the original map in the top left corner highlighted:
11637517422274862853338597396444961841755517295286
13813736722492484783351359589446246169155735727126
21365113283247622439435873354154698446526571955763
36949315694715142671582625378269373648937148475914
74634171118574528222968563933317967414442817852555
13191281372421239248353234135946434524615754563572
13599124212461123532357223464346833457545794456865
31254216394236532741534764385264587549637569865174
12931385212314249632342535174345364628545647573965
23119445813422155692453326671356443778246755488935
22748628533385973964449618417555172952866628316397
24924847833513595894462461691557357271266846838237
32476224394358733541546984465265719557637682166874
47151426715826253782693736489371484759148259586125
85745282229685639333179674144428178525553928963666
24212392483532341359464345246157545635726865674683
24611235323572234643468334575457944568656815567976
42365327415347643852645875496375698651748671976285
23142496323425351743453646285456475739656758684176
34221556924533266713564437782467554889357866599146
33859739644496184175551729528666283163977739427418
35135958944624616915573572712668468382377957949348
43587335415469844652657195576376821668748793277985
58262537826937364893714847591482595861259361697236
96856393331796741444281785255539289636664139174777
35323413594643452461575456357268656746837976785794
35722346434683345754579445686568155679767926678187
53476438526458754963756986517486719762859782187396
34253517434536462854564757396567586841767869795287
45332667135644377824675548893578665991468977611257
44961841755517295286662831639777394274188841538529
46246169155735727126684683823779579493488168151459
54698446526571955763768216687487932779859814388196
69373648937148475914825958612593616972361472718347
17967414442817852555392896366641391747775241285888
46434524615754563572686567468379767857948187896815
46833457545794456865681556797679266781878137789298
64587549637569865174867197628597821873961893298417
45364628545647573965675868417678697952878971816398
56443778246755488935786659914689776112579188722368
55172952866628316397773942741888415385299952649631
57357271266846838237795794934881681514599279262561
65719557637682166874879327798598143881961925499217
71484759148259586125936169723614727183472583829458
28178525553928963666413917477752412858886352396999
57545635726865674683797678579481878968159298917926
57944568656815567976792667818781377892989248891319
75698651748671976285978218739618932984172914319528
56475739656758684176786979528789718163989182927419
67554889357866599146897761125791887223681299833479
Equipped with the full map, you can now find a path from the top left corner to the bottom right corner with the lowest total risk:
11637517422274862853338597396444961841755517295286
13813736722492484783351359589446246169155735727126
21365113283247622439435873354154698446526571955763
36949315694715142671582625378269373648937148475914
74634171118574528222968563933317967414442817852555
13191281372421239248353234135946434524615754563572
13599124212461123532357223464346833457545794456865
31254216394236532741534764385264587549637569865174
12931385212314249632342535174345364628545647573965
23119445813422155692453326671356443778246755488935
22748628533385973964449618417555172952866628316397
24924847833513595894462461691557357271266846838237
32476224394358733541546984465265719557637682166874
47151426715826253782693736489371484759148259586125
85745282229685639333179674144428178525553928963666
24212392483532341359464345246157545635726865674683
24611235323572234643468334575457944568656815567976
42365327415347643852645875496375698651748671976285
23142496323425351743453646285456475739656758684176
34221556924533266713564437782467554889357866599146
33859739644496184175551729528666283163977739427418
35135958944624616915573572712668468382377957949348
43587335415469844652657195576376821668748793277985
58262537826937364893714847591482595861259361697236
96856393331796741444281785255539289636664139174777
35323413594643452461575456357268656746837976785794
35722346434683345754579445686568155679767926678187
53476438526458754963756986517486719762859782187396
34253517434536462854564757396567586841767869795287
45332667135644377824675548893578665991468977611257
44961841755517295286662831639777394274188841538529
46246169155735727126684683823779579493488168151459
54698446526571955763768216687487932779859814388196
69373648937148475914825958612593616972361472718347
17967414442817852555392896366641391747775241285888
46434524615754563572686567468379767857948187896815
46833457545794456865681556797679266781878137789298
64587549637569865174867197628597821873961893298417
45364628545647573965675868417678697952878971816398
56443778246755488935786659914689776112579188722368
55172952866628316397773942741888415385299952649631
57357271266846838237795794934881681514599279262561
65719557637682166874879327798598143881961925499217
71484759148259586125936169723614727183472583829458
28178525553928963666413917477752412858886352396999
57545635726865674683797678579481878968159298917926
57944568656815567976792667818781377892989248891319
75698651748671976285978218739618932984172914319528
56475739656758684176786979528789718163989182927419
67554889357866599146897761125791887223681299833479
The total risk of this path is 315 (the starting position is still never entered, so its risk is not counted).
Using the full map, what is the lowest total risk of any path from the top left to the bottom right?

219
2021/day15/aoc-c.c Normal file
View File

@@ -0,0 +1,219 @@
/* aoc-c.c: Advent of Code 2021, day 15 parts 1 & 2
*
* Copyright (C) 2021 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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>
#include <stdint.h>
#include "debug.h"
#include "pool.h"
#include "bits.h"
#include "list.h"
#define MAX_SIZE 128
int a[] = {
['a'] = 0,
['b'] = 1,
['c'] = 2
};
typedef struct square {
uchar cost;
u32 totcost;;
uchar visited;
} square_t;
typedef struct priority_queue {
int l;
int c;
u32 cost;
struct list_head list;
} pqueue_t;
static LIST_HEAD(plist_head);
static struct square array[MAX_SIZE][MAX_SIZE];
static int asize;
static pool_t *pool;
#define VALID(x, y) ((x) >= 0 && (x) < size && (y) >= 0 && (y) < size )
static void print_array()
{
log(3, "array (%dx%d):\n", asize, asize);
for (int i = 0; i < asize; ++i) {
for (int j = 0; j < asize; ++j)
log(3, "%1d/%2d ", array[i][j].cost, array[i][j].totcost);
log(3, "\n");
}
}
static void print_queue()
{
log(3, "queue:\n", asize, asize);
pqueue_t *tmp;
int i = 1;
list_for_each_entry(tmp, &plist_head, list) {
int l = tmp->l, c = tmp->c;
u32 cost = array[l][c].cost;
u32 acc = array[l][c].totcost;
log(3, "%d: (%d,%d): cost=%u acc=%lu\n", i, l, c, cost, acc);
i++;
}
}
/* insert l,c in queue, keeping cost sorted */
static pqueue_t *push(int l, int c, u32 parentcost)
{
pqueue_t *queue;
u32 newcost;
if (l >= asize || c >= asize || array[l][c].visited)
return NULL;
newcost = parentcost + array[l][c].cost;
if (newcost > array[l][c].totcost)
return NULL;
queue = pool_get(pool);
queue->l = l;
queue->c = c;
queue->cost = newcost;
array[l][c].totcost = newcost;
log_f(3, "(%d,%d) pcost=%u\n", l, c, newcost);
list_add_tail(&queue->list, &plist_head);
return queue;
}
static pqueue_t *pop()
{
pqueue_t *tmp, *cur;
list_for_each_entry_safe(cur, tmp, &plist_head, list) {
int l = cur->l, c = cur->c;
list_del(&cur->list);
if (array[l][c].visited) {
pool_add(pool, cur);
continue;
}
log_f(3, "(%d,%d) cost=%u\n", l, c, array[l][c].totcost);
return cur;
}
return NULL;
}
/* read data and create graph.
*/
static int read_input()
{
size_t alloc = 0;
char *buf;
ssize_t len;
int l = 0, c;
/* get points list */
while ((len = getline(&buf, &alloc, stdin)) > 0) {
buf[--len] = 0;
//printf("%d: size=%d [%s]\n", l, asize, buf);
for (c = 0; buf[c]; ++c) {
array[l][c].cost = buf[c] - '0';
array[l][c].totcost = UINT32_MAX;
array[l][c].visited = 0;
}
l++;
}
free(buf);
asize = l;
//print_array();
return asize;
}
static u32 part1()
{
pqueue_t *pqueue;
u32 best = UINT32_MAX;
push(0, 0, 0);
while ((pqueue = pop())) {
int l = pqueue->l, c = pqueue->c;
u32 acc = array[l][c].totcost;
if (l == (asize - 1) && c == (asize - 1)) {
if (acc < best) {
best = acc;
log_f(3, "New best: %u\n", best);
}
}
push(l, c + 1, acc);
push(l + 1, c, acc);
array[l][c].visited = 1;
pool_add(pool, pqueue); /* recycle pqueue in memory pool */
}
/* as we accounted [0][0] cost, we must substract it */
return best - array[0][0].cost;
}
static u32 part2()
{
return 2;
}
static u32 doit(int part)
{
read_input();
return part == 1? part1(): part2();
}
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);
if (!(pool = pool_init("stack", 1024, sizeof (pqueue_t))))
return -1;
printf("%s : res=%d\n", *av, doit(part));
exit (0);
}