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

Day 3: Gear Ratios

day-03.livemd

Day 3: Gear Ratios

Mix.install([
  {:smaoc, git: "https://github.com/nettinho/smaoc"}
])

Part 1

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 2

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?

Solution

defmodule Smaoc.Solution do
  def solve(:part1, input) do
    lines = input |> parse_lines() |> Enum.with_index()

    symbols = symbol_coords(lines)
    numbers = number_coords(lines)

    Enum.reduce(numbers, 0, fn {npos, nlen, num}, sum ->
      if Enum.any?(symbols, fn {spos, _sym} -> adjacent?(spos, npos, nlen) end) do
        sum + num
      else
        sum
      end
    end)
  end

  def solve(:part2, input) do
    lines = input |> parse_lines() |> Enum.with_index()

    symbols = symbol_coords(lines)
    numbers = number_coords(lines)

    Enum.reduce(symbols, 0, fn
      {spos, "*"}, sum ->
        adj_numbers =
          Enum.filter(numbers, fn {npos, nlen, _num} ->
            adjacent?(spos, npos, nlen)
          end)

        case adj_numbers do
          [{_, _, num1}, {_, _, num2}] -> sum + num1 * num2
          _ -> sum
        end

      {_spos, _sym}, sum ->
        sum
    end)
  end

  defp parse_lines(input) do
    input |> String.split("\n") |> Enum.reject(&(&1 == ""))
  end

  def adjacent?({sr, sc}, {nr, nc}, nlen) do
    (sr - nr) in [-1, 0, 1] and sc >= nc - 1 and sc <= nc + nlen
  end

  def symbol_coords(lines) do
    for {line, sr} <- lines, [{sc, 1}] <- Regex.scan(~r/[^\.\w]/, line, return: :index) do
      {{sr, sc}, String.at(line, sc)}
    end
  end

  def number_coords(lines) do
    for {line, nr} <- lines, [{nc, nlen}] <- Regex.scan(~r/\d+/, line, return: :index) do
      num = line |> String.slice(nc, nlen) |> String.to_integer()
      {{nr, nc}, nlen, num}
    end
  end
end

year = "2023"
day = "3"
run_config = ["example", "puzzle", "part2"]

inputs = %{
  "puzzle" =>
    ".........798...145.........629....579.....455.....................130.............243.................154........167........................\n............*.....*...........*...&...179.*........737...194.........*854........./...........52..560*............................699...&...\n........459..489.817........880.........*..996........*....*........................................................................*.36....\n...........@.........................813............234.552..307....184............370..................736.....960..............631........\n...100...................*...............131..................*........=......435..*......34...........*....................................\n......+....#126.......214..........$......*.....+.............939................/..729..............861.....243..........438...............\n...................................854....979....177.......%.........280..138................158*241............*..........*....*904...#....\n.......427............................................683&.726...303........*.......905.......................&..115.....412.479.....491....\n.....=.*...........989......888.....................$............../.226....922.172....*..702*693...........543.............................\n..924..219...........*.....*........979............191.%.........+..............*.....323....................................777............\n....................109........................559.....835.....708........989..685................................#.............=.915.......\n........................................*........=.........751........886................243.......154+..922...347................/.........\n......923...............571.....@.672....963.471.......154...*.........*.....................914...........*.............891.........73*212.\n.........*......654+....&....309...=.........*....543.........583....871.....*........................551...370...$......*...492............\n..618*....60.........................426...&.645....*....................203..310....$...........@166...........477...@.......%......150*...\n......353......381..........416.....*....19......536......#..312..../790.*............779..$.........../............985..................761\n...........#............636..*....238...................576.....*........899................460.841..829.415...271*.......168.77....=.......\n..........427............$.......................................75..............789...942........-......*.........795......*.......73......\n....863................%.......920..........*152.....852*387.........990.....563..*.....*...629........772....912............283............\n837*.......897.......674...966....&.%621..77..................................&..571...272.........566.....16*.....936.....&......980.......\n..............*............*....-.........................*..............424#...................53.*......................203...............\n...826.781.....105......&...771.982............967......40.223.....................71............*..123.........-...@....................998\n...$................64.562.............../382....*.................866-....276.....*........750.975...........878..156..........188.........\n...........38+..............937...................142.........*712........$.....619..*734............424..................=.................\n....746...................@....+..868..-......881...........................+.........................*.......832.../....649...-.......181..\n...*.......+637...478....628......%.....651....................../.....399%..396.............&..293.472..........*.960..........988....*....\n....693..........$..................-.......711.99.489.878....541...........................565..#.........32..710......499............888..\n....................681.59.442.....368.%.........*.......&..........................974............*........%...............................\n677....924......797.......*............964......666.................89......309.......*.........324.179..........437.......235.321...678....\n............+..........$......5...............$......./......23..............$.........328..564.........392......*............*........*....\n...298......811.........976..*.................400...140....*.......893................................*....%.682.....345.............726...\n..............................570.839......................828.....*......-......59....373.......%...558..271...........*......*751.........\n.....570...........353*674........#....636........659..............647.....226..*.......+......684...............139...290..401........290..\n........&..................231...............80..........@112....................892......605...............247...+................$...*....\n165........*329..94*254..........115...............938.................192...............*......$58.............@......786.....328..59..285.\n.........98........................$.....895......*...............459..*...............399..641..................285...$..........*.........\n.............../..455*429............794*.....................512.*....770.....448...........@...................................336.175....\n......#......277................................763............*..254.....................*....143...495..$.......676...................#...\n.......272.........-..................711..911...*.....614..516............747.389.....803........%.....*.832.......*....905....576..#......\n625.................595......989...............291.....*................*.....*............$..201.............528.722....%.....*......896...\n....342............................................569..486.......@..278.184.........259..107................%.................749..........\n...#..................851.......16..436*910.......@..............338...........989...*..........+.367.867............=..918.................\n.................106..#.........@...........................@.....................-...99......38....%....*.18&....550..*..........250.......\n..................+.........................&732....53....258....850..................................306.............539....@..............\n658*869..............684*636....422.686............*...............*...955..............*......................../...........313.....$......\n.............604.............................954....357.........458..................168.310.......698............435......%......394.......\n.....109....*.........163...........962$.....@..........=..................876*........................230..125...........24..972.....641...\n......*...312..........#........................*914....740....466*............374.............+......%....*...........46....*.....60.......\n...422............804......$.@......%........589...................500..958.............21..301...........889............*..717.............\n..........474..=.....*...302.475.155.............665..800..46..............=...................................19......902........936...887.\n............*.754...831...............8@.@............................110.....554..........732...=913.........*.....$............*..........\n456......211.................562.........183........................@......+..*.....839...............@...500.739..962.....105...528..-.....\n...............794.882.......+.....@..........833............595....704.693......=...*..............81...*.............571*............727..\n......590.30......*.................872...@...................................773.....156......&346......807.813.285+.........380...........\n........$..*...........*621.....528.....835.....556.......*85...516-.......................28.........*......-..........791$.....%..........\n....................965.....507.*.............*....*...377.............957................@....835.707.409.....987*...........=.......729...\n.....503......471..........*....267..169....200.369..........499..#...*....................................161.....464.....794.....*...#....\n........$.......*..........808......*....&..........643.....*....39....354......159........117@...308....-...*.........144.........452......\n..........$......910...........195..952..320.........*......832.................*...296.............*..860..401.........%....157/...........\n.......438..721................@..............89..144..332........987.........979..*.....%.........824............709................710....\n...209.......-........../............=952..............*.............*292.........582.661.....$........815.............857....254....*......\n....&..218%..........100................................74....................*................692.-.......545...................*..........\n.............297..........101....276.756..............#....462......492*578..636......=............860..........................399.........\n........171..............=........#..*...............407...%.......................331..915..............................919................\n....454*...................*83......100..702..............................271...........@...........59....582.../........*...........+......\n..............185*......687...........................186............739-...=......*.................+....*.....380..539..192.80......455...\n.......174........686.........562...958......192.......*..117...108.............567.298.................545...........*...........$.........\n.....$.....887........131..........-....955...*.....504...........%.243..281........................877.............610......216..130...-...\n.....646....*......76....................%..50.......................*.......541.......782.....752+....*......296*.......523...@......881...\n............802....*........976.................223*478........96...785.142.*......530....*721.........577........317.......*.....295.......\n....412.114........561...#....+............................52....*......*....999......*./....................................221.....*40....\n......*./......*.........435..........168..779....-..........*.272.....372.........652...489........*988.......810..........................\n80.110.......137....938...........924....#.+...896........305....................................885...........................831..........\n........&...............792.........................................773*100.958..&78.....*..............902....286................+.........\n........873..............&...........585..................127*899..............*.......60.982...........$......@....268..370.............567\n............781...*...........*174...*...............................26........203.652................................*............538......\n.............$.....534.....848......170......418........494/....298...*....792......./.......%......+687..............179............@.160..\n..474...711....................417.................................@.458....$..........*338..719............509..488......781.982...........\n.....*..........235.................622............$...804+..............*......577.870.............&..43.....&..-.............*.....278....\n....696.222.604.*.......*29...........$.....&....755...........55.....631.202.....................443....*....................692...*.......\n...........*....55...456.....782..422.......133......844........*..........................286-...........865.89..27...718........317.......\n.......611.....................-...*..995........712..*..........89...............@166..............771............*.....*..............589.\n...........417.....150..678...........*.............$.417..298.............611&..........811.327......*..$...610%.614.....443......-...*....\n.......658*..........-...*.......622..940.............................&....................*../..773.194.479..........996.........263..872..\n....90......%550.587......215.......*.......322.....229......841*....996...........826....305......@.........#405.....$.....................\n......*...........*..................671....*......*.............695.......694....+..............................................223.462.826\n..419.832.......271...828................228.................380........21*..........644............725....703......255..330........*.......\n.....................*.............................357.................................*........360...+.....#..%173...*..../../.............\n..................182...........88.664*......865..*................................428.................................143...829.552.....829\n...&..................425..894...*.....792...*.....321..219................=...564....*..166....%....+.....764...................*......=...\n..619.....529..384.....*......*...693.........188......-............281.238......=.339....*...124...278.....+....490.....481....416.........\n.................*.442..76....801.....376.................+511.......*......*.............967.......................*.68*............919....\n...731..484....875..#....................*243...681.................135..191.162.688%.........29.....*............688..........437......*...\n.....%.=........................329*87.............*138.-949..186.................................595.554...............755....*.....454....\n............341..302.........&...........330.....................@.......................779..............353......@.......*..535...........\n.....766.....*..*..........315......856.*................540..............136............*.......914..162*.......7..336..383............642.\n......../..253...502................*...990.....299.......+...413............*..........338.......%.............................294*........\n.....&..................636.986.431.345...............693.......*.......*...............................276..........836@.278.......716..469\n.....247........*.905*....$..*...*.......................$.....502...177.........72=......$...232......*................................*...\n.........674.905......664...124............*127.............................564........924.....*.....&.616...661..403....10.........429.724.\n........../..............................78..............#5..................*................498..650..........*.......*............*......\n.....348....*.........488...........414..........................962...552...107...@...533......................109....432....*......423....\n....*.......536................283....&......800.............&.......+..../........677.$..................32...............410.392..........\n....826.............755........*........396..*......../.......771.....837........=.............577.................53...............314.....\n.........@....109$...*..........988........$..241..750.............@............169...162......*...%....575.743......*..................278.\n..........862........227....585........158.............262.....51..151..................*..509...31............*..........+....144.481.&....\n........*........467........*.........#..../..........*......................%....973..218..*..........@.......543......678.......*.........\n......32.106...$...*......260...........425.........912.................383..392.*...........503....546..212........%......./442.....221....\n..............668...593............422.......966*37................503..*.........776.....5$..............*......833.................&......\n............-...................21....*.-497............................133...........862......241.............-..........824...............\n.........215.......................851.........-........932........@./.......-.......-....%..................597..........*........@........\n................*323....557....782..........964...14..$....$.....631.144.....588.........316......................252..396........257...957.\n.............367........*......*.....64.97........=..976.....245................../982..........135...169........*.....................=....\n....513..........96.101..359.212.585..............................965*287.803.501.........460.........*.........618....98.208...............\n.............802*...+............#..............804..123...579*..............*.............*.......282.....263...........*..................\n.707....522....................................*........@......106..143.........716......770...........576*......................497........\n......#.../...=.........835............545*610.462..188............*.............*.....$...............................*115......*....*.....\n928%.606.......207.......*......61..........................797.....228..357....484..492..........247...............574..........880.994....\n...........541...........444......@....122......................505......+...14..........534........*.....210...........150.948..........210\n..............*..676*765...............*.....295.156...601#....*......-.......*.....709..........901.....*........714......*............&...\n.......723...299................325...965...........*.........656......156....824...../..............840.383...........249..........=.......\n..................................#........26*781.456................#.....................745......*.............165.*..........278..452...\n.746..............953.943.............................877.....59......128............#....+......585......641....%.....229...*2........*....\n...*.......614=......*.......903.............433.370...=...54....+..............&.....336.....$.............*..........................856..\n698...272.....................%......545.....-....$.............223.183..........339........189............994.......531.815*206............\n........./................657...........#...............571............=..-832.+....................876..............................754....\n.....&.....*960....628.............597.............747$.*......776..+..........709..................*..........266..........225.....*.......\n...541..511.............353.......*......................649.....*..753...719......212..526=...578..634..........*..........*......113......\n................996=...*...........18........#628..............321........*.......*...........*...................194....334.............126\n......................627..693.........248..........290.................@..204..=..247...574..311......791....................679*..........\n.......@....................=......283*............*.............38..214........90.........*.......447...+.............77...%.....265.......\n......566......&.................*..........965...929...........*......................869.908.753..$.......437....848*.....943.............\n...........403.950..............439............*............340.419...........868.....*...........*...128.....&.....................869.....\n............*..........637...............256.793........436*....................*.....85.361.....280..................349...................\n.........654...........*..............=.-........949............=....224..506.631....................*21.@...........%......................\n.............#..44.......105.108...190..........*....$173......750....*...../.........37.......-..461.....727...75.......................893\n......@...583.....*........*................358...........750........................*......532..................*...22...../....512...#....\n863...112................178...+...........*...........5./............98......584..222..........862...235.....448...*.....737.....*.....516.\n....#.......425..............923.84*......947......999*..............*....280*...........732...&.....*.............14.............683.......\n.....353............................914........105.................829...........112...............75.......................................\n",
  "example" =>
    "467..114..\n...*......\n..35..633.\n......#...\n617*......\n.....+.58.\n..592.....\n......755.\n...$.*....\n.664.598.."
}

response =
  for part <- ["part1", "part2"], input_key <- ["puzzle", "example"] do
    {part, input_key}
  end

response =
  response
  |> Enum.filter(fn {part, input_key} ->
    part in run_config and input_key in run_config
  end)
  |> Enum.map(fn {part, input_key} ->
    [part, input_key, Smaoc.Solution.solve(String.to_atom(part), inputs[input_key])]
  end)

Smaoc.Response.new(%{response: response, run_config: run_config, year: year, day: day})