Day 3 part 1 complete.

This commit is contained in:
Alexander Andreev 2023-12-03 17:57:26 +04:00
parent 7ce37165f7
commit 61fe20524b
Signed by: Arav
GPG Key ID: D22A817D95815393
4 changed files with 232 additions and 0 deletions

View File

@ -17,6 +17,9 @@ day2: day2/p1.c day2/p2.c
${CC} ${CFLAGS} $@/p1.c -o bin/$@p1
${CC} ${CFLAGS} $@/p2.c -o bin/$@p2
day3: day3/p1.c day3/p2.c
${CC} ${CFLAGS} $@/p1.c -o bin/$@p1
# ${CC} ${CFLAGS} $@/p2.c -o bin/$@p2
clean:
rm -f bin/day*p*

140
day3/in.my Normal file
View File

@ -0,0 +1,140 @@
...................305.124................................432..............................................576..313.....514.................
.............113...-......&....................&...819...........654..../..........................&901................*....869.257.........
...377..&783../.................................9...........855*......940..463................-.........................844.*....@......679.
......*...........197.261.....817..336.759............&742......548.......&........748......844.............#.......&........254...169..*...
.......36....368.*...............*....*.........*..88......%866.......135.........*..................515.682.....114...%...........*.....768
...233......*....909..698.........427..........127.*...................*...........450.........482..../.................312.....621.........
.../.......882...............776...................555......180.........971....217.......857.../........212....674.917......736.....441*760.
............................*.....@....907....%940.....%.......+.#..........45*...........$............#..........*...............=.........
..................370.225.425..211...................932..........381..267.............45.....=....549....238........367..&.....488.........
.468.............*.......................761...677.......&.................929..907.....%....79...*.......*....324..*....515................
....-.#103.504...388...........$..........*.....@........766.%................*....*623.........532..881...573....*..840..............497...
...........*.........305....412..687.......971................676....@640....657........709......................807............98.....+....
....279...502...62...*.............*..............................................838%.....$.........585..........................*.........
....=...=......*....209.825.482.993..........972............620*664..-759............................*.......184.........952....160.....156.
.......814.....931......*...*............551..*....711...65.................224*115...851.772..104..505.....=........796*...................
....................723.103..275........*......564..*.........830........................*...../..............................232...........
.............$......&....................951........703......=............160+.....138......*........257*809.............$298........284....
.....684....464.333..................827.....*211..................................#.....855.86.....................$913............*.......
.......*...........*..*......917.163....*.409......294.......................#..39.......................511....358.......$671.......237....
.......663.......311..163....*...%...158........*....-.442.........958....108.....#.....941*837.........*..........*........................
............865=..........409....................128...*.......283....................................680.........730.935@........768.......
....../................=.............54.....857.........418.....*..911.......610..643............381...............................@........
.......925............984...........-...800*.................194.../.........*.......*.........../.....................................@....
.................................@...............................&......615...16....695.....-..........................*750.............855.
....611..136...................108.540.........362..711......*....420.....................190.......................213.............86......
204*....@.........288..825..+........&...........*....*......733.........................................7..387.......................*.....
...........795..../.......+.505.834.....228....192...619..........886......467.....942.............................%838.........512...340...
.......957.........................*...*..................962........................*..............211...$..............624................
.......*....................349....144......./786..350.......*.......................541..21.......$.....284..190*......*............518....
....484...@....17..........*..../........309..........*....530...118..........208..............253................489.828...........*.......
..........309...........715.....531........+.516#...645.........$........369....*.......*185...@............=.#...........@............&....
491*192..............................................................233.........711.948.........572.....869..289......932...........149....
........274.....511.....425..........421.288*965...652=.919........./....*.786...........676.....*..................+.........=.............
.........*.........@......$...*.........................*........*....791...+..113......*....527..897..499+.904...166........580.........389
.......306.....................353.497..929/.......=....553......495.................884.....&........................794........774........
...................../...........................582....................................................489..........*...............&......
.605......537.....247.....................479................74................227*995.761.....@...........+.......958...87.....547.795.....
...*.....*....................289+.....*.....%........967..............*...............*........611.....28....367.................*.........
..690....916.497....................894.440......547$.............596.492......23...............................*...............237.221..786
.........................681.....=..........580+.................+...............+.......696............@..542..56..........................
...936..639*947.............+...48.*709..................307.257...-248.697.553.............+.........146.....#.......*241..................
...*................568....................=378....41.......*............*...%..11......*..........................982.............#........
..323..704.+724..*....*..195*....812..879...........*.....*....508@.....852....$.....664.......317....*......332..............326.158.......
.......*........610..767.....936...#...&..744.290..839.382.543....................................&.827...../.............323*.........884..
...........962...............................*...................46....502.265.150...................................639...............*....
955*818.......*...@...124*512..256...................+....&..............*....*.......170....996...#........................786.......4.....
..............284.697...........*......172...........398.435.......-.....380..........+..........698....428...361................206........
.822...#..............936$...745..........*..%59..................726...........+.........249......../...*.....*..................%.........
.....870................................978.......761=.......179.............692.........*..........477..550.....676..281.............507...
..............913.................215.........................*..745...#.............882..130..569....................&..........*472..*....
...27..710.....*.....................*.205*403..............59....+..172................=.............................................697...
....*....*....731.......865.......155..........849*....#..............................................679...854....................&........
.985..%.............599....#..........191..520.......473..#........./...........708....406..677+...=............................599.....133.
......501...........*........@.........*...*..............446....767...*444........@......&.........49......#...311*413.....................
..................157.......830..13.90.398.945.978............................950.................%.......88................$...............
.............297........618.......+.*............%............317.............../.........709..481............503.........570...305*845.....
......661....*....632.....@..+......524......../.....+...........*...590..............944....*...............*................+..........681
.......*.....820...@..........829...........671.......206.......239.+......................339..#............106......415..213..........-...
........114........................903.669=............................764...319....373........642...................................-......
.................*...........................*.......121.......898&.....$......*...*.....................................211#.......579.....
........*.......362......787.440..........842.825.....*......................590...357..........949......523....470..292....................
.....594.459........98.....*....*.......................355&..*..................*..........554..*.......*.......*....*..222...243..........
.........................535.....674.........804....#..........455.............847............=.32...23..951.....217.346....+.*....+272.....
..183...........443..........119.......833....#..693......818#........................@..699..............................*....678..........
...*......455/...%..826.............#.....*.........................$...........868.739....=..753...181....773.........323.508..............
...382................*...169..510..590.295....212.767....*970......711.........*........#.......*..*............651...........36..766*.....
............&..61..697.....*................46.*....*..691......970............786....953.....775...235..................*550....*.....417..
..........45.....*........689...645.....549*...821.601.............*......................952..............#..........464.....609...........
.....657.........48...888.......*...................................305......+47..........................254.27......................123...
818......388............*....412....../491.........#336...506..422.................989....15...70..............*..354............@.....*....
..............506........11......./...........643..........#..=..........703...963*........@.........613.....413....%...485.......265...33..
....589.......*................207..............%...............................................+................*......*.....94............
...*.........663...413+....................................819...........655.%...648..823.......770....385......457..150...#.../............
515.......................475....825......33../350.....459...%..........*....681...*...*..............%..................437.......689......
....$993..897@..............*.......*837...-...........*..............726........372....922......&198................................*......
180............*..........679.................71.629=.796......109...........................527..................682.....558.....798.......
........437.742.46.................@.......&...................*......498.......@.............*............./......$........*...............
..761..$...........148.......856....701.377..536*510...604...540.....&........929........172.799.............310.....700...........440.%....
.....*........808*....=.........@......................*.........577.....................*................&...........$...417.39.....&.229..
........794.......446..............................214.543...%...*..........935......865..445...../....%.615................=..*............
..572..+...............................419....932@.........924..72.902.......-..579.-...........997.203................873......945..567....
.....*.................273.............*...........................*...142............688................755.850...........846.........=....
...880................*..............883.698.....$.......=........197.*...........316*.........365.........$...*............*............904
.........385..977..179....+...................652......875.............350...863............73......464.........214.852..478................
......22....*..@.........93............*682..................*137..309.........*....557.....*........................=.......698.....+.218..
......*...645......163...............89......*853..................*...386..341.......*...228............718*492...............*..153..*....
.....576......................857.........513....................509......*.....322.710.........615.......................613.165.......292.
................................@.....370.......598......487...........503..*.....................*...738#..#..$901...$.....#.......648.....
..19*454.............78................/..876..#..........*......+..........937.......383........523........69......326.678.....212....*....
.....................*.....436.@...646...*..........100....367..325.....361.......207*....50............621...............*..........237....
757.........714......502...%...54......909.411.....*.....................*..........................=..$................415.................
...$.......&.................................*...471..985...508.472....891.842..$.................418.........&.....&.......................
.................$........906..........*215.338...............*..............*..606.#235......................813..259........229...........
........472...222.........*.....245.572.......................927...769....344.............291.239.....................94....*..............
...818...$.............291..............288...........163.734......+...........699............*.......981&...514*.............224.....=.....
.....*..................................*.........................................+..189..........452............644...................669..
....850................326$..............869..&985......784..../..............592.......*..153.....*....887..493.....211........%...........
.............................457....834.....................94..564..........&.........498....*...304....../...*.....*..........845.....&...
.......12*48.753...244..196....=......$..721....90*29.........*.................+.522.......487...............317.....531..311........20....
.177...............*.....*.................*.............*969.611.......*565..338...*.............712..922$.......770......*....522.........
....*..459......149......165...=........879...........103............497..........258.931..75....*..........400*.....*......972.............
.269...*............#........715.............................*475..........................*....145.............414.....228........893......
........209......508...................../331......469....894............347.890$..541.....56...............#........#..*...................
......................=...........37................*...........................................$...........197...119.....=...........756...
...........&.......850.....367.34...*............517........178*716..........=...................892......-............883...755............
.....@496.125.................*......965.....428.....908..................405.....650...................=..493..89..........................
..............581........396.............596.........*....964........672......159.%....463............768......./.............391...........
.............*...........*.....871.660..*......577.73......@.....932....#....*............*574...186........154......996........*......720..
............914..132..137......=.......746......*..................*......376.......&661............*560.%.......%....*...264....511........
...%...151................*851...................................*..284.........754..........190.19.......611...326.841...............89....
644.....*...797........133.....476..............................810.............*.....866......+.*..............................#903..*.....
........839..*..478...........*........372.#.....744.....227*............477.3.253.%....&..746.....289.&860.....720*708.....................
............443...%.....68*....283.......%..900...%....%..........584......*.$......308....*......*......................592................
...239................@....250........50........&...%...192.$111.*........74............831....914...#.....886*492..........=...............
...*......425.....181..680..............*......873.502............93.................................503.@..............116........372......
492..........*.....$.............302...789.................861.......................290.......452........83....626....*.....76.......*.....
......178...528..........*739...@..........................*......804....320....#.........&878.*.....746........./..&...148...$......741....
...&....*.............262..............325...........599..592.579.....%...+..808...............386..*...............761.....................
663..462...........*..........109..706*.................=.............553........712......*971......674.396...635*..........................
........../...728.952...413......*......744.......%..........................300..*....782................*.......742..&424........41+...564
........375...%.........*......450.456.$.........714........851.327..#...+......*...+.......179.630....854..................................
..251................195...887.....*........*209.......430.*......-.83.596....956..966.274.%.....*........................83.359........411.
....%.......................*..........89.........194..+...491.........................&.....*.762..+741..........598.......*.....910..*....
..........618+..............677........*..598........+..........-.....58....290...........795...........................*.....580.&....559..
...................561..............910......-..321....834....602.752*....=..+........191..............463....@..........792..*.............
............216.......%...722*668.....................$..................109............*....222...195*.......689....380.......637..........
.....846.......*..436..............=827...263@.764*.......*.464.................729.....934...*......................................289+...
.127*.......757.....&......./954...................965..664........./......716*.+...........266....781.........989..........844...$.........
................57.....................&118......=...........385.623...245..............345........*.....615-.....*..........*...768........
...538...........#..860......................783.460..........*............401.............*796..354..............37......797........725.193
....*..#620../.........*..........578..54...*..........780..251...634..946....*........................./....702........................*...
..202.......815........724....#..............822..972...................$..........*.....#....@........27.....*...........268.....347.......
.................+..........98.....703..............&....*52.....320.............670...318....734.............47..................+....239..
.953*919...=......273................=...454.....@....601...........*...958.........................+.................758...=...............
..........905./50.........@...................971..................762.*..................169........915.682..........=......533.......502..
.....884.................24...........262.........@.....531..698.......234.................................................%.......149..*...
....................834.....994.......*........266.....*......*............................................/.....941.434...812.......*..211.
.............133......*......*.....613...85..........871.......497..346...737....88.....176.....192$........128.*.......&......./163.26.....
....734..543....*.....656....461........*......531..................*........../...*..&................$971.....931.........................
................606....................506............................779.......30...211.....243..........................153...504.........

10
day3/in.test Normal file
View File

@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

79
day3/p1.c Normal file
View File

@ -0,0 +1,79 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <ctype.h>
int main(int argc, char **argv) {
FILE *input;
char *line = NULL;
size_t line_length = 0;
if (argc == 1 || argv[1][0] == '-')
input = stdin;
else if ((input = fopen(argv[1], "r")) == NULL) {
printf("Cannot open file %s\n", argv[1]);
return -1;
}
char **engine = NULL;
ssize_t engine_line_size = 0;
ssize_t engine_lines = 0;
while (getline(&line, &line_length, input) != -1) {
ssize_t len = strlen(line)-1;
if (engine == NULL) {
engine = (char **) calloc(len, sizeof(char*));
engine_line_size = len;
}
engine[engine_lines] = (char*) calloc(len, sizeof(char));
memcpy(engine[engine_lines], line, len);
++engine_lines;
memset(line, 0, line_length);
}
int32_t part_number = 0;
int32_t n = 0;
int32_t multiplier = 1;
bool adjacent = false;
for (ssize_t i = engine_lines-1; i >= 0; --i) {
for (ssize_t j = engine_line_size-1; j >= 0; --j) {
if (isdigit(engine[i][j])) {
n += (engine[i][j]&0xf) * multiplier;
multiplier *= 10;
if (adjacent == false && ((j+1 <= engine_line_size-1 && !isdigit(engine[i][j+1]) && engine[i][j+1] != '.') ||
(j-1 >=0 && !isdigit(engine[i][j-1]) && engine[i][j-1] != '.') ||
(i+1 <= engine_lines-1 && !isdigit(engine[i+1][j]) && engine[i+1][j] != '.') ||
(i-1 >= 0 && !isdigit(engine[i-1][j]) && engine[i-1][j] != '.') ||
((i+1 <= engine_lines-1 && j+1 <= engine_line_size-1) && !isdigit(engine[i+1][j+1]) && engine[i+1][j+1] != '.') ||
((i+1 <= engine_lines-1 && j-1 >= 0) && !isdigit(engine[i+1][j-1]) && engine[i+1][j-1] != '.') ||
((i-1 >= 0 && j+1 <= engine_line_size-1) && !isdigit(engine[i-1][j+1]) && engine[i-1][j+1] != '.') ||
((i-1 >= 0 && j-1 >= 0) && !isdigit(engine[i-1][j-1]) && engine[i-1][j-1] != '.'))) {
adjacent = true;
}
}
if ((!isdigit(engine[i][j]) || j == 0) && n != 0) {
if (adjacent)
part_number += n;
n = 0;
multiplier = 1;
adjacent = false;
}
}
}
for (ssize_t i = 0; i < engine_lines; ++i)
free(engine[i]);
free(engine);
printf("%d\n", part_number);
return 0;
}