about summary refs log tree commit diff
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2023-12-05 19:18:00 -0600
committerStarfall <us@starfall.systems>2023-12-05 19:18:00 -0600
commit16fe43e6c7906961843c17149597ebc19fe42c4d (patch)
tree7d78ab521a6af1538fc18ea9c6360017398ae47c
parente01998ad87780da7dbbcde00b678a037bc8bf14b (diff)
aoc2023: day 3 solution
-rw-r--r--aoc2023/day3/Cargo.lock7
-rw-r--r--aoc2023/day3/Cargo.toml8
-rw-r--r--aoc2023/day3/input140
-rw-r--r--aoc2023/day3/src/main.rs386
4 files changed, 541 insertions, 0 deletions
diff --git a/aoc2023/day3/Cargo.lock b/aoc2023/day3/Cargo.lock
new file mode 100644
index 0000000..4104d70
--- /dev/null
+++ b/aoc2023/day3/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "day3"
+version = "0.1.0"
diff --git a/aoc2023/day3/Cargo.toml b/aoc2023/day3/Cargo.toml
new file mode 100644
index 0000000..898e70d
--- /dev/null
+++ b/aoc2023/day3/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "day3"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/aoc2023/day3/input b/aoc2023/day3/input
new file mode 100644
index 0000000..e70b8d2
--- /dev/null
+++ b/aoc2023/day3/input
@@ -0,0 +1,140 @@
+...........................775.651...............887....79...946...921...493.....942..942.....151....155....................................
+......240...................*.....-......................$..*...................*.......%.....+....................956.549.*290.......834...
+.485...+............437......906......%..608.805.725..72.....242....745..61......440................................*..*.........515...*....
+..........917.......&....146........790.....*......*....*..........*.....*...............207*......................796..116......../...924..
+722...323.-................./...410.............72..748.442............384.708...............849..%............................470..........
+.....*..........................*.....271..691....-...............4*...........388.................448......&........*....848&......751.....
+....370..$....639.748.......*...467....*.....*........921*909.....................*...32.....................165..452.30....................
+........984....*.....*...782........711.....50.172...............61&..........415.803.*.........524.......................203.......106*643.
+...............57..433.........390....................&../.............*122...........674.........*.........................................
+379..................../.........*........908..477...305.876..*..............297.415*......+.......94........../.....@378.......226..56*....
+...*..-......82.......167...936.17..958...=....*...............196......+......*.......-...380.992.............989.........29......*....301.
+..124..317.....*....................*..........839.......................191.882......231.........+......976.................+......469.....
+............643................*........778............541........655..............*............*........./............*.................515
+.....&............498#...&....726.774..............111....*378..................723.221.......473............752....523.638.......789...*...
+.....353................518..........-.........../....*...............*88.........................*..@280..=..-....................-..297...
+...........279...........................*816.....286.975.&319.....614......694.......799.......503.......861...569.............+...........
+..474........*.228..............308......................................................*............207........................838..408...
+...........226./....*................82.........................268/......814.........640.............../..489........719..=..........*.....
+....83............88.544....250.......=...87..............199.........115*....................................*...377.@.....643........658..
+........-...651.............*................&..............................*379.....411...686.397............202.*....../........217.......
+.....781.......*897......630................330.....117.961..........61..543.....#...*.....*.......323.............996.603.......*..........
+.......................................................*........473....*.....739.184.511..........+......43.833.................953...@.....
+....110......@471...........*464....@.347......779................/.....205.*.......................681.....@.........................694...
+.........................203......158.-....310./...449...726*982.............437.....+....259...................185......940*702............
+.....465....*.......747....................*..........*...............................238..&......./..963...755....*........................
+.....*....687..104..%.........645........959..497..732........988........733.....12...........417.176....$..........176.323....657..........
+...566........*...........938*......418......*............925....*28..............-.156........*................................*..#........
+.............933.......................*....988........../............................*........813.................868*674...375...839......
+.....................804.......464..452...................................47......324.789..........692*288..................................
+...959...........792....*10.....=......................*455.77...827.99....*.........................................166.......8...586......
+.....*...607......*..............................12.764......*.......*..905....590.236...#.677...823......972..204............*......*......
+..661....*........776....938..901.............@.*.........$...216...995...........*....382..*....#....546*.....#...........564........707...
+......755................*.......*698......315..291.......964................-.......=.....865........................521........150........
+........................323............./............973.........*35..........318.....204...........546..+667....214%..*..............973...
+........143......&725...........619....149..............*235..650.........604..........................*..............195..............*....
+................................*.................................926...................@240......+...433..302..672.........977....149..335.
+.......880.......&...............301.512.$.....876......331.897...#..............#...............464......+...........938.........*.........
+......@....942...603....&....879..........117..........*.....*.............562..905....377*862..............*56.........%........352..*165..
+.726......*..........239....*.....915..............@...661...905..............*...................466....223.................730............
+....*......97..839.......308.........*....115....512..........................497.392.....563........=...............448...........462*884..
+....122.........*...471*...........52...&..=...................=..................@.........*...............................531.............
+.........*..522...&.....274...........116......*572...........907..........................161..670@..@....................=........57*.....
+.........28.*.....606.......................925...............................908...86...............78..............712.....813*......16...
+....549.....512....................................825.....797.......224..660*........@.425*656.........=536..256.......*........599........
+...*.............963..................842..704.............+....425..*..............................102.......$....565.179..774/........576.
+...................*..........................*..............=...*....922........50...262....%............603......*........................
+.......636.......349...&...224...$............899.........983..319.............#........&...663....*575..%.......95.....383*................
+......=...............928........218................84.............964.........953..+...........212.........................857.............
+............................179.......68......................&....*.................787.............92&.72....455..969...........846..40...
+.993=..../..........110.407*...........%.............164..#....306.215...................552..............*....=...#.....69.602...#...*.....
+......322..........#...........561..............968..*....913............%..........@586...=.330...........166.............*........863.....
+............810...........157.*....557.....30.*.....774...................968.601*...........*....491..........557.....83*..................
+.......*368.*.......842...*...435.-...........65.-................................680.....913....#.......793....*.........87.262....34.-....
+....896......875.....*...........................581.957..758............$................................@..981..............#......*.844..
+..................957...150.......503................@....*......89....59......261..........653....................................372......
+...731...-129..+........*........-......................718.713....*.......197..*......30......+...476............872.......................
+....*...........671....917.......................800.&......=....197..763....*...96......#.........*......%......*..........739......290....
+.....234..............................814*251....../.755.............../...375.....................309.914..816...877..=.......$......$.....
+.........543.378.....74.%608.25...755..........237..............834..................431..................../..........585..................
+....408...&...*..............*................*.............214*.....................*.........89.475*.......................@..939..824....
+...*..........526..........713..&.....132...899..108.75..#..................521....428....97../.......603.....248.........878...&...*.......
+....383...............873......1....................*....850....862....%......#.=................533.........*.......................863....
+.............852.........*................578%....................*..492.........905.145.927........*........437.107.....@....29............
+......604.......#./531.179..&........56.........921..233.......943......................*..........174...........*.......271.*...32.........
+........*..619.............588......*............*...*..............918*837.....120.128......508...........536...............558...*638.....
+523....794...-....................451..544....648..365.778.....@.............@.....*............$..%414...*............453#.................
+..................&......124*...........*...............*...259........696...424...........*...............322..534................*519.....
+........201*.30...474........567.526..363............%.963......287+......*.........939.964.....994.............*.....-........287.......657
+........................709*......=.........854...527................50..591...859....%..........*....196.......188..395..157...*...........
+.281*....878................354.......*221...*............308....309...*.........$..@...72#......577.........................*...734.....481
+.....9....*.............658........235......767..12..548...=...#.......585..830....609........................603.130.....884...........*...
+........324........@...*........................*....*........628.............................................*...$...........=......658....
+..................139.206....$.....768*........986....311..........967...15...174....100-.....7$..............962....=.........561..........
+......521.....154.............318......622...................-.............%...@.................856.......60.........24..481.......520.772.
+.825..#.......*.............................180.............228.11.....173.......128......428.........405.+...68...........=..882......*....
+....*.......657...........27....798*460...................@............-..............31.*.....840...........*................@.............
+.965...931............974...*...........................798.................455*.........856......*...809...97..934.740$..570...............
+........*..511.$427....../..652..-297............296........$........552..........884..................*........%........*........128.......
+..........-..........207............................=....641.........+..........-..*....43..........329................990...........*......
+.10...................=..11....&............................./.143........875..732....&.......&............................349.....265...858
+...*......222....495%...*...618..322.......%.........116...833....*........*.........776....288....760.........-............-...............
+....291......*..........453.........*...811...398..............515........365..445....................*.....961.........433......743...$....
+.............33..................543.........@..........................=...../.....793*...........266..............668...........#.....804.
+................701....................17*.................449*741.....961......814..........................*245......*...809*.............
+.......206......*....668..728$............529..........*93........................-...833.713..............69.......292........157...224*...
+..........*585...969............810..522............866..............254.....570........*....*.....588..................+113.......-.....752
+................................/......../......257.............119...-..844...*..979..442....169.....=.259......+.............752..401.....
+...726...967........&.234.................326.....*....590*636...*............130.../.........................933.....652*439...*...........
+.....*......*12..332...-...........500#..........944..............747.......$.....................203....513.......................%864.....
+....389.&....................*...............938.......893....970.........864........538.755&.258..*........#...............717.96......+...
+.65.....180...&.............864.....169.........*................*...390............*.........*....904.........................*...354...478
+....620......472.726.931...............*.......150../.......@....960....................*928.812...........................428......*.......
+...*................*.................463..........323.....478..........964..........175.........324%........=827......66.......193..303....
+313.......................@...705.214.....174&........................-.....271........................818...............*.........+........
+........171+..830...328..158...*..%..................17....721.....568......../...........949.............*.............608.....90......570.
+....327..........*....*.......44......................*..................193-.......67.................970......966.503................#....
+669*..........386..306....+............105............443...........................*........490..130...........*....$..........$62.........
+......454...............&..954...38....*..........229....................643......388.671....@.......*...8......506....66*148.........$.....
+......*................259.........*.................*756.......596.........*........................638..+....................%....854.....
+...700......895..............................757................*.........847..............630*612..................854..606....38..........
+...............*.....93.....573......274........*...952..336...................562..................+...637............*.*..................
+945........../.814..=..........*....*...........38..=............-....&........*..................118......*.........745..855.........367...
+......676..688..................604..153...............671......513...690....891.%.....101.728..........767......28...................../...
+.........=.......700..222.443............114.....................................458...../....#...................*.........=...............
+...........660.%...&.....*.....98.232.......*............308.......650..267/..........$............................417.....806.....502......
+.......276*....500............@.............63.......621*.....520...-........913.....225.........620.597...709*632.............663*.........
+...................232...149............65.......................&........................228.....%.....-................467............800.
+..........118........+....*....@.........-.458...737.....-247.........425................/..................574......424*.........421..*....
+......158.-....*........564.599......128......*...#..........................419.............764*156.............................*......996.
+............577.563.....................*...777.......................508..............*827............................*465.....306.........
+.........................*..............848......996*......688....154...*.......%...780......51...410......357.148..856.....................
+...648.............835.713....*...609.......................&........*..885....494..........*..........661....*.............................
+...-.....477./693..*........938.....*./...........642..149......465.502......$............=.523.........#.......892................=824.....
+....................296...........44...707..........+.....*...................462......476........@...............*..........388............
+.....58...321..............127................................446...............................965....446........114.........*.............
+.........*........951...3....*.................623.263.............=.-....122..........................=....519*...........692.......%313...
+........943.......*......$....990.......795..../......*..135.....815.483....*..937*.............................634..............771........
+............460....757.....@......511/..*............564..-..............340.......109.206.......%785..148...21....................*....&...
+.47.........&...............102........265......555..................182......12*.........................*...%.................788..200....
+.../............=..................#82..........$.......207.............*863.....751.........251.....641.702.......141..640.................
+..........23..902.......174...............*.............=.....314...........................*..........*.....*667...........*...............
+.........*...............*.......*766..842.35.......243..........*......@..................57..435....501.285.............964.....*.........
+..165...100.@512.........292.......................%......79..@...726....341.511*.............*...................573..........602.74.......
+...*..................................596#...&...............769........................314.397..&31................%..855..................
+...6.....673....................898..........836.......827.......+.....-..............................133*766..........*..............290...
+..........*.........799........*.........629.......634........%.188.103.......594......464.799*295.............895...930.........-....$.....
+.....908..504.......*......603..800..936...*......*.........768..........973...*.......*...............904*16.*................358..........
+.....@............825..%......&.........*...963.294.74...........%........*....102...967...-343.................#..................94.698...
+.810...................968..............429...........*....950..313..482..397...................33.........@..297..198...114.......*...*....
+.....-............................554..................................-.......245.$...............991..282.........-...*....161..894..78...
+..705.....177.....*..872@.285........*.../813.........235....729..........*287..*...472.788.645........................144..................
+.............*..984.......*........722............496*..........*990....79......715........*............456...................812..470......
+............518......@.....950.........712..............981...........................238....579.533........373.466...615/......$.....*907..
+..716..907.......761..471..............*......../...........823..+............978*......=.....*.....#......&....*...........................
+....*...............*........990.331...46....871...........*.....447.723...@...............594...............801......................#757..
+...312...*.........211..........*.................90.860...970.........*...375...........*.....-....*779.........&..........................
+.......692..............602.............................*.............595.............841...166..327......-444....154.......................
+..............$....316..=.............+978.843........42....135/.+332.........454.........................................707.../......295..
+.........$....743..../.......-................+........................*990..................343......#.....*...............*....684........
+......651.....................644....................887.812........187.................783........749...928.291...........131...........293
diff --git a/aoc2023/day3/src/main.rs b/aoc2023/day3/src/main.rs
new file mode 100644
index 0000000..18a6736
--- /dev/null
+++ b/aoc2023/day3/src/main.rs
@@ -0,0 +1,386 @@
+/*
+--- 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?
+--- 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?
+
+--- Part Two ---
+
+The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the
+closest gondola, finally ready to ascend to the water source.
+
+You don't seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone
+labeled "help", so you pick it up and the engineer answers.
+
+Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a
+phone in one hand and waving with the other. You're going so slowly that you haven't even left the station. You exit
+the gondola.
+
+The missing part wasn't the only issue - one of the gears in the engine is wrong. A gear is any * symbol that is
+adjacent to exactly two part numbers. Its gear ratio is the result of multiplying those two numbers together.
+
+This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out
+which gear needs to be replaced.
+
+Consider the same engine schematic again:
+
+467..114..
+...*......
+..35..633.
+......#...
+617*......
+.....+.58.
+..592.....
+......755.
+...$.*....
+.664.598..
+
+In this schematic, there are two gears. The first is in the top left; it has part numbers 467 and 35, so its gear ratio
+is 16345. The second gear is in the lower right; its gear ratio is 451490. (The * adjacent to 617 is not a gear because
+it is only adjacent to one part number.) Adding up all of the gear ratios produces 467835.
+
+What is the sum of all of the gear ratios in your engine schematic?
+*/
+
+use std::fs;
+
+fn main() {
+    let input = fs::read("input").unwrap();
+    let w = input.iter().position(|&c| c == u8::try_from('\n').unwrap()).unwrap();
+    // let h = input.iter().filter(|&&c| c == u8::try_from('\n').unwrap()).count();
+    assert_eq!(w, 140);
+
+    let arr: Vec<char> = input
+        .iter()
+        .filter_map(|&c| {
+            return if c != u8::try_from('\n').unwrap() {
+                Some(c as char)
+            } else {
+                None
+            };
+        })
+        .collect();
+
+    let symbols: Vec<i32> = arr
+        .iter()
+        .enumerate()
+        .filter_map(|(i, &c)| {
+            return if !c.is_ascii_digit() && c != '.' {
+                Some(i as i32)
+            } else {
+                None
+            };
+        })
+        .collect();
+
+    // and now for the horribly inefficient part
+    let mut part_numbers: Vec<u32> = Vec::new();
+
+    let mut i= 0;
+    while i < arr.len() {
+        // look for a digit
+        if !arr.get(i).unwrap().is_ascii_digit() {
+            i += 1;
+            continue;
+        }
+
+        // gobble digits until EOL or nondigits
+        let mut j= 0;
+        let mut n = 0;
+        while i + j + 1 % w != 0 && i + j < arr.len()
+            && arr.get(i + j).unwrap().is_ascii_digit()
+        {
+            n *= 10;
+            n += arr.get(i + j).unwrap().to_digit(10).unwrap();
+            j += 1;
+        }
+
+        // check for symbol adjacency
+        let mut adj = false;
+        let left = i as i32 - 1;
+        let right = i as i32 + j as i32;
+        // current row
+        if symbols.contains(&left) { adj = true }
+        if symbols.contains(&right) { adj = true }
+        // previous row
+        if left > w as i32 {
+            for k in left - w as i32 .. right + 1 - w as i32 {
+                if symbols.contains(&k) { adj = true }
+            }
+        }
+        // next row
+        if (right + w as i32) < arr.len() as i32 {
+            for k in left + w as i32 .. right + 1 + w as i32 {
+                if symbols.contains(&k) { adj = true }
+            }
+        }
+
+        // wrap up
+        i += j;
+        if adj {
+            part_numbers.push(n);
+        }
+    }
+
+    let sum:u32 = part_numbers.iter().sum();
+    println!("Part one answer: {sum}");
+
+    // PART TWO
+    let mut n_gears = 0;
+    // in which we realize that searching the other direction was better
+    let sumproduct: u32 = arr.iter().enumerate()
+        .filter_map(|(i, &c)| {
+            return if gear(i, &arr, w).is_some() {
+                n_gears += 1;
+                gear_ratio(i, &arr, w)
+            } else {
+                None
+            };
+        })
+        .reduce(|acc, e| acc + e)
+        .unwrap();
+
+    println!("{n_gears} gears found");
+    println!("Part two answer: {sumproduct}");
+}
+
+fn gear(loc: usize, arr: &Vec<char>, width: usize) -> Option<i32> {
+    if arr.get(loc).unwrap() != &'*' { return None }
+
+    // valid gears come in an enumerable number of flavors:
+    // . is a non-digit character
+    // 1 is a digit
+    // ??? can be ..1, .11, 111, 11., 1.., .1., but never 1.1
+
+    let bitmasks = [
+    // 1.1.*....
+        0b101000000,
+    // ...1*1...
+        0b000101000,
+    // ....*.1.1
+        0b000000101,
+    // ???1*....
+        0b100100000,
+        0b010100000,
+        0b001100000,
+        0b110100000,
+        0b111100000,
+        0b011100000,
+    // ???.*1...
+        0b100001000,
+        0b010001000,
+        0b001001000,
+        0b110001000,
+        0b011001000,
+        0b111001000,
+    // ...1*.???
+        0b000100100,
+        0b000100010,
+        0b000100001,
+        0b000100110,
+        0b000100011,
+        0b000100111,
+    // ....*1???
+        0b000001100,
+        0b000001010,
+        0b000001001,
+        0b000001110,
+        0b000001011,
+        0b000001111,
+    // ???.*.???
+        0b100000100,
+        0b010000100,
+        0b001000100,
+        0b110000100,
+        0b011000100,
+        0b111000100,
+
+        0b100000010,
+        0b010000010,
+        0b001000010,
+        0b110000010,
+        0b011000010,
+        0b111000010,
+
+        0b100000001,
+        0b010000001,
+        0b001000001,
+        0b110000001,
+        0b011000001,
+        0b111000001,
+
+        0b100000110,
+        0b010000110,
+        0b001000110,
+        0b110000110,
+        0b011000110,
+        0b111000110,
+
+        0b100000011,
+        0b010000011,
+        0b001000011,
+        0b110000011,
+        0b011000011,
+        0b111000011,
+
+        0b100000111,
+        0b010000111,
+        0b001000111,
+        0b110000111,
+        0b011000111,
+        0b111000111,
+
+    ];
+
+    let mut neighborhood:i32 = 0;
+
+    if arr.get(loc - width - 1).ok_or('.').unwrap().is_ascii_digit() {
+        neighborhood |= 0b100000000;
+    }
+    if arr.get(loc - width    ).ok_or('.').unwrap().is_ascii_digit() {
+        neighborhood |= 0b010000000;
+    }
+    if arr.get(loc - width + 1).ok_or('.').unwrap().is_ascii_digit() {
+        neighborhood |= 0b001000000;
+    }
+    if arr.get(loc         - 1).ok_or('.').unwrap().is_ascii_digit() {
+        neighborhood |= 0b000100000;
+    }
+    if arr.get(loc         + 1).ok_or('.').unwrap().is_ascii_digit() {
+        neighborhood |= 0b000001000;
+    }
+    if arr.get(loc + width - 1).ok_or('.').unwrap().is_ascii_digit() {
+        neighborhood |= 0b000000100;
+    }
+    if arr.get(loc + width    ).ok_or('.').unwrap().is_ascii_digit() {
+        neighborhood |= 0b000000010;
+    }
+    if arr.get(loc + width + 1).ok_or('.').unwrap().is_ascii_digit() {
+        neighborhood |= 0b000000001;
+    }
+
+    bitmasks.contains(&neighborhood).then(|| neighborhood)
+}
+
+fn read_number(loc: usize, arr: &Vec<char>, width: usize) -> Option<u32> {
+    if !arr.get(loc).unwrap().is_ascii_digit() { return None }
+
+    // scan left to start of number
+    let mut i = loc;
+    while i % width != 0 && arr.get(i-1).unwrap().is_ascii_digit() {
+        i -= 1;
+    }
+
+    // read right to complete the number
+    let mut n = 0;
+    while i+1 % width != 0 && arr.get(i).unwrap().is_ascii_digit() {
+        n *= 10;
+        n += arr.get(i).unwrap().to_digit(10).unwrap();
+        i += 1;
+    }
+
+    assert!(n < 1000);
+
+    (n != 0).then(|| n)
+}
+
+fn gear_ratio(loc: usize, arr: &Vec<char>, width: usize) -> Option<u32> {
+    let neighborhood = gear(loc, arr, width)?;
+
+    return if neighborhood & 0b111000000 == 0b101000000 {
+        // special case: two numbers on top
+        let x = read_number(loc - width - 1, arr, width).unwrap();
+        let y = read_number(loc - width + 1, arr, width).unwrap();
+        // println!("{x} {y}");
+        Some(x * y)
+    } else if neighborhood & 0b000000111 == 0b000000101 {
+        // special case: two numbers on bottom
+        let x = read_number(loc + width - 1, arr, width).unwrap();
+        let y = read_number(loc + width + 1, arr, width).unwrap();
+        // println!("{x} {y}");
+        Some(x * y)
+    } else {
+        let top =
+                read_number(loc - width - 1, arr, width)
+            .or(read_number(loc - width    , arr, width))
+            .or(read_number(loc - width + 1, arr, width))
+            .or(Some(1))
+            .unwrap();
+
+        let left = read_number(loc - 1, arr, width).or(Some(1)).unwrap();
+
+        let right = read_number(loc + 1, arr, width).or(Some(1)).unwrap();
+
+        let bottom =
+                read_number(loc + width - 1, arr, width)
+            .or(read_number(loc + width    , arr, width))
+            .or(read_number(loc + width + 1, arr, width))
+            .or(Some(1))
+            .unwrap();
+
+        // println!("{top} {left} {right} {bottom}");
+        Some(top * left * right * bottom)
+    }
+}
\ No newline at end of file