Finish D3P2 tmrw, P1 done

Change-Id: I7950f6d287c11a0c1c3f6568fa350abc35a9d1af
Reviewed-on: https://git.clicks.codes/c/Coded/AoC2023/+/161
diff --git a/day 3/index.ts b/day 3/index.ts
index ccd56c7..06ad2bc 100644
--- a/day 3/index.ts
+++ b/day 3/index.ts
@@ -1,6 +1,6 @@
 import { readFileSync } from "fs";
 
-const input = readFileSync('./input.txt').toString().split('\n');
+const input = readFileSync('./testdata.txt').toString().split('\n');
 
 type coord = `${number}:${number}`
 
@@ -9,6 +9,10 @@
 const ignoreCoords: coord[] = []
 
 function formatCoords(x: number, y: number): coord {return `${x}:${y}`}
+function getCoords(crd: coord): {x: number, y: number} {
+   const [x,y] = crd.match(/\d+/g)!.map(s => parseInt(s)) as [number, number]
+   return {x,y}
+}
 
 // INPUT CURRENT COORDS
 function checkNumChar(x: number, y: number, currentNum?: string): undefined | string {
@@ -48,31 +52,127 @@
    // Parse Coords
    const [x,y] = loc.match(/\d+/g)!.map(s => parseInt(s)) as [number, number]
 
-   // Check Next Door
+   // Next to
    const lx = x-1;
-   const rx = x + 1 + val.length;
+   const rx = x + val.length;
 
-   if(
-      coords[formatCoords(lx,y)] ||
-      coords[formatCoords(rx,y)]
-   ) {
-      sum += parseInt(val);
-      continue;
-   }
-   
-   // Check Above / Below w/ diagonals
+   // Above / Below w/ diagonals
    const ay = y - 1;
    const by = y + 1;
 
-   for(let i = -1; i <= val.length; i++) {
-      if(
-         coords[formatCoords(x+i, ay)] ||
-         coords[formatCoords(x+i, by)]
-      ) {
-         sum += parseInt(val)
-         break;
+   console.log(`${loc} (${val})`)
+   if(
+      (coords[formatCoords(lx,y)] && isNaN(parseInt(coords[formatCoords(lx,y)])))
+   ) {
+      console.log(`${loc} (${val}) next to: ${formatCoords(lx,y)} (${coords[formatCoords(lx,y)]})`);
+      sum += parseInt(val);
+      continue;
+   } else if(
+      coords[formatCoords(rx,y)] && isNaN(parseInt(coords[formatCoords(rx,y)]))
+   ) {
+      console.log(`${loc} (${val}) next to: ${formatCoords(rx,y)} (${coords[formatCoords(rx,y)]})`);
+      sum += parseInt(val);
+      continue;
+   } else {
+      for(let i = -1; i <= val.length; i++) {
+         if(
+            coords[formatCoords(x+i,ay)] && isNaN(parseInt(coords[formatCoords(x+i,ay)]))
+         ) {
+            console.log(`${loc} (${val}) next to: ${formatCoords(x+i,ay)} (${coords[formatCoords(x+i,ay)]})`);
+            sum += parseInt(val)
+            break;
+         } else if (
+            coords[formatCoords(x+i, by)] && isNaN(parseInt(coords[formatCoords(x+i, by)]))
+         ) {
+            console.log(`${loc} (${val}) next to: ${formatCoords(x+i,by)} (${coords[formatCoords(x+i,by)]})`);
+            sum += parseInt(val)
+            break;
+         }
       }
    }
 }
 
 console.log(sum);
+
+function getNumFromRight(x: number, y: number, curCoord?: coord): coord {
+   curCoord = curCoord ?? formatCoords(x,y)
+   const char = input[y][x];
+   if(isNaN(parseInt(char))) return curCoord;
+   curCoord = formatCoords(x,y)
+   return getNumFromRight(x-1, y, curCoord);
+}
+
+
+// Part 2
+
+let ratio = 0;
+for(const [loc, val] of Object.entries(coords)) {
+   if(val !== "*") continue;
+   let curratio = 1;
+   let curgears = 0;
+
+   // Parse Coords
+   const [x,y] = loc.match(/\d+/g)!.map(s => parseInt(s)) as [number, number]
+
+   // Next to
+   const lx = x-1;
+   const rx = x + val.length;
+
+   // Above / Below w/ diagonals
+   const ay = y - 1;
+   const by = y + 1;
+
+   //left
+   if(!isNaN(parseInt(input[y][lx]))) {
+      if(coords[formatCoords(lx,y)]) {
+         console.log(`${loc} next to: `)
+         curratio *= parseInt(coords[formatCoords(lx,y)]);
+         curgears++;
+      } else {
+         const n = getNumFromRight(lx,y)
+         curratio *= n ? parseInt(coords[n]) : 1
+         curgears++;
+      }
+   }
+
+   //right
+   if(!isNaN(parseInt(input[y][rx]))) {
+      if(coords[formatCoords(rx,y)]) {
+         curratio *= parseInt(coords[formatCoords(rx,y)]);
+         curgears++;
+      } else {
+         const n = getNumFromRight(rx,y)
+         curratio *= n ? parseInt(coords[n]) : 1
+         curgears++;
+      }
+   }
+
+   //above and below
+   for(let i = -1; i <= 1; i++) {
+      //above
+      if(!isNaN(parseInt(input[ay][x+i]))) {
+         if(coords[formatCoords(x+i,ay)]) {
+            curratio *= parseInt(coords[formatCoords(x+i,ay)]);
+            curgears++;
+         } else {
+            const n = getNumFromRight(x+i,ay)
+            curratio *= n ? parseInt(coords[n]) : 1
+            curgears++;
+         }
+      }
+      //below
+      if(!isNaN(parseInt(input[by][x+i]))) {
+         if(coords[formatCoords(x+i,by)]) {
+            curratio *= parseInt(coords[formatCoords(x+i,by)]);
+            curgears++;
+         } else {
+            const n = getNumFromRight(x+i,by)
+            curratio *= n ? parseInt(coords[n]) : 1
+            curgears++;
+         }
+      }
+   }
+   if(curgears === 2) ratio += curratio
+}
+
+console.log(ratio)
\ No newline at end of file
diff --git a/day 3/pinea.txt b/day 3/pinea.txt
new file mode 100644
index 0000000..50c8971
--- /dev/null
+++ b/day 3/pinea.txt
@@ -0,0 +1,140 @@
+.....................................164.................429.35...........221....................................................34.........
+........................464...........*.................................../.......53*.....954.763.....................114*.764..............
+223............275.....................725.....$.........460....176............................*............+.................&.267.........
+.........854..........919.798...............541.....302...................723......$...............196.......275......$....@....*...+2...388
+..........@.......284*............429..211..........*..........633.............503..66......865.....*....234..........21....918.779..../....
+...71....................40...856*........*.......................*.....438.......*.................636.#......671......................404.
+.............585..........................866...971$.....869......548......#.456...907...146$.320%............+....*..354....*870...........
+.........................334...75.800.....................*...746.....&......*..........................984.......174..%..417..........701..
+.....497...................*....*....$.......397=..620..671......*.852..656..616............................503...............#.......*.....
+.......*.....160.613....559...100......408............*.......574........+.........737......333...502.........&..937...395..21........58....
+......55........*.......................*....569.397.948...6..........................*......*...%.....*172........=...$........131.........
+...........*760......./...........502....169....*...........&....=..40.................592.552......660...................@.....*...997.....
+..625....17........225............*................855.........795..*............713....................*...496.....420...432.615.....#.....
+.....*..........................305.........%..863...*..............94...382.....&....................94...@.........%......................
+......791..................................609.&.....733..234.............$.........../921.....*.............678...............454.....287..
+............................................................*...$....#.........618..........548.485..........*............./..%.........*...
+...................703..332*259..+192.....504.....=......539..693...64..567*......*487................633.200.......886...550.........129...
+.576...533.........-.............................791........................658...................776.................*.........*...........
+.............916................719..........753..........@..376.......102.......................*.......964........390.......262.934*185...
+.......%.......*.....@592.........*...............998..886..*............#...650.............+...930.764.*.....225..........................
+.....473..63...614.................123.771....615.&........122......84.................&...842...........201...=..............831*61.....388
+......................./......=.....................%.................*.........@......243........................781....594.........541*...
+.........+850...........747..686....131....=....893..151...............105.967...557.............163.........596.*......&...................
+346*869............................*....471........*...............449......*...........$.......&.....302....*....482.....@..247...784......
+....................................668............647.282*746.%....%..739.931..........56.........=..........687........439..-...*.........
+..337.....................108..................................94.................952............37..903...-8...............................
+.....*826.......259.........*..668.....299.......687......654.....905.........302...*.*239..299.......*........................566.......60.
+...#.....................789....*..........899...*....674....*......*.632.........511......*.....865.640.931..................@......573*...
+...986..........................154..386..-.......303........691.562..*...................73.544..........*.....987....811..................
+.........703.750..........418%..........*....439.......................474..731.350............*..127*318..554....*...$...............%181..
+....................828...........=....446...................................#.......864.....657.................193....*........194%.......
+.......................*387.....59................882..278.............586................@.......%......#138........595.276................
+........$.................................596.....*...*..........&.....*...308....465.....899...454....=.....................351..235..229..
+....929.228.960............+632..148.....*......763..652......203...774.......*36....*................91.....................*....*.........
+...@..........+.........................919...............................917.........529.964......$.................*....55.727...742...877
+.....+830.........570...819..468...794.......&430.......590........212...*..........................278.....*.........714..*................
+..........894....*.........+....%...*.....................*...540...*.....803..../........413............886..............478....255*.39....
+......../.*...328....&205..........635.398*.........119..449....*....533.........479......*......................837-.619..............*....
+.....459..923..............................51..........=.........830.....................231..452.............................465..788..762.
+................648......../..................671.....................399%.......137..........*...........172...........457....*......*.....
+........../.323..*......367......407...@731...*.........981./658.277.......@321.*......10.....753.....275*.....150*815.*....555.......371...
+.16....950.....*..531........................810........*..........*.............155...=...............................747..................
+.........../..635........407.........909*189...........915......980.........*............$591...489..............353+............273+.......
+...865...34........790../....*866............................38......377....719.......42...........*708..104..........................%.....
+90.............172.......................540*361..504....895..*..874*..................*..473=............*...../...................19......
+.....283..310.....................7..............*........*..947........563.........406.........=....102..113.805........704................
+............-...#....702...795%....*............755.592.640.............*...#..911......208.*...813...*.............131....*....&.....120...
+..............*..103..*............994..229.......................598.979.729....*.....&....182........729..410*772.*...177......900....*...
+........=....952......898....203.......*.....*268..........187......*............725......@.........................681..................410
+..97...430.......951.........../......691.769........942$..#.../.....660...............841...-.@.....169.................666................
+................*................705.........................696.708..............+........599.306.................798......................
+................622..960......................473.................*............158....*337.....................=....*...............505.....
+.........664..........*........-........845....*....831...........802.563.1........807.........264....-....117.58..615.........29.......937.
+...431................909.......718....*................921..../......%....*341................*....263.....*............&.....*...685......
+......@.................................947..............*....536...................106........695...........774....42..722..636..*......264
+..............................................%201.265....657........@...+267.502.....*.282..........228*.........................737.......
+.....................545............810..868.......=................217.........*...479.+................725.................802............
+....409..............#..............$....@...........=..........29........74...341................405..........512.............*............
+.........*677..859*.........247............&.....325..475.........*244...*................175.300....-.....446../..*.........909........*...
+...69..............836........*...........580...*.........................632................*...............*.....856..&275.........789.475
+.....*426.619...%..........706..318...........807..............................2.633..................301/....188...........................
+................886.181........*.....42..236*..............260.833............/..$.........931......................430...$...24............
+....69................*......345....*........103..............*.........933.........769....*...............171*438...&..91.......202/...423.
+....%......264*939....541..........253.................516.........636.....*...349.....*....881........476..................@...............
+........41......................+......568......301........610.667........531.........409................*.......734#......62......#....545.
+..........&.........213.........960.......+.......*..979....$....*...........................191..573.606........................673...$....
+..363..................$...220......858.....95.................750................*...........@.....*.......534............468..............
+....*.925%.=..................*.....&...422*...........579..........#..........764.155...............874....*...=28.916.......*..@..........
+...........380......210.303....221............877.879.*...........867.73-..617................283........208...........@.....147..657.......
+....*...........656...&...%...............195....*.....690.................@.......*...........*....823.......803..770......................
+.319.151.+119../.............701*767.........*.....................578.741...29.142.623..........*........718..-..............239...935.....
+.....................123....................839.............422.......*......*.................476...671....-...................*......&....
+..............747.....+....159......115.........@.............+.........32.556....@164..740........................662.550......718.........
+....+..775-.....*............*.........*.........899....978........938@..*................*........726...462*796.....*.&...&243.....353.....
+..340.......+....70..128...559......323..622................924.........85.............383........................269...........752.=.......
+.............315....*........................345......444..*.......318.....................................179.........420..158*......67.278
+.................772..470..&...........354-............*.......346*....576.352.......*......*766.553..535.....%........*..........173.......
+......=...335...........&...676........................431..................*.....382.............*../....711........221.........#..........
+......944..........#......*......................474..............387.380...289.......988....204.869.......*......+................391......
+................938......790...=.....296.219*564....*........101..*..................*.......*...........41...440.851...47.....87..*........
+........534....................853.....*.........150..511.....-..347......-..529..773...181..417......#.........#..........234./..957.......
+....................................419......39.........................279...............*.........249................158..*...............
+..................&96.....711..................$.......=919...............................31.............135.528...378*.....................
+.......668....939........+.....................................@.....303..9.447.201..............799.505....*...........*654....*257........
+........*...........851.........................................942....*..*....*....662.%........*.....*.....................335............
+......939..764......*......830..........825/........779*680...........948.565.........*..8..470.691..68....*8.........%...%.......807.......
+..........&............786....*671.................................................297.....*............688......760..532.416....*......36..
+...............939.280*............535......................801..710....*284.............254........................&........./...105.......
+....703....../...=............506.....*.............................*...........733............975........200.218.............34............
+...*.......867......309...900*......918../.......749*980.........#.43.....533.....*...279.162..*.........*......*.....137...................
+....944...........1...*...................693.................207.........../.....964.*.....*..812.....452....12.......*....@........174*931
+........927..559.*....389........@................%...........................311.....37.542.....................+..759......636............
+.906...@.......&..243............709.............236.........465.....739.........$..............762....438*892..577.........................
+...@......420...............319.........898.424.......897.......*...%...................338....*...........................=..........9.....
+......297*......................+926.......*...........*.......713....703........./.324....*...718....846....974....390...252.....529*..65..
+.643........317........................530............927................@.....224...../...544........*.............*...................*...
+..........7*......15......356.............*...648.149......578......#114...972...................=....452.626......170.................635..
+.....................355..*..............997.....*......*......662.........*...89*824...734.....855.......................57.....876........
+........813.....*911.#....614...382*37.......-.%........868......./........605.............*........&...$.....95..37..10..*........*..99....
+........*....516..........................972..27.*457...............987........751.#619...785.......5..682...............892...845...*.....
+........177.............../....................................899...............*.............+....................................163.....
+............44.........225...=.........991.................995.*.................106..430.....197.....228*....87.....481.......682..........
+..............*.............957.234....@...95..................788...........497......*...................490...*976............*....491....
+....169..417.471.378............./...................178*806...................*....886............891...................225....231....*....
+....*............../.......804......238....380....................&..796.....228....................*..608.....$885....................381..
+.....243......490.........*..........*........-..54..294.........19.....*.........864..27...849....927....*......................261#.......
+................*........432..814....638.............@...400.........727.....165....@........+...-.....202.......565..666...............455.
+...........764....636........*............819*412.........@....775%......632...=.......156.....717.........33&....*........724.....774......
+......*559..#.....@........787.611................111.746.................&......962.....@...........@.........690.......=..*........$.178..
+544.45........377.................*........986...*.....*..........317..............*...........-64..800......*.........150...257..@.........
+..........*.....$.&...............410.........*...635..446...353....-...774.820.773......397..............364.775................955....*...
+........34.711.....814........................912.............*............*.........924...*..........11...............735.&880......935....
+...............694...........627..587...-.....................778..&969.......867.........336.....887*......640........*....................
+...-......889..*................*.....63.......349....266.......................=.953...................................416...945...........
+...11........$.415....80.644.832.................%....=.....368=....*.............*.....44......689@......967..220.499..........#..241..#...
+......................&....*......712/....25.......................781...935.....64.......*..........25...*....*....*........*.......*.33...
+..........-.................568.............+....347.465......851........*.................530.........*...299.597...755......294.385.......
+...223....57..........%849..........................*.....*.....*.....847.......%..270..............531..................474................
+42...*........................+........890/.499.........585......690.........839......*..................579................*.562*..........
+.....968...........*764./59...902............*..91...........992........$............638.............210*.................320.....911.466...
+............213.364.............................&........................395...117..........................#........901*..............+....
+....946*521..@.......185.....792#...........432....193.............27...........+.............668.......419..489.........428................
+...............................................$..................*...................176.........-........*.........648....................
+......-.....$......6..........1...........&.42...........681......684........%.../.......-........564...911......751.*........=76..203*.....
+.....776...588......@.............760..615.......822........*...............421...246.........586............*........279..............426..
+.................@..........106....*........773.*.............................................*.../.147...744.324.................490.......
+......201.........868.........*..976.........@........................582.122&.....365.....511...35....*..........................*...+.....
+.....*.................887.115..........149.....580*948......./651......................#............411...........................38.26....
+....882............................894.....*............381..........98............2.245......./..............789....49.....289.............
+..........................976*......#..$....207........*....507*.......*.135......*...........469...798./944...........*....*......237......
+.701....759...................99.......622......430...126.......275.384..*......94....254............+...........367..274..179.../....*.....
+..........#.........................77......15..*...........258.........179..........*......................498*.............../..657.196...
+.......45.................*...-......*...#.......339..195*.....*................891.332........+..667...........194.....201...670...........
+.........#...445.334...908.5...566..433...149.............331...998.......................171.90..*.....................*...................
+...................*............................../....28.....................704.........@........959.&.............504..........@..-......
+.................684................705...........476...............&..4.939...*....252.................26......519.......*.....994.855.....
+...572...$..699.........+.......942*.............................997..#..#...562......$...67...991.............@...........764..............
+...*....692.*........368...151.............847.....959.....................9.....................*........782........892.......188&.........
+.399........739.............*....208..........*......*...928....*...........*...........729.....721..........#.......*...............-......
+.........................577...............144....906..........864.........416...............45.........73........388.............689...11..
\ No newline at end of file
diff --git a/day 3/testdata.txt b/day 3/testdata.txt
new file mode 100644
index 0000000..624ea4f
--- /dev/null
+++ b/day 3/testdata.txt
@@ -0,0 +1,10 @@
+467..114..
+...*......
+..35..633.
+......#...
+617*......
+.....+.58.
+..592.....
+......755.
+...$.*....
+.664.598..
\ No newline at end of file