diff --git a/2023/day03/input b/2023/day03/input new file mode 100644 index 0000000..15cb53f --- /dev/null +++ b/2023/day03/input @@ -0,0 +1,140 @@ +....401.............425.......323......791......697...............963............................................420........................ +...*..................................%......#.....*....290.........................492.............656...@953.....................+830..... +..159...........823...33.717.....572.......806...896......-.....335....834......815.............791....*..............776................... +.........-.....#........*.........*..................715..........*.....*........*.....................5...*.....................688........ +....=..=..573..............212.......553.632....622....*.......260....29.........692.129....727...........290.........%32.....%...*..441.... +.791..998..............................*............531.......................84.......*..../.......506%......=............240..831..*...... +.....................321.586........447...&.-276.21......@..........12.......&.......-..333...................619..94................982.... +..........@...........#..*..............525.......*...183............*............407.......23.........%............&......846.............. +....97..686.............89.....................-..494..............875....*607.............*.......239.407..................%.......529..... +.....+.........815..703............208.=555.856...........@............133........................*..........672...501........898#...*...... +...............*.......*...659.......*....................183..............528.......614.......4....................................39...... +...602..........804..313....*.......480..............602.............354......*877................832@.....140...$...........707.......222.. +...*....999.................23.264..............516.....*.......-....*....................................*.....722......24....*........*... +436....+................./......./..........765*......37.....843....218..........852*68....290............410...........*.....539....896.... +.........../...#........907............587...............&................149...............*.......*..............#780..707................ +........349....603.598....................-....203.....178.......431......./...............152......571....926.750.............127.......... +.............@........*176.283...................*...%......642......../......20..............................*.........@.............534... +...749..../.194..............*.......337.395*....682.429.=...#..........186.....*.........682..../......498..........%.8.............*...... +....*..324..........893*...878......*........659..........65....671.189.......641...........+.859.........$.......149....648........53.+.... +...776......$.....................667.................432...........*..............697............282.901...................*889........471. +............103............../........-..................-...748&.257.............&...........739*....*......491*543................997..... +.........2.........813*.....278..316.333.....296.142.+...................673.%....../.....+.........490...................790........*...... +......@.*.......................*...............*....643.464............*....25....108..807..179.........931.......289..........365...522... +....367.250.927../.............754.........14...............*.+..........23...................*......&.....*..%..........323................ +................939...644..............791..*.551....35...937..806...............&.............536.930...703...565......*...............*640 +534$.................*.........$..448.....=......*...................707*658.....33....920..74.....................844................36.... +.......572....672...875......770.+................380.............52......................*.......-....251............%........*909......677 +.......*.......=........................658.889.........882.....=......................534..*..291...%....*.................289............. +.212......993..............438./..........*....@..532......*586.163....213...307...........167.....981.700.................................. +....*.....*..................-..12...611...196.......*309.....................+.......*685............................403....243..+385...... +59...428..14...22.747....990............*........575...............................247.....985....&..................*.......*.............. +.................*........%...539.......416.712.....*2..408.......%....................999.......275......738.....715......670.............. +..357........653..............*....374..........285................785.........384......*.............277*........................765.137... +...@..........*..@632......361.......=..............35.........329..............................=858..............798.................*..... +......................216*.......459..................*..........&........479.....255......302......................*.530.........269..453.. +............./.573........153.....*........#.......462...%..........45......+.122*............*672....545........157.....*862.....*......... +820....%...977.+...................726...790.199.........631....308..................755..............*...460..................643....#237.. +.......229.............727..425............../..........................228........#..$...............385....&...........695................ +...................978.*....*....................700....*.........256..@.........625......311.170...+................642*................... +.492.....983.........-.577.743..................*....267.521........%....479.......................696.980......................*........... +....*820.#....................................236.................*......*...856.............137...........489......409........177..450..... +974.........479.$......182..456......./.............*755..617..333.849.........*...............*..........*..........=...-...........*...... +...+..........*..83.#....@.=...........893.......610.........*.................727....6*..488.839...449..541../97......560.........449...... +............208......168......971..........................547..........559............................=...........237...................641 +.......336...................*....563..993.911.=......853................*...............391...@572.................*.......*......614..*... +........$...........579.....810....-......*....480....*......649.138..410..201..604........*..............450#.....485...124.841..*......790 +..399...........928.............67..................#..295......*............*...#....333...83...#111.............................635....... +...=........./.....*..807...529..*......642#......603......704.........@....599......&.................=.................................... +..............233.618..../........114........566......100..*.....423.26...............................168................555$..........#625. +.....608*372..................543..........*.*............4.....*.......+374.......436*...................680.135....................*...... +.................................@...385.309..111......69....991..127........693#......985....25..856*96.+.......*860..........%..278.160... +.......351.......495=.772..............*...............*...........*...........................@............569...............905........... +........................*.$402........640.*.............366.........350...623&...250.............446*341.....=.........................292.. +....#..*...595.289...347..........75.......844......307..........................*....585.....*..................766+...................*... +.148..906.....*..........232.449...*...........................................326..$...-..102.595.....406............-...........650....682 +..........619...23........$...*.....138.893.......890...79....53.........118........966................+..............233............*...... +.........*......../..........62..........*........*....*.............@..*....................245...........-....806.........98........318... +....534+.264..993...420.................675..77..659..263..........615.405...............=..*......%..75...614.*..................4......... +...............*..............421.........................834*287.......................813..651.902...*........134.....$...........334..... +....631....=..443........................352..........941...............317.......*286...............562.855............690....387.....*.... +.27...$.430......................89....-....*90...&......%..........155.......899.......%59....*986.................775..............25..... +...*....................-.....+...*...952.........62.........478...........&...../..........694.........775.....640..$...................... +....102..........+197....109.270.453.....................................641.......123..........573....*.......=............880.403+........ +.........87.186..........................709.+219........224.726...882.............*...868..203*....732............928*607..*............... +..819....*..*.............*....81........*....................-../..%...+..........700.=.......................149............*....438..=... +....#.875..223.106.........646......%.....519...579.............38.....883.626@....................149..........=..........205.272./....480. +.......................965.........332.........*......221.292...........................594..945......*931..365....814*214.................. +..228*....29............*....79...........+.....811.....*..*..592..........681.............*....#.427........*................170..58....... +......563...*........817..93*...+60..507...898........796.942...*..=335.......@...490....488.......-......974....817...........*....+....... +...........519..&...................*..........948.............454......184.........*.........=.......................409......947.......... +...............272..........257....712............+.@.....338..............*381...824..923..756.............971*181....=....+......430...... +....228.............709.......&......................685.....*..597....135......+.....*..........+......471..................452..*......923 +....*..................#...........271.....................167.*.........*.......325.935..........224......*.....749&...-..........587...... +..464.../199..$..........50...........*..794..........36.......733....................................@....414...........605................ +...............765.%.......=.215*717........*715....................210......845.......................643........66...........*908......... +.........215........761............................544....781...390*.....576..*....869..781....517................*.....356.664.......=..... +..............316...........235/...............715*.....-....*.............*..130..........*..@..........&.........564.*...............89... +...................................&....169...........353....390....57.71.847...........716.........466..516..651......447.................. +.......459..................101.379.....*......324...................#.#............943..............*...........%.........$625.../...586... +........*.....173...................198..306...*....313........51............629...*..........-958..228.178.......................967....... +......714...@.........206.......934*.........109...........*.........../......*....832...................*........934.....921..........897.. +...........487............700......................*507..415....349%...179...961................437.......489.......*............../...*.... +.................*440.952*................667...533.....................................359.618....*927.........................968...412... +...........-..778..................667.....@..........764..401.........279.......#.748.....*................534*.....444.................... +...835..779......../.....443.........*.........................53/......=......774......=....784...204..385.....929..........+...861........ +......%..........74..947*.....187&.874..........&.........................545........752.......$....$...*..................308.....*..284... +........#.................190...........445$..784...51*226...................*............*245..........435.......=...200..........88.*..... +640.....310...&...........*......296..........................................107......943..........244...........662......=..440+....302... +...*..........67........381..............856....349...........790*...911........................901...*......733........844................. +.327..............885........169.....419........*.....62..........70..@..664...........=...422%...*....617.....*....................230*.... +...........916...@.............................95....@.....+.........................355.........104...........149......................569. +.............*.........................230.../..............350..............................432.....710............=.........=............. +..925..545..634.....@..297*933...........*....839...33...........34.....162.......577..248.....#...........814...524..........790......9/... +...*...............66....................421............606+..*../.......&.........*....&..129..............=......................529...... +659..........253...........$504.......*......................379....&........./..860......+..............&...............%.....658*....310.. +........208...*........................354..........................647......326...............911.....503.......11.......293............... +....*.....&.617..511.........885.906......................166/......................589..........*..........749....*931..........915........ +.852.908........................*................../........................297.470*........#628..269...613......#................*......... +.........................716.................689...460...737*502...510.............../...........................778.......373..734.....+... +..520=.........../.....*....*226........326.....+.................+............971...362........800$..904.303.........*874..........=..548.. +............554.889.720.854......808.....*..258............284*........639/...*...........................*........603.............839...... +...........*........................*690.71..*.........314.....795.86.........53.................786%...134...................890*.......... +663.....225...%........513...................951..........*........*...............275.995...764.....................=............30........ +...*...........353....*...........%......................468.....87..225........31..=.....%.=......../.......886.....84..................... +..694......166........243........380...275..287..............261.....@.....888....*............162...278.......&..........351.534........197 +........$.......209.-......261...........*.......876.....15.....*.........*........712.....870...*.......897.....46...........#............. +......450.........%..463.....%.679....119.....=.*....@...@.........871...408..995*...........*...922.+..........*...66.466.................. +..............803.........................$.250...493...............&.............356.519.552........737.......153....*.............+....... +......982........*.....................540...................641.......................*......&....................................851../... +..............638..............=.306....................+938..@................*520....511.174......*..........-......*503...832.........219 +.292..239...................775..*......334..../582...................173...859..................238.678......117..429.......%...708.534.... +........*........240....491.....546...................+................*.............%....498.............................55........*.....74 +.816.....406.......*...&..............181...........765.......776....+..636...&337...264..*............*716..$908...........*............... +...................860....................980...................$..323....................163.......851............*.......396......501..... +...-..20*197................722..........*...............................&.....408..............109.......794*155..590........./....$....... +.482...........257..........*............999./850.........482..........933........*.*982...../.....*..........................826.......908. +.......%597......@..345.329..172.....295...........349....*.....=527........174.727........645...167......@..........*...................... +.....................*.....+..........$.............*....725...............*............$............+.447..723...346.759............967.... +............638..............................589.....................................=..312..+....951..........=..........809*..........#... +.............@............325..........322.........&......................508......359......768........8..&.........992..................... +.783.32...................*............*.........656.............98*178.....%.....................74*.....2...410&....@............+........ +...*...*..54*525...112..720...66......641.+280.......................................................467.......................580..348..... +..699.673......................................491..869............#......171..............98............473......%..............$.......... +........................%.....214.......303$......#..%.......191.852..560*.......835..550...$.150.........#........291.......240............ +.......579...982.........713....*...536.......................+...................*....#........$..840.......43................*............ +......$.........*....*.......606......+....48...........993.......838*744.880....969........@........*......*....*726..........168...&...... +...............395.579............340......*........386.=...420..............*........833..835........257...842....................810...635 +......#......................%...........983...632.@........*........-...%..157...193*...........................@..137....209.............. +.....819.....................278..377........................578...195..230........................$......586.985...............263.417..... +................592....../............*......../.............................922.......350........138.156*............698...78*.........$571 +....................405/..573.764....257.@940...34.977$..441....304.............*.....*................................*.......93..600...... +...643....675...262..........................................................993.....556...........314........278.258.322............*...... +...*.........*.....&................/.....................222.........959................@..121.......*...930.......#.......170*462.....588. +384....+..19.863..............407=.283.........972...340....#....427*....*737...........558...%.....91......*...........................=... +.....812...@.........298..563....................*......*............506..........845......................634...&............624+...&...... +.....................*.....=.....*.............492.563...950..827*...............+....................968......652..................49...... +.730.........498..382.........399.......824.........&...................390.630........248...........*.......-......448......-.............. +................*......333..........832*.....#...............$...565.....*...............*............701..564.........*.115..718.......313. +.960...*965....993......*.....*..............604..........730...*..................234&..662.....+..............=69.930...*.......967*...... +.....30..............355...978...........67.........419........239..........732...............645..........................484........664... diff --git a/2023/day03/main.go b/2023/day03/main.go new file mode 100644 index 0000000..72a40f9 --- /dev/null +++ b/2023/day03/main.go @@ -0,0 +1,110 @@ +package main + +import ( + "fmt" + + h "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + inp := h.StdinToStringSlice() + part1(inp) + part2(inp) +} + +func part1(input []string) { + bm := h.StringSliceToCoordByteMap(input) + symbols := bm.FindAllNot('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.') + // Gather a list of numbers that are adjacent to the symbols + nums := make(map[h.Coordinate]int) + for k, _ := range symbols { + wrk := findPartNumsFrom(bm, k) + for coord, val := range wrk { + nums[coord] = val + } + } + var sum int + for _, v := range nums { + sum += v + } + fmt.Println("# Part 1") + fmt.Println(sum) +} + +func part2(input []string) { + bm := h.StringSliceToCoordByteMap(input) + maybeGears := bm.FindAll('*') + var ratioSum int + for _, k := range maybeGears { + wrk := findPartNumsFrom(bm, k) + if len(wrk) != 2 { + // It's not a gear + continue + } + var second bool + var ratio int + for _, v := range wrk { + if !second { + ratio += v + second = true + } else { + ratio *= v + } + } + ratioSum += ratio + } + fmt.Println("# Part 2") + fmt.Println(ratioSum) +} + +func findPartNumsFrom(m h.CoordByteMap, c h.Coordinate) map[h.Coordinate]int { + partNums := make(map[h.Coordinate]int) + for _, chk := range []h.Coordinate{ + c.North(), + c.NE(), + c.East(), + c.SE(), + c.South(), + c.SW(), + c.West(), + c.NW(), + } { + if m.Get(chk) >= '0' && m.Get(chk) <= '9' { + v, pos := parseNumAt(m, chk) + partNums[pos] = v + } + } + return partNums +} + +// parseNumAt returns the number at c and the Coordinate +// that the first digit of that number is at +func parseNumAt(m h.CoordByteMap, c h.Coordinate) (int, h.Coordinate) { + var resStr string + startCoord := c + // From 'c' move left until we find the first digit + chk := m.Get(startCoord) + for coordIsNum(m, startCoord) { + startCoord = startCoord.West() + chk = m.Get(startCoord) + } + startCoord = startCoord.East() + + // Now put the number at 'startCoord' together + parseNum := startCoord + chk = m.Get(startCoord) + for coordIsNum(m, parseNum) { + resStr = fmt.Sprintf("%s%s", resStr, string(chk)) + parseNum = parseNum.East() + chk = m.Get(parseNum) + } + if resStr == "" { + return -1, h.Coordinate{X: -1, Y: -1} + } + return h.Atoi(resStr), startCoord +} + +func coordIsNum(m h.CoordByteMap, c h.Coordinate) bool { + wrk := m.Get(c) + return wrk >= '0' && wrk <= '9' +} diff --git a/2023/day03/problem b/2023/day03/problem new file mode 100644 index 0000000..2c65d27 --- /dev/null +++ b/2023/day03/problem @@ -0,0 +1,110 @@ +Advent of Code + +br0xen (AoC++) 6* + +--- 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? + + Your puzzle answer was 543867. + +--- 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? + + Your puzzle answer was 79613331. + + Both parts of this puzzle are complete! They provide two gold stars: ** + + At this point, you should return to your Advent calendar and try another puzzle. + + If you still want to see it, you can get your puzzle input. + + You can also [Shareon Twitter Mastodon] this puzzle. + +References + + Visible links + . https://adventofcode.com/ + . https://adventofcode.com/2023/about + . https://adventofcode.com/2023/events + . https://teespring.com/stores/advent-of-code + . https://adventofcode.com/2023/settings + . https://adventofcode.com/2023/auth/logout + . Advent of Code Supporter + https://adventofcode.com/2023/support + . https://adventofcode.com/2023 + . https://adventofcode.com/2023 + . https://adventofcode.com/2023/support + . https://adventofcode.com/2023/sponsors + . https://adventofcode.com/2023/leaderboard + . https://adventofcode.com/2023/stats + . https://adventofcode.com/2023/sponsors + . https://en.wikipedia.org/wiki/Gondola_lift + . https://adventofcode.com/2023 + . https://adventofcode.com/2023/day/3/input diff --git a/2023/day03/testinput b/2023/day03/testinput new file mode 100644 index 0000000..b20187f --- /dev/null +++ b/2023/day03/testinput @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598..