Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 3

advent_of_code/2023/day-03.livemd

Day 3

Day 3: Gear Ratios

Day 3: Gear Ratios

Input

input = """
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
"""

Part 1

input = """
...........................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
"""
[head | tail] =
  input
  |> String.split("\n", trim: true)
  |> Enum.map(&String.to_charlist/1)

x_count = Enum.count(head)
y_count = Enum.count(tail) + 1
list_of_lists = [head | tail]

map =
  for y <- 0..(y_count - 1), x <- 0..(x_count - 1), into: %{} do
    {{x, y}, Enum.at(list_of_lists, y) |> Enum.at(x)}
  end

f_is_number = fn x -> x in ?0..?9 end
f_is_symbol = fn x -> !f_is_number.(x) and x != ?. end

f_is_adjacent_to_a_symbol = fn {x, y} ->
  Map.get(map, {x, y - 1}, ?0) |> f_is_symbol.() or
    Map.get(map, {x + 1, y - 1}, ?0) |> f_is_symbol.() or
    Map.get(map, {x + 1, y}, ?0) |> f_is_symbol.() or
    Map.get(map, {x + 1, y + 1}, ?0) |> f_is_symbol.() or
    Map.get(map, {x, y + 1}, ?0) |> f_is_symbol.() or
    Map.get(map, {x - 1, y + 1}, ?0) |> f_is_symbol.() or
    Map.get(map, {x - 1, y}, ?0) |> f_is_symbol.() or
    Map.get(map, {x - 1, y - 1}, ?0) |> f_is_symbol.()
end

0..(y_count - 1)
|> Enum.reduce([], fn y, acc ->
  0..(x_count - 1)
  |> Enum.reduce({[], [], false}, fn x, {acc, tmp, adjacent} ->
    num = Map.get(map, {x, y})

    if x == x_count - 1 do
      if f_is_number.(num) do
        if adjacent do
          new_num = List.insert_at(tmp, -1, num - ?0) |> Enum.join() |> String.to_integer()
          {[new_num | acc], [], false}
        else
          adjacent = f_is_adjacent_to_a_symbol.({x, y})

          if adjacent do
            new_num = List.insert_at(tmp, -1, num - ?0) |> Enum.join() |> String.to_integer()
            {[new_num | acc], [], false}
          else
            {acc, [], false}
          end
        end
      else
        if adjacent do
          new_num = tmp |> Enum.join() |> String.to_integer()
          {[new_num | acc], [], false}
        else
          {acc, [], false}
        end
      end
    else
      if f_is_number.(num) do
        if adjacent do
          {acc, List.insert_at(tmp, -1, num - ?0), adjacent}
        else
          {acc, List.insert_at(tmp, -1, num - ?0), f_is_adjacent_to_a_symbol.({x, y})}
        end
      else
        if adjacent do
          new_num = tmp |> Enum.join() |> String.to_integer()
          {[new_num | acc], [], false}
        else
          {acc, [], false}
        end
      end
    end
  end)
  |> elem(0)
  |> Kernel.++(acc)
end)
|> Enum.sum()
f_is_adjacent_to_a_symbol.({10, 1})

Part 2