From 2926608df2537dd8b8dbb9b4bf68e32ecd1ad58a Mon Sep 17 00:00:00 2001 From: Petra Date: Sun, 3 Dec 2023 20:40:01 +1300 Subject: [PATCH] 2023 Day 3 --- 2023/data/day03.txt | 140 ++++++++++++++++++++++++++++++++++++++ 2023/data/day03ex1.txt | 10 +++ 2023/src/day03.f90 | 151 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 301 insertions(+) create mode 100644 2023/data/day03.txt create mode 100644 2023/data/day03ex1.txt create mode 100644 2023/src/day03.f90 diff --git a/2023/data/day03.txt b/2023/data/day03.txt new file mode 100644 index 0000000..30b7cbd --- /dev/null +++ b/2023/data/day03.txt @@ -0,0 +1,140 @@ +........440...............418..643.....438......740.261......................................727...........................870.............. +...............338.............-........*.......*.......34&.$........@.....&742................................353..26.......*...188...238.. +..................*369.....334.......624..749....533........690...894...........466......../....&......294....................1............. +..338....367............................................../...............565..@....456.357....873........#..=...916.#60.................... +........*...............*......402.......473..%400..+415.165....364........*.....$....*......*...............410...=..........#........#.... +..342....886....122..457..866........438....*.....................*........739...716...131....561..748.......................206......155... +.................-.........../..........@.185..78.556........120..856..436............................*.....%730.307....382#................ +...................352...........................*....282.......$...........*......716......904*588....807.........*........................ +.590.......225....*......610...............&434......*...................805.878......*......................#539..412......452......993.... +....*407...................%............=..........489..............#..................390....621.708........................-.............. +..........542#...880...@.......*.....%...877..................264....3........750.............../.*.............-.......570.......&......... +.....290...........@.540....167.873.970................669.....#.....................169...........718....&...240......*........764.400..... +.......*......639.......................103.....@.......*..................................731.........413...........974...............*.... +......174........#..-331....&................465.............428...$118.....................*......714.......209*418........*....@......513. +...........869.............120.....101................379...*..................488.579...344......*.......................94.802.105........ +...834.............254............=........657.........@...198..160....258........*.............449........972@.553%.................234.... +...*.....@...........*....................../.....273............*...................&......................................545.365......... +.773......296.........85...256.......977.............*....203...216.....394......872.240.......406*.......&254...............*..*........... +......-...............................................830.....%....................*..............................................615.608... +...739..15*903....93.........714..442*...769.....838.......829.....741.....@469.......60...........181..376.......63.........172........*... +.................=......306............@...*.......*...900.....309..*...............-.....428..333...*.....*921..*....673............499.... +.........599...........&........221....137..432....930....*.....*..216..........#..381...*.....*....767.................@..........=........ +.993......-..............243....$.......................150...456.............677......400..338.............................766.468......... +........................*....................509....................815.............................................308.....*..........575.. +...................908.112........767....215..........400..220........*............962.825......46......30.....21-........875......453*..... +....845........840............../....*.../........423*....*....929..76......682-...*.....&.........@.....*..........&196.................... +867.*.........+.........=671....429..278......46.......$...194../.................611.............401.....788....&.......201.........*38.... +....21...599........................................863..................946...23............*547.............244...........*.....963....... +.........*...........65.............659......612..........497.......&705..................121.........................29....80.............. +.......670....391...*......&....405.*..........*..........=.....................721............321...........585.464...............598...... +....................892..472.....*...526....615.......51......790..........-633........847.......*...310..60..#..*.......517.415......*..... +.....732......*473.............825.................67*.......*.....488...........@....*....298.240....*....*....548.............+........... +........*..222.....377.........................710......512...839........340......829.836....#.........619.776.........695.358......417..... +.......51................632*........................85*..........*.......*..125...........%....@360............109....../.............*.... +...........880....836........979...........496.247.......885......754..658.....*..622.......840...........118......*.120......779..92..746.. +651....617....*..*...............@.....291*.......*883...*...................529...*.............531.........*...857.-..........%.=......... +.........*.......699.....639.583.181........457+.......699.829......308..........704....299.......*....332.622..........467*.........684.491 +......258...............................25*........441...............-...212..............*......820...............440*.....352.........*... +...34.........186................255.......526.618..........775.702.........*.940......=...822.........................515......947......... +...*.............*195.....693.......*..........#.....826.......*.....%....331...*.......49.....677..............920.........781*......-..... +....657....52.............*..........766................*..289.....589........34................*...................&............=...421.... +............*.......9.$...831..............*952....469.886....*.........283........525.....986..516..............184..949......670.......... +.568........283...../.431......*....420.259...................807..568..*........%..+..634*...........915..&938.........$................... +...*...........................77............874...558..............#....596..902..............368......#..................&....$.958..702.. +455....................389..............&..../.......+......*409......-.............909.......*...........978*341...188...74..26..*....*.... +..........17....360.......*............617...............153.........887.122.......*.......184........258........................755...348.. +......612.#.......*......425.968$..496..............918.......313........+....535..764.............19*.....*......173....=.................. +........-......440..384...................474...940..=..586@.&.....466...........=...............-.........797.......@.406...-......%....... +....816............*..............@.760....*.....*..................$..156...............676...451..462........774.........346.....558...... +....*.......258..888...#165.....166...#...27......766...........83......$.........&302......-........*........*............................. +..655.....$.................548.......................109......-....................................61..252..849.......299.....880....-..... +..........550.=495.62.........*.......-........691*......*648.......837&....736*722....*12.................*......*......$.283*.....35...539 +......475............*358...&.........605..........963..............................514....673..840........787....420....................... +..431....*....62.964.......814.$832.............................................844.......*.......*.293.......................297.....+..... +......%..730......*........................./..145...&............45.#.............*.....365....276...*..............125......*.......178... +....924.........814......143%.............295.#....811.*253.....-..-.910.....605..595................34..957.........*........413........... +..........220.................$.....769......................738......................461.177.........../.......848...63..407........354.... +.........$..........657.985...426........................-....................903.......*..#...703........784.....&......*.............*.... +...............105....&.-...................264..........50.....593.181..252...*......808.........*.......*............509.154.....678..210. +.......263........@...........57....854.............*38.........*....*...-.....89...........$../........%.889....404.........-.............. +.918..%.................*534..*.............838..731.......$501.409...............942....958..516....720.........%..............777......... +....$.........%435....24.......657.561.......*.......841...................961......./............-.......886*......442....627=...*....516.. +.........925...............827......*...149..236..28*.........599...249...$......958...........220............611.....*.&.......314......... +..........*..*.........120....*..$..158.*...........................................=..786...............428.......831...211.........%...... +.........530.456........*..355..74......870........65.........................843........&.../......@......*..........................425... +.....................657..........................*....50.......=............*.......93......817..413..598..83.............................. +.....818*427...+131.................459..474.938...570.........53........217..785...*..................*.......*......................#..... +.193....................233..491.......*................52.........$.......*........483...817...484....114....295.......%298..689..552...... +....*...........................#....25...848....@537..$...........302.....507............&.........................%....................... +...817..........383..431...464............................845....................896.........................715.920....468..........531.... +........................*.................337...85.......*.....................................331.........................*326..../.=...... +.........587.....533...601.................*...+.......726...949.760...374*.............849...............923.....328...........671......... +.705....../........*..............437.....426..................*...........945...............................*................/.....670..... +....*...........657....*593........./.............279.......672................*711..........742*679.........754....351@.......225.......... +..898...600..........57................86....826...#..................97....601........................93................163..........270... +..........*..............&.530-....324*.....*........833.....876........*.........908*........172$....*............548................*..... +..71......403.........584................377.........*......*...........920...........432.............723......650*....570.......593...718.. +...............739...........................199..418......16..668.............852.........555#...........................*516..*........... +....642.............*..................93@.....*..........................602.+............................49......&.............202.267.... +..........861..268...60.....-...............993......%...320................/...............261.........$.........897..................=.... +.........*........*..........249....................389..*.........416.........*...........*.............962...........928*310.............. +................33................727...346*850...........845.......*.......695.388...690..834.....471.........................746.....882.. +....576...............824.....491../...............464...........173..............................*.....514+....27.965........#....939...... +.................................*.............832....*.....515......337........./215..640$.......985.............*..................%...... +.......-..........343..586......................*....569..*....=......#...618.............................368.................%188.....815.. +....721...480..................847...............651.......576...........&........./...356..........287..-.................................. +..........+.......587...........*.....................34.......84.................96..*......790....+..............286..310.@........451.... +............929..*.......613.....480.389..761........*........*..............97......712.......*....................*..*....335....%.....795 +....125.762*......104....&...900.......*...*.......794........83..............*.............860.....$........622.723..771.......706.....*... +119*........................*....&....474..268..............*....65.........781.......817............814.................................940 +..............777.192.....187.302....................363.253....=.............................565........834........-.......519............. +..............*...................*..+197.........*.....................14....230.........................*....552.733..657....-......134... +....47$...448.744.....562..234..849.......894...298........136..........*....$...........650*..31...787....357.............................. +..........=..........*.......@..............*.............$..........839.........................*....*.............201...992............... +......+...........535...@.............989-..........477......................273.......625.....544.367....................+............69... +....456..493..........282..............................@....810..689@.361.....*...123.....*262...............995..429*........../......*.... +.............................564.115...507*720................................473..*.../........................+.....200....230........8... +.................814.....90.*.......+..........508.872..418....496*783.................643......&437..244.....$...................654....... +.......376..........*...*....32..........356......*......*.................691.../.......................*.221............813@.......$...... +.........#.........31....971....352@.....*...566*.......278....-.......%..*......25.........*...222....963.......42.............-........... +.....944...............................189.........#..&.......213....542.468.613.........494.......*..........$...............666........... +.......+...783...........930......*701......772...148.350...................../.../681..........*......@.......799......30........729....... +..................407.....#.....#............*............$.............142..................513.858...431................*.513...=......... +.....621.+720.41...@.............318...$661..197.104..997.252.*346.......*.....241..............................249....296../.........*355.. +.........................*........................*....=.................194..../....285............&......204*...*................614...... +....465......256.......31.154............84....990..............999..+.................*...965.....817..........674......................... +......+.........$...............515.........................554.*.....485.$411.......382..#...........................399....364....*568.... +.........87..52.....896..........&....................583..@.....550..........................@774...........97*106.....*......-.670........ +..........*...........*.&......@...2....*444.............*...388........................................................150................. +.........263.........20.623..264..*..513.................655.......430.....820.....................=....156..840/..883...........283*78..... +..........................................793.....298........../.......212...@..234.715.*133....705.....*............#...................... +.....................................*.......*...*.............992......*.........=..@..................870................324......=..712.. +.452.......818*237...........329...73.....754.....955....645.+...........478............340.970......*................817....@...982........ +....@..705..........115...................................*...157.620...........&.......*....$......565.....8.@.........*.............669... +..........*715.481.*....................*........836......714.....*...........124....968........=...........*..426....693........753...*.... +..26@..........*....892...272.259....461.387..82....*...........208...126.....................850..........299..........................135. +......947...682...*..........*................@...482.....854............*.....613.....................719.................................. +...33.-.........131....586............396.......#.....1...................889..+....*577..........782....*.......969$...............951.653. +....*........$............*.............*....469......*..111./822................105.........131+.*..........816........618............*.... +...95.....355..........+.....+.469.241..736.........831...........227.......639........474$........703..610.....*......*.....199.....$...... +..................597...572.85....*............462.......@536..........@793....*..77.........................798...374..20......&....246.... +......374.........*.................848....552*................545...........307......956.67............336*.........*...................... +.647.......#819..305.........735...*....................-........*...............824....*...*....100........383...@...695........%...245.... +....@.762...............727...*..373.........241*656...581..842.910...................672..229...*...............869............249......... +........*...........563......899.........406................*..........12%.....................78..614......+166........../92...........787. +.........558....282*.......................*.763...313.....999.....807...............+....489.......*............261...........130...*.*.... +.....356.............325.....265..740...923..*......*................*..../....120...456.....*....%.102.....791....*..141........@.666.65... +......*...870$.......*...431..#......*.......631...212............957..124.......*........558..946.............&.763...-..903#.............. +....437..............412....*.........29....................539...............703.......*............844.................................... +.........345................177..................609..398......@.....................138.185..+......*......................193-.....929.... +..............&...%....950...............489........=....*119.....909.522........%.............928.331...18.799.....*914.................... +....422.....304....753.*..................=.....751....................*........144.....53*878............%..$...579...............658...... +.......................618...................$.........344*91........533............439...........556..................=..............*..... +.........275..................................12.................764........&..........*..................710..609....902........80.753..... +............*.738......5#.................................595..........976+.887.......468.-.......114............./........*484............. +495.......804...*.................988....+..411...........=................................86...........$......=........631................. +.................458.........927...*..514.....-.......933.........192.850.85......858..209............379......462............139....70..... +........@.....+..........107*.....229....................*............*....%.896./......../..................=...........*.....#............ +........991..272.....575.................................958...........917.....*............*.......94.....985...+587...184................. +...............................................657..........................423..........742.367...............................634.......... diff --git a/2023/data/day03ex1.txt b/2023/data/day03ex1.txt new file mode 100644 index 0000000..b20187f --- /dev/null +++ b/2023/data/day03ex1.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. diff --git a/2023/src/day03.f90 b/2023/src/day03.f90 new file mode 100644 index 0000000..1035a5d --- /dev/null +++ b/2023/src/day03.f90 @@ -0,0 +1,151 @@ +program day3 + implicit none + integer, parameter :: max_lines = 200 + integer, parameter :: max_chars = 200 + character(200) :: fname + integer :: n_arguments + character(10) :: numbers + character(11) :: non_symbols + character(1) :: symb + integer :: part_sum = 0 + integer :: part_val + character(max_chars) :: schematic(1:max_lines) + character(max_chars) :: temp_line + character(max_chars) :: num_string + integer :: line, cp0, cp1, cp2, i1, i2, cind + integer :: schematic_width, schematic_height + integer :: istat + logical :: is_part + integer, parameter :: max_gears = 10000 + integer :: gear_parts(max_gears, 0:2) + integer :: found_gears = 0 + integer, allocatable :: gearmap(:,:) + integer, allocatable :: gearval(:,:) + integer :: gear_ratio_sum = 0 + + n_arguments = command_argument_count() + if (n_arguments .eq. 1) then + call get_command_argument(1, fname) + print *, "File: ", fname + else + print *, "Wrong number of arguments: ", n_arguments + stop + end if + do i1=0,9 + numbers(i1+1:i1+1) = char(ichar('0') + i1) + end do + non_symbols(1:10) = numbers + non_symbols(11:11) = "." + open(10, file=fname) + do line=1,max_lines + read(10, "(A)", iostat = istat) temp_line + if (is_iostat_end(istat)) then + exit + else if (len_trim(temp_line) .eq. 0) then + exit + else + schematic(line) = temp_line + end if + end do + schematic_height = line - 1 + schematic_width = len_trim(schematic(1)) + do line=1,schematic_height + temp_line = schematic(line) + cp2 = 1 + do + cp0 = cp2 + cp1 = scan(temp_line(cp0:schematic_width), numbers) + if (cp1 .eq. 0) then + exit + end if + cp1 = cp1 + cp0 - 1 + cp2 = verify(temp_line(cp1:schematic_width), numbers) + if (cp2 .eq. 0) then + cp2 = schematic_width+1 + else + cp2 = cp2 + cp1 - 1 + end if + num_string = temp_line(cp1:(cp2-1)) + read(num_string, *) part_val + i1 = max(1, cp1-1) + i2 = min(schematic_width, cp2) + is_part = .false. + if (line .gt. 1) then + cind = verify(schematic(line-1)(i1:i2), non_symbols) + if (cind .ne. 0) then + symb = schematic(line-1)(i1 + cind - 1:i1 + cind - 1) + print *, line, cp1, symb, part_val + part_sum = part_sum + part_val + is_part = .true. + if (symb .eq. '*') then + found_gears = found_gears + 1 + gear_parts(found_gears, 0) = part_val + gear_parts(found_gears, 1) = cind+i1-1 + gear_parts(found_gears, 2) = line-1 + end if + end if + end if + cind = verify(schematic(line)(i1:i2), non_symbols) + if (cind .ne. 0) then + symb = schematic(line)(i1 + cind - 1:i1 + cind - 1) + print *, line, cp1, symb, part_val + if (.not. is_part) then + part_sum = part_sum + part_val + is_part = .true. + end if + if (symb .eq. '*') then + found_gears = found_gears + 1 + gear_parts(found_gears, 0) = part_val + gear_parts(found_gears, 1) = cind+i1-1 + gear_parts(found_gears, 2) = line + end if + end if + if (line .lt. schematic_height) then + cind = verify(schematic(line+1)(i1:i2), non_symbols) + if (cind .ne. 0) then + symb = schematic(line+1)(i1 + cind - 1:i1 + cind - 1) + print *, line, cp1, symb, part_val + part_sum = part_sum + part_val + if (.not. is_part) then + part_sum = part_sum + part_val + is_part = .true. + end if + if (symb .eq. '*') then + found_gears = found_gears + 1 + gear_parts(found_gears, 0) = part_val + gear_parts(found_gears, 1) = cind+i1-1 + gear_parts(found_gears, 2) = line+1 + end if + end if + end if + end do + end do + close(10) + print * + print *, "Sum of part numbers: ", part_sum + print *, "Number of found gears: ", found_gears + + allocate(gearmap(1:schematic_width, 1:schematic_height), source = 0) + allocate(gearval(1:schematic_width, 1:schematic_height), source = 1) + do i1=1,found_gears + cp1 = gear_parts(i1, 1) + cp2 = gear_parts(i1, 2) + gearmap(cp1, cp2) = gearmap(cp1, cp2) + 1 + gearval(cp1, cp2) = gearval(cp1, cp2) * gear_parts(i1, 0) + end do + do i1=1,found_gears + cp1 = gear_parts(i1, 1) + cp2 = gear_parts(i1, 2) + if (gearmap(cp1, cp2) .eq. 2) then + gearmap(cp1, cp2) = -1 + gear_ratio_sum = gear_ratio_sum + gearval(cp1, cp2) + end if + end do + print *, "Gear ratio sum: ", gear_ratio_sum + + + + deallocate(gearmap) + deallocate(gearval) + +end program day3