diff --git a/3/1/input.txt b/3/1/input.txt new file mode 100644 index 0000000..6337732 --- /dev/null +++ b/3/1/input.txt @@ -0,0 +1,140 @@ +.........426.............985.........40..........207............................841..463................................633........17.384... +531&......+..........125....-..312..........#........895......998..945.....@......$.....-...33...................353.....*........*......... +........................#......*...........21..727..*..../..-./.............545......80...................602......@..272.......743......... +...........558.577..........486...186*925.....*....483.883.1....286...................................625..................#474.....491..... +..............*.........243.................287................*............$....245............830.........793......#..........306..*...... +238.685.................*................#.........%........807.........28.947.................*.....705.....*....573...500*781...#..496.... +..................989..923.......713...539......917.................115..*.....-...........662.........-......413........................... +...........=......*..........886.*.........................442......*...........398........*.............%.............636...........%...... +............976.413...498..../...266........796....................87.....................969.881..&.....815...........*.....279....415..... +......728*..............*..............129..........670...890.....................760...=.......@.832........227.....632.212*............... +..........257.....712.491....-41...........970........*....*...373........742.......*....330....................*.....................814... +....................*..................415......406..441..35..../.........-..........828.........................239.....@...533*206........ +......687.........834......448...658...*..........&..................467*..................244.......+332....*..........35..............*... +.............453.......616..*...........609.............662......672.....56........661.602*...............549.661...................141..73. +.......699...*.........*...904..........................&..........*...@..............................189..........$677.......473.....*..... +........*..670.........938.........267.......684...........514...211....875.........667..171.110&.426*......../...............*........29... +.....164..........................*....@786.*..........646.=.........43.......885....+...=.............641.....996...........117....28...... +.........*921....150*....635..973............587...346*......771........&3....+....................=................................@....... +......962............821....*...*...........................$....174............391+......732*177...101.....146.............555............. +..........................117.965.........327.....................*../127.54.........354.....................*...37/.......&....56....656... +.335.747*967..*469.................................-.....$.450..560...........553...+.......327*..........215.........950..............-.... +......................253%...........796.........267...946..*........&...583.-...........-......727.37........@....#....-.......337......... +.............-...............-.649.........-.................316..680......*.....982..398..................961......632...718......*..%..... +..........599.............876......700..617....344*138...611..........907........-...........................................*...291...209.. +.860...........................*......*......................850........*..............=....761...721*804...............%...33.............. +...*...........................61./....378........268....956..#..813@..191...610.168..216....$...............853%......826.................. +.757..=422.........................427..........+....%....=...................#.../..............908................@....................... +.........................172.................464...........................-.....................*.......667...931...131..@.........939..... +............456.....462....*.....985................801......933..464..641.874............*189..42.........................430..596...=..... +..............*....%........945.@........456....*23...........*..........*.............335.........889..557#....................*........... +....810.........65......382................#.532...............704......796...192....................@..........46...527......929........... +...=....%..................*433...........................925.............................................30.....*................617....... +.....792.......386.....................575...%859................................................................993...334*..........&...... +..........657.&.....&....................*.....................296.+513.............36..........267........703.............186.............. +......336...........443...#.....474......897.215*346.....100.....=...........+.812../......47.%....*444..&............128........-...196.... +705.............980......932.......*122..................*...........305..817..*......424.../.40.........193.210.736.*..../....&.512..*..... +...%...............*.....................................326..615.......*.......472..*...........350*201.......*..@....681...902......330... +...................907...303...........140*112.............../........93...875......339....361=.............730............................. +..............540...........*...................498.170.....................*....................#...757........241...............201....... +...359*42.....=.......309...561....528.....444.....*........570............................125/.379......707.......*........*285.$.......... +............%.........................*770.............253..*....515..926..................................=........45.946..............*... +....155..573..103.24..............................@......*...179..*........275......................*...................*................134 +....*............*......963...........444......801...656.796.....524.84#......*433.......997.....122.500....711.......447................... +..80.........992..........*............*...+..........*....................................*.................*...............$.....894...61. +........183..../.......492..955....+.222....519........373....=......304........151.........691..............655......223....37......*...... +.........$...................&..221................859.....929.......*...398.......*......-......591.....&.............$..........134....... +................960......264........./..............#..............508...@........118..449........*...907......=791......................... +...........239....#.....=...........248.531...................@..................................696.......741...................50..174.... +.....=......*............................*.....506*809.....796.....906.....=...........17*...........51...*.......................*..*...... +...520...198...781....253..............957.222.....................*.....216..............513....959*....638.312........172.318......514.... +...............*.........*239...............*.....%.....923.........852..............414+............../.....%...104.......*.............936 +.....221.....116.....712...........201...346...475.........*...............&..............745....109..202...........*....................... +....*............957*.....$....353...............................171....468....%..........*.........*.........906..593..592#......&316...... +.........232...........251....#.......%.......................$...*...........179..904.....365.....331..541..*.............................. +...................................856..........412..-....844.877..871....*.........../.................*....416...........810*685..776.969. +.158.......@......319........681..........780.#...+...428.*............650.241....@....................................123.............*.... +....%....826......@....%.688*.......983../....723.........892...................270.....234....*......443...970...........*................. +....................314................*.................................................*..=...588......*.....%........225...496*732....... +...344+........................644......413.997....553...................................87.282..........943........866..................... +..........*563....910......267..*..546........*.....*..................60.............+..........421*739.............*...251.169.605........ +938.....52...........*761.*....762............335....842.....................44/.......528..........................77......*.......*201.... +..........................597......$.....................896.....543.76.........................938./707...............8........982......... +...894..195.......310*986.....=54.854...553................#.......*...*..192...127..566*768..........................*....850...*..970..... +.....*..................................=...835...874...........663...65..*.....*.................*947..539.12...947.485.....*..749..*...... +......34.378........$............#.........*......*......922...............947.598..880........723.....*....*...@..........791........18.... +..165.............388..94.......863........132.184..973.*.......58.................*......226..............618.....................=........ +.....*.............................................@.....154.............240..34.297.......*...........429.....625...............672........ +......619...#............641*520.340.918............................../..@...............390......502..-..../.*.....164....789........984... +.............933.....................*.....838..240..400&..........288.......196..109............*.......749...513........*.......751...*... +.....709.....................881..905.........*....*.....................643..*.....@...114...608......................190..........*....993 +...............109...........*........368..123....98...501.....691..410-...*.126......................92........+..546...........219........ +..................*......18..70..296.....*..............&..987..*........235............8.............././...828...*........................ +.151.............551.153*..........*.....883...............*.......*833........497..287*..427.377%.428...910......448.598.....334..53..619.. +..........589.......................723.........832.131..705.............................#............*.................@......*...*..*..... +..........*........794*686......790...............*..........192..............................970......612.546.98............942......941... +....223.686..............................547...665.......396..*.....210......408.322..839.......*..557......*...+.................521....... +.....*.......170...........166.....390..............100.....*..921....*.........*.....+........984....*..450..........639..../....*...296... +...372..........*760...266.*...600*......999.........*...906........................#....632.......469........250.......*...422..643....@... +..............-........#...911......494...*...355...159.......................848..676.................../.....*......35.................... +......81$.....544..67...............*.....159....*............209.747*29........./..........812.........430.232...................199*587... +760.................*...#........331.................%...158...................#.....................29.................596...477........... +..........%./....922...388..970...............510...68......*....590....545...621...-..977.593..889...*.........................=..879..*389 +....163.618.370..............................*...............946..................332....*.......&..&..837.658......................*....... +.............................835.....129...566.....690.237..............#.............$.....386....36......#......411.....52..733...783..... +.........670...978.84...........=......*.......991*....*....632.......957...52.......548.....&....................*.........-.*............. +....920....*..*.....*...............949..............482...*......812........-...413.....201................663.286.....-......424...296*630 +........992...307...634.................716..287.........811.....+........4......*...413*.........896......*...........84.52................ +.........................253.............................................*.....459..........926..*.......446..............$.......175....... +.........................*.......168-.....781*......344.........85......373..........224.....*..393.545........749%.643......-..%.....968... +...*776../145...&.....140..........................*........578*........................#..45..........*............/......596..639......... +415............393..................988...........878.22..............909..........284...............173...564*48.......33.............444.. +........259..........%.....207-........*631...........*......669*...................*...........282......%........%.....*................... +.130................119.....................422......440.........461...-154........575.............$.=....329...180..576.....367............ +...........618...............=.../..538$.......&............493*............729..........903.........82.......................*............. +.............*............541..124.........627........806*......666.703.146*...........*.%..............-.....................142........551 +...#..3......660.....................*........*.......................*......./164...498..............872.621....991........................ +174....*...............683..542*.....960.....782.....755............557../.....................#282.......#.........*409.103.......943...... +........231.891...........*.....782.....................*992.............492......69.......610...............+...............557......*..... +............*...964.104.289..........853......691..830.......&.....................*...870.*.............77..189.310*...............583..... +....449....435.*....../........732+.=.........*...*.........238...161.....688......675..*...934......................891.................... +...............21.......................239........430..........@....*.....@..376.......205......790........................73..334...598... +......863-................474...339.....*.................765.534.116...........*..................*..........@.......513...-....%...../.... +.262..........#......785....*..$....915..622....440$.&416.*.............143...34..45....515#....448...........268........*.................. +...*..+.......844......*.142..................*...........666.......266...*......*...................236..............439......338.496...285 +647...393...........236.......218..........819.779...+................./.........765...................*.......292.........$......*......... +..........................257....*.................274.........247...........951........................121........391....817.../........... +........742..................&....98...................628....*.................%.168..........539............................701........... +.......................659................................*.622...868.............=...713.357....*.298....917...781......................... +.....467....382..899......*819.....952*388..............684.......*.....................*.....558...%....%...............$..*.....60..615... +.....*.....*.......*............-..........604.................85.76....%893.866.&...784.....................$423.....334....935..*....*.... +..154....424......583.......8....926...$............$441.....................*...50...............=....155.......................599..115... +.............%............-...........96.506..846.................152.....985..........589.......384...@...44...............560............. +.............161...%167..906..901.........*......&....92..+610.......*247.............*...................*...........330..*....92.......... +....26.........................*...........523........*......................190...711.......*..........302.690.........*.986........714@... +........154.....................763..59..............305..560...726......79...*...........521.160..159................81.................... +991.339*........-.........434..........*....................-..*.........*..626....................*..............692......+................ +..............448.....96./...........782............353........949.....655...............752.522....905...964#......-.683..83............... +.......=..........127../.........$....................../.......................................*......................+.........671........ +.783..14...........*...........746....271.38............666..................513..37.....=984.824....@....$..463.................*...154.... +.................60..892..304............*....354*20.................99.........*..%.351............367..914.*....622...........310...*..194 +.../.....339............*......318..........6.................329*...*..240.....76...*...45....*..............601......+.............211.... +.700...../...925......616...........*56.570.$...970...751...........556..%..958.....50..*...667........63...........793..................... +...............................8.........*........*......*854................*.........304........324.....%..................*.....213%..... +832...............%....$.........572......303.....872.=.............822.....2...434........%..76.@......34...866.684*.......571..........756 +.....922........908.811......792....#.967..............432..............304......+........676......314........*..................364....#... +........................=.....*.........*.......39............127.........*........27..............+.........937.172.....532*......&........ +....*.............156....626...667......151..../.......................776..846...*............562.....254$......../..@......297.......751.. +..457.213.....403*....=......................#.......656.....546%..............*.......*866......+...................954.576...........%.... +.........*..........227........364....752..977.231..*...........................739.610...................................*...730..#........ +......838......645........$692.................*....170...............906........................149.....578..470.........981.....310....... +959...........*....447.................33.513...412..........784.......*....................365.....%....*.................................. +...../......593......*....=..965..=907...*.............................482.....................*.......915..............327...529.....425... +......613.........%.490..971.-..............&542..............................695.......803.....917........446.....53...*........*455..$.... +...............258..................+..303+..........................517....7*....598..@....472.....224...*............903..#............... +........................724...+....575........312...&.........................................*....*......628........@.....108.............. +.....343.374.......$....*....675...............%...371......409.....502.928.135...482.384....195...59.............144..982........787....... +....*....*.......289..729..........990....................../.........+.......*..*......*...............*.@...........#.............+....... +....147...613.............*534.........938....882...740.518.....994..........800.222..933...836.......260..339.=...........628.$935...../... +...............726.....308.............%........*...../.+........=..../146.................*...................509..........*........593.... +930.........................823..............994.................................100.....857.......................708.220.184.............. diff --git a/3/1/puzzle.txt b/3/1/puzzle.txt new file mode 100644 index 0000000..b160962 --- /dev/null +++ b/3/1/puzzle.txt @@ -0,0 +1,44 @@ +--- Day 3: Gear Ratios --- + +You and the Elf eventually reach a gondola lift station; he says the gondola +lift will take you up to the water source, but this is as far as he can bring +you. You go inside. + +It doesn't take long to find the gondolas, but there seems to be a problem: +they're not moving. + +"Aaah!" + +You turn around to see a slightly-greasy Elf with a wrench and a look of +surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working +right now; it'll still be a while before I can fix it." You offer to help. + +The engineer explains that an engine part seems to be missing from the engine, +but nobody can figure out which one. If you can add up all the part numbers in +the engine schematic, it should be easy to work out which part is missing. + +The engine schematic (your puzzle input) consists of a visual representation of +the engine. There are lots of numbers and symbols you don't really understand, +but apparently any number adjacent to a symbol, even diagonally, is a "part +number" and should be included in your sum. (Periods (.) do not count as a +symbol.) + +Here is an example engine schematic: + +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. + +In this schematic, two numbers are not part numbers because they are not +adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number +is adjacent to a symbol and so is a part number; their sum is 4361. + +Of course, the actual engine schematic is much larger. What is the sum of all of +the part numbers in the engine schematic? diff --git a/3/1/solution.sh b/3/1/solution.sh new file mode 100755 index 0000000..a6b3d75 --- /dev/null +++ b/3/1/solution.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail +if [[ "${TRACE-0}" == "1" ]]; then + set -o xtrace +fi + +cd "$(dirname "$0")" + +LINES=$(wc -l < "$1") +COLUMNS=140 +mapfile -t -O 1 INPUT < "$1" +declare -a valid_numbers + +find_numbers () +{ + # Find and append valid numbers to array. + for ((line=1; line<=LINES; line++)); do + for ((column=1; column<=COLUMNS; column++)); do + if [[ $(arr "${line}" $((column-1))) =~ ^[0-9]$ ]]; then + continue # not start of number + else + local number='' length=0 + for ((i=column; ; i++)); do # find length + if [[ $(arr "${line}" "${i}") =~ ^[0-9]$ ]]; then + ((length++)) || true + else + break + fi + done + [[ length -gt 0 ]] || continue # not a number + for ((i=0; iLINES)) || ((column<0)); then + echo -n '' + else + echo -n "${INPUT[${line}]:${column}:1}" + fi +} + + +main () +{ + find_numbers + sum=0 + for number in "${valid_numbers[@]}"; do + sum=$((sum+number)) + done + echo $sum +} + +main