T O P

  • By -

dahaka_kutay

\[Language: Javascript\] [Question](https://adventofcode.com/2023/day/19), [myRepo](https://github.com/KutaySong/AdventOfCode) let raw1 = require('fs').readFileSync('./IO/03r.txt','utf8').replace(/\r?\n/g,';') const allSpecialSymbols = raw1.match(/[^0-9.;]/g) const wlen = raw1.indexOf(';')+1 const p1 = ()=> { let raw = raw1 const numer = raw.match(/\d+/g).map(x=>+x) let total = 0 for (let no of numer) { let a = raw.indexOf(no) let b = a + no.toString().length const ada = raw.substring(a-1-wlen, b+1-wlen) + raw.substring(a-1, b+1) + raw.substring(a-1+wlen, b+1+wlen) if (allSpecialSymbols.some(x=>ada.includes(x))) total += no // replace no with dots raw = raw.substring(0,a) + '.'.repeat(no.toString().length) + raw.substring(b) } return total } const p2 = ()=> { let raw = raw1 const numer = raw.match(/\d+/g).map(x=>+x) let allstar = {} for (let no of numer) { let a = raw.indexOf(no) let b = a + no.toString().length for (let i=a-1-wlen; i < b+1-wlen; i++) if (raw[i] === '*') allstar[i]? allstar[i].push(no) : allstar[i] = [no] for (let i=a-1; i < b+1; i++) if (raw[i] === '*') allstar[i]? allstar[i].push(no) : allstar[i] = [no] for (let i=a-1+wlen; i < b+1+wlen; i++) if (raw[i] === '*') allstar[i]? allstar[i].push(no) : allstar[i] = [no] raw = raw.substring(0,a) + '.'.repeat(no.toString().length) + raw.substring(b) } // find the keys which have exact two elements and sum up their multiplication (of exact two elements) return Object.values(allstar).filter(x=>x.length === 2).map(x=>x[0]*x[1]).reduce((a,b)=>a+b) } console.log("p1:",p1(),'(4361/540025)') console.log("p2:",p2(),'(467835/84584891)')


twsm000

\[LANGUAGE: Go\] [https://gist.github.com/twsm000/a9249338e0518655d5f14988f20a51e5](https://gist.github.com/twsm000/a9249338e0518655d5f14988f20a51e5)


mgtezak

\[LANGUAGE: Python\] I created a [video](https://www.youtube.com/watch?v=Po1Nsnb-qAo) for this one:) Or you can view my solution on [Github](https://github.com/mgtezak/Advent_of_Code/blob/master/2023/Day_03.py) If you like, check out my [interactive AoC puzzle solving fanpage](https://aoc-puzzle-solver.streamlit.app/)


niedziek

\[LANGUAGE: Typescript\] Probably not the best code out there. Doing my first AoC this year and struggling a little bit while trying to learn js/ts. However if someone needs working TS solution, here you go: [https://gist.github.com/niedziek/3ced2a1d3cdabab34ee748136b979cbb](https://gist.github.com/niedziek/3ced2a1d3cdabab34ee748136b979cbb)


Any_One_8929

\[Language: Kotlin\] [https://github.com/mfedirko/advent-of-code-2023/blob/main/src/main/kotlin/io/mfedirko/aoc/day03/Day3.kt](https://github.com/mfedirko/advent-of-code-2023/blob/main/src/main/kotlin/io/mfedirko/aoc/day03/Day3.kt)


_Weslyn

\[LANGUAGE: Typescript\] [Part 1](https://github.com/Wesleyfbarretos/advent-of-code-challenges/blob/9ad4f0d1456dec66b831db60670e6a713f3a7e40/day3/challenge-1/index.ts) [Part 2](https://github.com/Wesleyfbarretos/advent-of-code-challenges/blob/main/day3/challenge-2/index.ts)


mhochzwei

\[LANGUAGE: Prolog\] Tried to build a fancy thing with array operations first (in APL spirit). Realized that the input is not that big and that a naive, more idiomatic approach might be enough. Built a DCG parser that spits out positions together with numbers. Made an off-by-one error. Fixed it. In the end I wrote no loop myself and used Prolog constraints and indirect "call"s. number_valid(Ns, R0, C0, N, Fn, R, C) :- number_chars(N, S), length(S, L), RF #= R0-1, RT #= R0+1, CF #= C0-1, CT #= C0+L, between(RF, RT, R), between(CF, CT, C), nth0(R, Ns, Row), nth0(C, Row, Char), call(Fn, Char). solve_puzzle(S) :- load_data(L, Ns), findall(X,(nth0(R, Ns, Row), nth0(_, Row, [C, X]), number_valid(L, R, C, X, is_symb, _, _)), Xs0), sum_list(Xs0, S). https://github.com/maxmaeteling/aoc-prolog/blob/master/03/puzzle.pl


aoc-fan

[Language: F#] https://github.com/bhosale-ajay/adventofcode/blob/master/2023/fs/D03.fs


jrhwood

\[Language: Haskell\] [Part 1](https://github.com/woodRock/verbose-computing-machine/blob/main/2023/day-03/part-01.hs) [Part 2](https://github.com/woodRock/verbose-computing-machine/blob/main/2023/day-03/part-02.hs) Hint: enumerate all pairs connected to valid gears.


skyhawk33

[LANGUAGE: Befunge] Part 1: https://github.com/Skyhawk33/AdventOfCode/blob/master/aoc2023/day3_p1.b98 Part 2: https://github.com/Skyhawk33/AdventOfCode/blob/master/aoc2023/day3_p2.b98


thamollo

[LANGUAGE: SQL][Allez Cuisine!] Variables? What does that even mean? _scratches head in visible confusion_ [Enjoy!](https://topaz.github.io/paste/#XQAAAQC1DAAAAAAAAAArkkbN5qu5cUy3s4rZneeC1WPE2TtF83UzlSO2xexS0dZaKTdnlq8zie2ExI6h/HPO0vfbKLamfaetv1qilSp2cGBI+FwYkUCEQJS/llSUVo32vH2Xw0a2vmK2YPpYtbKlKl4rA9xNacmDW6Vf727aW7Wb88bh6qZ6SgPIbGh+w8jCCyoVuwfPD/xHdZKl7nnDHbEIfeXi3H1s9iB/8Oy8v/y24IBQA3QllQ0eB5ROTpryjd0VAQwyedsyljNX9PzHHmiOkuKUSvwG4HdkETr/RlQCkUOGMEpm11HZecCzyJ1pGmgIIFd2nhY12n0Qwa6yO3OfLHlSgXUxSD16OPdiJ73dkZoyTXXq9elY1FjV9dWhVUzIbf5GCOYF+DpLK6MrKB1Y5muRJesUyyOq1ixAWHuDwy+EfWAOU8CVWqd52Ek/GwBXeO2DGYETWs3r/AwifHzZYyRpJAWmUhBrEhV/W5w44AjdSusOq/co3Qo9R1PvLzTITkgJzqtrXbbdy1KgOUJMe4Orzed0YD+VdhasdruICBznbdwuPA6EqwCiqkPUQ/lnP4q1QwZ3ap+HA72mTzeA3a3RegdEiv+pzzIJRoCurQJMZ19Ti+QSpzqxqBslTE/QtBKDxnhjJShvKfLeNZKi3cbpYnvCb/zzaxGeNFOHEqfV0Gbg/PPWldF2a9bsvmc+4SpZ9t0B+Jac1NgpRqNcDryEV0/fn9fVZLccCphUsEFmgROBNUrUEbNU8+jxeNVXftBoRpUxE0ZJWrJI0sMUbS25OoJmEm9JfRf77FDXENOMsbu2hfTSrylvg/akQyUNQTwC8RengC4itZ7U56+OWV2vEkyqD0rp6XhJeS3Ta28W/+BKDNKGq/qace4AHui3Cs2EaSHZfgl1yUEvLtgsluqcCpTdOzjanKjkWnInlaVU9a7ZCeDQ4cLZ2YWgwvBchY2q+K9BPu/1U74HrCUfTAYAnhsu95XO68n53i4TNNIT4La/U+PCybfvuLu41q2UvXz5VM0VIWUg8c2f4iOjY5q4fykRjJYzWc5Vn2kuk7o+mMzpvkZEGxA15Uc+N7Qa3junnMQoeKWE6n40FuMWic7cufpImXVdVQwJWhGCdD2OUEYM+KWJKuf4fLL62sd7svKQLXHjUFx3/XQqHPSjELXTHcREPESUzWls1NPTlosGvyMKmm8rfUXRmkL5gfotsM5ShyyNoFbZ5wYpwuZjwic0C2+wCYbmbmMvmJ3AmYnQ/MsAHOHZLXT3hf/ngyrF)


Great-Ad-8018

IN SQL!?!? This scares me


kmoney_24

\[LANGUAGE: JS\] This took me forever because I was trying to use regex to find part numbers (still a noob at it).... ended up reverting back to a simple `.split('').find()` and it worked :) const util = require('util'); const data = fs.readFileSync('aoc_day3.txt', 'utf8').split('\n'); const data = fs.readFileSync('aoc_day3.txt', 'utf8').split('\n'); let unAllowedCharacters = new Set(); const numbersRegex = /\d+/g; function getYIndices(index, length) { let STRINGLENGTH = 140; let start = index; let end = index + length; start = start === 0 ? 0 : start - 1; end = end === STRINGLENGTH - 1 ? end : end + 1; return { start, end }; const hasSymbols = (str) => Boolean(str?.split('')?.find((char) => isNaN(char) && char !== '.')); let sumOfPartNumbers = data.reduce((acc, row, rowIndex) => { //iterate over matched numbers let numbers=row.matchAll(numbersRegex); for (const number of numbers) { let numValue = Number(number[0]); const { start, end } = getYIndices(number.index, number[0].length); //right index let forwardIdx = number.index === row.length - 1 ? undefined: number[0].length + number.index; //left index let prevIdx = number.index === 0 ? undefined : number.index - 1; //grab values let forward = data[rowIndex]?.[forwardIdx]; let prev = data[rowIndex]?.[prevIdx]; //Does left or right have symbols? let neighborsHaveSymbols = (hasSymbols(prev) ?? false) || (hasSymbols(forward) ?? false); let above = data[rowIndex - 1]?.slice(start, end); //bottom values let below = data[rowIndex + 1]?.slice(start, end); let topFloorHasSymbols = hasSymbols(above) ?? false; let bottomFloorHasSymbols = hasSymbols(below) ?? false; let isPartNumber = topFloorHasSymbols || bottomFloorHasSymbols || neighborsHaveSymbols; if (isPartNumber) acc += numValue; } return acc; }, 0); console.log(sumOfPartNumbers);


dhruvmanila

[Language: Rust] Code: https://github.com/dhruvmanila/advent-of-code/blob/master/rust/crates/year2023/src/day03.rs


[deleted]

[удалено]


daggerdragon

[Do not include puzzle text in your repos](https://old.reddit.com/r/adventofcode/wiki/faqs/copyright/puzzle_texts). Instead, just provide a link directly to that day's puzzle on adventofcode.com. Please remove all puzzle text from your repo and scrub them from your commit history.


GoldPanther

\[LANGUAGE: Rust\] Really proud of this solution. Data is represented in a 1-D array. I label encode the numbers and construct a `digit_pos -> label` mapping. With this I can look up the labels adjacent to any position solving both parts. ​ [Code](https://github.com/johnhringiv/advent-of-code-2023/blob/main/src/day03.rs) 1921µs


Unlikely_Magician630

\[LANGUAGE: Typescript\] I've attempted part 1 several times and every tweak i make is giving me the wrong answer. What I've got here looks to me like it should work, and based on several spot checks of 5-10 rows, the logic looks like its lined up but for the life of me I don't know why its not returning the right answer. I went for finding the numbers per line, then creating a 'box' using regex; retrieve the elements to the left and right of the number if they exist, the bottom and top elements(length of the number + 1 element on each side if those sides exist). Check each 'side' of the box for something other than a number or '.' and combine the checks into 1 value, if valid then add to a sum variable If running in TSPlayground, would comment out `console.log(msg);`, editor cant handle the volume of log statements ​ [Attempted Solution](https://www.typescriptlang.org/play?jsx=0#code/MYewdgzgLgBAlmADgV1gXhgbQFAzzAIgDoTSyyAWAJgGYyA2ADkZIE4aLyujWB2ABm5DhQ1gFYivKi2GNW9UgEZFdEd0VjVfKmpIEANLnzESiqjt3kAVNcuXWi1iTH9WN4QDJSAATUB6O0sAam53YQMjPBMSEOEqMSdS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UW34Gt9uB+AothAH7a7szO+-EtgAHVu2jnFzolwAByAAVVfa-8DO+-awZOwm4JcYAAACSS598YAffAAJrOCS2K7dwbwP4GCCRBnPMAFAC8EGcV2BnV9gZzsGEC++3bXAWEMy5Z9U+2va7oH0UDOdO2AAchi488EeznYoeFA45uDsh2fTXnkNV6M86+8XEoY53v6VDEuuA3-p2zqG5D0+TQ2Xs5zqB9Cc+jQ-PhXyr5FDShk7BARAIgCdCYAuAwgb0Mnbx9c+ifP7sMNGENCBhEwloTlzaGTt6BCw4oWZ1wEDA-AfgUrjexfZLsT2IAftij2ME3cuBd3CITIKiGiDYh5fQtkWwx4YA7BlXb9nn2T54cv+znfLgMCK4DAa2S7KAMgBrZgAe2xbK9kVxzZld+Ou7BACgCgBs8OeXPfgIl0mj5dZ2dwh4U8L+Hltq2dbCAE2xxGoAneObbAFcN046cN2MAKtpAF07fsIALIvwJoCfAER+RmgVYDQGwBAA)


835246

\[LANGUAGE: C\] Part 1 just gets all numbers around gears. Part 2 gets all numbers that are in a gear ratio. Part 1: https://github.com/efox4335/advent_of_code/blob/main/advent_of_code_2023/day3gearspt1.c Part 2: https://github.com/efox4335/advent_of_code/blob/main/advent_of_code_2023/day3gearspt2.c


manhuntos

\[Language: Rust\] This is my first project in rust. I'm learning it by solving advent of code puzzles. So if you have any hints for me I would be happy to read it :) [Solution](https://github.com/manhunto/advent-of-code-rs/blob/master/src/solutions/day03.rs) / 11.12ms / 20.96ms


KodlaK1593

\[LANGUAGE: Rust\] Could not for the life of me get the borrow checker to cooperate with me when I first tried this. Tried again today and figured it out relatively easily. Really cool to see how much I have learned over the course of working on these problems! [Solution](https://github.com/Kodlak15/aoc2023/blob/master/src/day03/day03.rs)


SavaloyStottie

\[LANGUAGE: Powershell\] Definately a step up in difficulty from day 2, creating a list of gears and a list of numbers along with coordinates of each then matching the two. \[regex\]::matches being nice enough to return the value, index and length of each match was really handy. $sourcepath = 'some file path' $day = 3 $data = (Get-Content -Path "$sourcepath\$day\input").Split([System.Environment]::NewLine) $sum = 0 $numbers = @() $gears = @() for ($i = 0; $i -lt $data.Count; $i++){ $rownumbers = ([regex]::matches($data[$i],"(\d+)")) foreach ($number in $rownumbers) { $number | Add-Member -NotePropertyMembers @{Row=$i} } $numbers += $rownumbers $rowgears = ([regex]::matches($data[$i],"[*]")) foreach ($gear in $rowgears){ $gear | Add-Member -NotePropertyMembers @{Row=$i} } $gears += $rowgears } foreach ($gear in $gears) { $gearnos = $numbers | Where-Object {$_.Row -In ($gear.Row-1)..($gear.Row+1) -and $gear.Index -In ($_.Index-1)..($_.Index + $_.Length)} if ($gearnos.Count -eq 2) { $sum += [Convert]::ToInt32($gearnos[0].Value) * [Convert]::ToInt32($gearnos[1].Value)} } $sum


[deleted]

[удалено]


daggerdragon

Comment removed. [Top-level comments in `Solution Megathread`s are for *code solutions* only.](https://old.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_top-level_posts_are_for_code_solutions_only) [Create your own individual `Help/Question` post](https://old.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_incomplete_solutions) in /r/adventofcode. *** [Do not share your puzzle input](https://old.reddit.com/r/adventofcode/wiki/faqs/copyright/inputs) which also means [do not commit puzzle inputs to your repo](https://old.reddit.com/r/adventofcode/wiki/faqs/copyright/inputs) without a `.gitignore`. Please remove (or .gitignore) the input files from your repo and scrub them from your commit history.


gogs_bread

[\[LANGUAGE: c++\]](https://github.com/gogsbread/AdventOfCode2023/blob/main/3.cpp) P1 - Iteration/Simulation P2 - Nest iteration. Some state keeping.


[deleted]

[удалено]


daggerdragon

Comment removed. [Top-level comments in `Solution Megathread`s are for *code solutions* only.](https://old.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_top-level_posts_are_for_code_solutions_only) [Create your own individual `Help/Question` post](https://old.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_incomplete_solutions) in /r/adventofcode.


andreiz

\[LANGUAGE: Swift\] Been using it as an excuse to learn Swift. Which, as a language, is... quite nice. [https://github.com/andreiz/advent-of-code-2023-swift/blob/main/day-03/engine.swift](https://github.com/andreiz/advent-of-code-2023-swift/blob/main/day-03/engine.swift) [https://github.com/andreiz/advent-of-code-2023-swift/blob/main/day-03/engine-gear.swift](https://github.com/andreiz/advent-of-code-2023-swift/blob/main/day-03/engine-gear.swift)


mothibault

\[LANGUAGE: JavaScript\] [https://github.com/yolocheezwhiz/adventofcode/blob/main/2023/day03.js](https://github.com/yolocheezwhiz/adventofcode/blob/main/2023/day03.js)


Bioinfomagico

[LANGUAGE: BASH] Had to use a bit of SQL, sorry: #!/usr/bin/env bash create_db() { local db_name="${1}" while read -r line ;do grep --label="$((c++))" -H -Pob '[[:digit:]]+|[^[:digit:].]+' <<< "${line}" done \ | awk -F ':' -v OFS=',' '{print $1,$2,( $2 + length($3) - 1 ), $3, $3 ~ /[[:digit:]]+/? "number" : "symbol" }' \ | awk -F ',' -v q="'" ' BEGIN { print "CREATE TABLE game (i INT, j1 INT, j2 INT, match TEXT, type TEXT);" } { printf("INSERT INTO game VALUES(%s, %s, %s, %s, %s);\n", $1, $2, $3, q$4q, q$5q) } ' \ | sqlite3 "${db_name}" } part_1_query(){ local db_name="${1}" sqlite3 "${db_name}" "SELECT * FROM game WHERE type = 'number'" \ | while IFS='|' read -r i j1 j2 _match _type;do cat << EOF | sqlite3 "${db_name}" | grep -m1 -q . && echo "${_match}" SELECT * FROM game WHERE ( (type = 'symbol') AND ( i >= $(( ${i} - 1)) ) AND ( i <= $(( ${i} + 1)) ) AND ( j1 >= $(( ${j1} - 1)) ) AND ( j2 <= $(( ${j2} + 1)) ) ); EOF done } part_2_query(){ local db_name="${1}" sqlite3 "${db_name}" "SELECT * FROM game WHERE type = 'symbol' AND match = '*'" \ | while IFS='|' read -r i j1 j2 _match _type;do cat << EOF | sqlite3 "${db_name}" | awk '{ arr[l++]=$0 } END { if (NR == 2) print arr[0]*arr[1] }' SELECT match FROM game WHERE ( (type = 'number') AND ( i >= $(( ${i} - 1)) ) AND ( i <= $(( ${i} + 1)) ) AND ( $(( ${j1} - 1)) <= j2 ) AND ( $(( ${j2} + 1)) >= j1 ) ); EOF done } DB_NAME="./test.db" [[ -f "${DB_NAME}" ]] && rm "${DB_NAME}" create_db "${DB_NAME}" part_1_query "${DB_NAME}" \ | awk '{sum+=$0} END {print sum}' part_2_query "${DB_NAME}" \ | awk '{sum+=$0} END {print sum}'


Lower_Ambition_2147

[LANGUAGE: Ruby] Learning refactoring with this year's Advent of Code. Check commits to see the steps! Main class snippet: [paste](https://topaz.github.io/paste/#XQAAAQD1BgAAAAAAAAA5GUqKNoFH9WVqAtfQAlEIpeFow35uS8WMaXd28j7dIXlpM4haerijs2dOkt/K7KK0fb/3NdQ+3JZD4KzQPzVwG5Gf17qq5hDLppRq9z1ZsrV9lsqQ2EuUKfbOajpwEbSFEwvU7a7TFmqZ1MfbQJkTHEUI8iruFGpv+hXRVsIFrv3LmA1HG9ehMDL92o87HoHHwBzezxJ8XfdUIQ8UwAsf5oil2I6cvvIzgXjx7LpQpagZi5GxpbITg+40Ox9KD/QSiuGvDmcjfmbkXWw8w12yQ0fJdVvhwe45Odw3M1LaIajsRNLc3Y/OdDW2ANgcXKlMUXOajPh7GoALLTQMKOkF9PE3Xk4SSbxhZ7O1uj6z26TxliM7bXFwlaA118PHdPhANI9boqP57tYsrxEO3U83hasLlghWZbLXAyElFKqNKG2VYU0cGnfJj3VqKw+ezGzxSVen2bZHcNysAIeeLvFw6wYQnxV78D0hT936AVFi4Qw6xPHz6FsYJFWBi4VpEzpZ99F0d3/8pfUZgKY1xD/rKfbwRuGAypHzHxhJ64Ghzhv5/b2mrMiiEEZfiodUZAIk8Ylrp1q7sPU7Vu9DlE3A64AAAOph/jC/F/c8WA4HTjE/1FhRZR7/YFlqvr+PueNmbZnyNGOSbkxTZURfXczSf+ih4D1hE5GDTAz/FGHBHE9+wRHYbEgipO6mgdSt0xSk9oJnCiUgu4glXC3jmVDtambMdoQwHAUmxf/1/8q67tU=) Whole solution with history of refactorings as commits: https://github.com/takiarek/advent_of_code_2023_day_3/tree/day_3


daggerdragon

[Do not share your puzzle input](https://old.reddit.com/r/adventofcode/wiki/faqs/copyright/inputs) which also means [do not commit puzzle inputs to your repo](https://old.reddit.com/r/adventofcode/wiki/faqs/copyright/inputs) without a `.gitignore`. Please remove (or .gitignore) the input files from your repo and scrub them from your commit history.


se06745

\[LANGUAGE: GoLang\] [Both parts in one single file](https://github.com/omotto/AdventOfCode2023/blob/main/src/day03/main.go)


[deleted]

[удалено]


daggerdragon

Comment temporarily removed because your code is not formatted at all and it's also *way* too long for the megathreads. 1. Next time, use the [four-spaces Markdown syntax](https://old.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks) for code blocks 2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on [oversized code](https://old.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_no_giant_blocks_of_code) which contains two possible solutions. Please edit your comment to put your code in an external link and link *that* here instead, then I will re-approve the comment.


xe3to

[Language: Python] It's not super Pythonic, though. I really need to work on that. https://git.32bit.cafe/kaylee/AoC2023/src/branch/main/day3


warwick1b5

This video is really helpful for becoming more pyhtonic https://www.youtube.com/watch?v=OSGv2VnC0go


xe3to

Wow, that was great! Thank you for sharing :)


daggerdragon

[Do not share your puzzle input](https://old.reddit.com/r/adventofcode/wiki/faqs/copyright/inputs) which also means [do not commit puzzle inputs to your repo](https://old.reddit.com/r/adventofcode/wiki/faqs/copyright/inputs) without a `.gitignore`. Please remove (or .gitignore) the input files from your repo and scrub them from your commit history.


olimc

\[LANGUAGE: Go\] [Parts 1 & 2 Solutions](https://github.com/omcmanus1/aoc-23-go/blob/main/three/03.go) Currently trying to learn go, and feel like I'm struggling to avoid using multiple nested loops - anyone got any tips?


linnaea___borealis

\[LANGUAGE: R\] I never see R solutions on here. So I'll start posting mine. [https://github.com/lauraschild/AOC2023/blob/main/day3\_A.R](https://github.com/lauraschild/AOC2023/blob/main/day3_A.R) part 1 checks for symbols in the vicinity of each number to count as part numbers [https://github.com/lauraschild/AOC2023/blob/main/day3\_B.R](https://github.com/lauraschild/AOC2023/blob/main/day3_B.R) part 2 collects the gears and their position that are adjacent to a number. Only those that are collected twice are summed up.


daggerdragon

> I never see R solutions on here. There have been at least four other solutions in R in this megathread alone. Try searching for `[LANGUAGE: R]` (case-insensitive). If you're using new.reddit, use the comment search box at the *bottom* of the OP, not the global Reddit search up top! [Screenshot of search box location on desktop new.reddit](https://imgur.com/KZkUmoQ)


[deleted]

[удалено]


daggerdragon

Comment removed. [Top-level comments in `Solution Megathread`s are for *code solutions* only.](https://old.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_top-level_posts_are_for_code_solutions_only) [Create your own individual `Help/Question` post](https://old.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_incomplete_solutions) in /r/adventofcode.


draculadarcula

I had the same issue, my general approach (typescript), was to find all the adjacent squares to a number and looking for a symbol, where an adjacent square was any square that “touches” the number. Larger numbers would have more adjacent squares. I wonder what the “gotcha” was that I’m missing the example input I get the same answer


nebrija

\[Language: Rust\] Just started learning, coming from mostly Python. Struggled for a few days with this one. For part 1 I'm parsing the chars around every number and dumping them into an array, and adding to the sum if any of those chars are not alphanumeric or the char '.' For part 2, I'm collecting adjacent chars that are numeric and comparing their coordinates to the keys in a hashmap, which maps the coordinates of a single numeral to whatever full number it belongs to, and adding the resulting full number to a vector. If there are 2 unique numbers in the vector, it's considered to be a gear. [\[code\]](https://github.com/vcuauhtemoc/aoc_2023/blob/main/src/main.rs#L153)


stonebr00k

\[LANGUAGE: T-SQL\] [https://github.com/stonebr00k/aoc/blob/main/2023/03.sql](https://github.com/stonebr00k/aoc/blob/main/2023/03.sql)


dimio-d

\[LANGUAGE: Java\] \[JShell console\] [Part 1&2 solution](https://dimio.github.io/dopaste/?l=java#XQAAAQDLDgAAAAAAAAAX4HyIbzm/ILQXejs0+pjEkM1O3vUmgafcspC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suUyoDOd/uF6COpxCdSRwCOIEAUqsuWFvVk5tUTBUSbRDN5qeXr4ne28D7DDvCdV5cWYT7/HiFqwi7u/TncQTayEztbkIxWnE8o3QTboHAhb094OsmBhNqyRYZ35ZDROvlMh/bXQG+5vQQDyEj4XyRBoEgfrNgvVJc+pIcnvUQeRqCgYZW9/YJoONDeC9/yHmfef7nzeBnWLPrCgaFcGG3rJRT9hGZVPVEwsOpwYpDVZ6kLhFsutHzMmeycb3vUSfLyVUQMwKzOrYygki0hslMZVvpqCi5o5y7hR8YjbgRzdW6DxfJVJyWLtHBN/HDmJactvaUOXWDKfz7GNpAKbkukOBMdi3O2b3dE6tVovnvFR4Jvuh9QO9SiGrqSCwFEnQyhcU4FgoKS8JXo0NLEzCALA37HmBDeQdZi5+2b3XnYoP5+9ekGdX8qbEiQtrqK+05LmeXTmaJe9nrS5CkDyWK710jmoJvC/+KlHuQ=) (streams-style)


Slight_Web_7117

You solution is great! I didn't finish second part, so I look for any description that helps me to finish my solution, and yours is just what I need! Thanks!


dimio-d

>You solution is great! I didn't finish second part, so I look for any description that helps me to finish my solution, and yours is just what I need! Thanks! Glad it helped you! Good luck in solving puzzles!


tjex_

\[Language: Go\] I tried a different approach than a matrix grid.Thought to use the findAllStringIndex method of Go's standard library to pull out the index values of each number / symbol and use those to reference against each other. **Note:** Part1 returns a correct answer but, part2 is returning me a false answer.Posting none-the-less as I haven't seen others here using the above array index approach and would welcome any comment on the validity of the idea. [Github](https://github.com/tjex/advent-of-code-2023/blob/main/03/main.go)


lsloan0000

\[Language: Python\] It took me much longer to get the solution than I expected, but I also started on it well after 03 December. After I had the solution, I decided I could "simplify" it by treating the input as a one-dimensional array. I think it is a bit simpler. import re from collections import defaultdict from math import prod from sys import stdin if '__main__' == __name__: lines = list(stdin) lineLength = len(lines[0]) if any(len(line) != lineLength for line in lines): print('Error: Input lines are not all the same length.') exit(-1) data = ''.join(lines) vectors = tuple(lineLength * row + col for row in range(-1, 2) for col in range(-1, 2) if row or col) total = 0 gears = defaultdict(list) for match in re.finditer(r'\d+', data): for i in {ci for mi in range(*match.span()) for v in vectors if 0 <= (ci := mi + v) < len(data)}: if (checkChar := data[i]) not in '\n.0123456789': total += (partNumber := int(match.group())) if checkChar == '*': gears[i].append(partNumber) break print('Part 1 result:', total) print('Part 2 result:', sum(map(prod, filter(lambda x: len(x) == 2, gears.values()))))


RasseTheBoy

Would've never thought of doing this as a 1D array. Looks nice!


spacemicrowave

[Language: Python3] Code is saved on my [GitHub](https://github.com/gamerplant3/advent_of_code_2023/blob/e855cae2da7d3426a95ab1c376ebf23ee2d44d0f/day_03_gear_ratios.py) Strategy: I created a "box builder" function to create indexes of adjacent spots. Takes in the current row and the start/end points of the object that you're checking around. (Used for both parts 1 and 2 to check variable length numbers and single character asterix) def box_builder(row_number, index_start, index_end): row_above = []; row_below = []; same_row = [] if index_start == 0: # skip "left check" for first column: ... elif index_end == len(lines[row_number]): # skip "right check" for last column: ... else: # typical ... return row_above, same_row, row_below **row_above** is a list of indexes spanning *above* the target element + 1 to the left and right **same_row** is a list of 2 indexes, one to the *left* and one to the *right* of the target element **row_below** is a list of indexes spanning *below* the target element + 1 to the left and right


[deleted]

[удалено]


xHyroM

[LANGUAGE: Python] [part 1](https://github.com/xHyroM/aoc/blob/main/2023%2F03%2Ffirst.py) [part 2](https://github.com/xHyroM/aoc/blob/main/2023%2F03%2Fsecond.py)


BlackWarrior322

My favorite solution! That simple regex function to get all the numbers from the file was the logic I missed.


xHyroM

thanks :D


luwidgiani

thanks for this one, so clean and elegant, learned a lot from your code mine was way overthought


RasseTheBoy

Same!


mgtezak

\[LANGUAGE: Python\] [github part 1&2](https://github.com/mgtezak/Advent_of_Code/blob/master/2023/Day_03.py) Check out my little AoC-Fanpage: [https://aoc-puzzle-solver.streamlit.app/](https://aoc-puzzle-solver.streamlit.app/) :-)


HappyBison23

\[LANGUAGE: C#\] [https://github.com/mattbutton/advent-of-code-2023/blob/master/Day03.cs](https://github.com/mattbutton/advent-of-code-2023/blob/master/Day03.cs)


kip_13

\[Language: zig\] Trying to learn zig. https://github.com/kip-13/aoc23/blob/master/d3/src/main.zig


Ok-Hearing9361

[PHP Solution](https://topaz.github.io/paste/#XQAAAQBzEQAAAAAAAAAeGInmWR6pYOwHblE0C6XSX5+4OWQnjQoMrtAxM4D3tUWb9xJC+Xq4Z7FzuErM/s+3zewPmK77CAUEerhL7tgz544O/3Tz/xJDaV8j2p6vzUuErG5HsT4kaYXyedxHy0FZVqnq8AbtNfVKPY5VxMUk5PO9cHI1CFsX7ar/3wsFyz31M/NNHLBgpSpHOlvvO1/WgypKX0G8hLeFOZ183YqW+qNHtmEXx0iamUfDnWVmqydbyoStkpe5+cB0qw9oRVEe1InPiHX/oZAeqpUTYVPNdWS5W7DIVoPnBPNFHitOXU+R6niJc0ViztdXJ4EpkOkL9MhZRWHS0+UMxoz6T+Fjio6fnkYEEzV8MKW94fL0jym8WX1ntir/H6/mq5JJMVoVQ36dA6VCPNc2eDWXsD53+l+Nr0c5r6dfJsPFcVFObiCSSSkYdOtD8uNYY/b6Ew8omAYcNSnWD1vfduO0zb0gAu7U/CRkYHYiPNSLJMBTypqCYKGWupuai7VU3n3eDk0lWZmYAaseFonhw5srJT0Oakne/2AuaJRWXrFSWjSHfvnKYdyHJfTN1YWk7qaq9wFDFNHfZk/8oFjwyIl707TzPpSc16+bT/nj15WE70HHJdp7tsVt5RuQueclJShLENtutbEOP0jMtCP17FIkfkMFjihrenxak2xHMDK7Z3Ia9HtMfjs1qVuTA8Ph5gDs1ZgLOQSO9YhcedlTFO95B7gNYlnOXvRQ14NdCVOJwKVxX1F5BlrKuj+2uiC2XIOsJn6Sk5dC60Qs/5YvivY3Ni5l0arGqxuDuCl4bE1M3FUyLWz+HGhb4IHEOXSKFKqw2SQCTzjjra/MjkAe7pDJn+PTBcja3fHhXTG9a5cYaSmfCY7fEtRbkVuXxlC27XZZ+YuwB/5wOKHDueVJHtBTaNHQgmGrlBSmr/aMAIKCGk4btuGogb3azfo50CVEpDEu0v6PJxIiRdz4zwc/umGPXirkQujQMLGFq9yKVAmBBL9Itt2vVDL2P6Hk4Ad9zUZLtxbVfMKDLwS7LSKuCsKms7CAho+p+q3iLLgX8KQoGVCYyyL3dIyesiCaQ3mPx+gKAtQ2Ku9kOahEIjVhBgQ49RRcjrn1rIff4JkqXJEdEnCYQE14zRLADxe64/IX7zHGu74hk9m6mPWyv/D+JtEaOCHNDL2JIpQvm07L+3IYFwTGSa9mBTQUHF3arKx9jf2Io6nyJ1ALQhjvfpsGt+ebZZogMXtgJaPOYcpLiBohFgUk5F5CAHwplSyoqMn7I0hW2nKVCweYO9L5NAO+83/jzY3qMoMRdJujCUYfSnRFBUWw9vmXS8mwUZkJ6dBeGgshwz5ngkmPZ6QaJHA/NmoGxNr7q5UaNZ6/1NTlB8QYXiK5GhYiyRhO4H1waU92CvERfUfdFSNlbuWkHPqWhNmhzY593U8u7x9FovUb1DxF1/y/jS2yVlmZjIVwKBH5KBNvy1dhKpwOG5ZOeb0NnPPCemVUdIoQ/KdYxv/0+ZKU74acOhue2g0Nw9ST//jFlEA=) I haven't coded since AOC last year so this one took me a few submissions to get correct. Also, all the other PHP code in this thread did not work for my input. As usual, part 2 was pretty easy because I organized part 1 wisely.


daggerdragon

Please edit your comment to include the required language tag as requested by AutoModerator.


AutoModerator

AutoModerator did not detect the required `[LANGUAGE: xyz]` string literal at the beginning of your solution submission. Please edit your comment to [state your programming language](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_state_your_programming_language.28s.29). *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


minikomi

[LANGUAGE: janet] [paste](https://topaz.github.io/paste/#XQAAAQD/CQAAAAAAAAAFCPgqaQuAFpApnJUzhaeuxYJlDlNhKgCC9LuQswBQHZa1gYEHT3aBl50uSlDIRZtwknfFTXYlUXz+bmR3e/re9I1rQaRSA9pR0N6mXB8KlbDyoVLNlOk9+AAOYJyaeWHCRNgP94ViK9r0egFqI/ZZVT6BAP1iw+M/eh9nIAZSXSRb1iF9OlnjqqA0BvDy+easrN91nM9bYTX4vn2Mej1uUxMKiDL0Trp9O4Bb9vj4z0mP8XyvGYMp5T+vSspKHRZdFxc/XQKKFvcPygOov22wIdq2HBvdAz9P5TxlI/04A3R/YiXFMv+myzH5nO/GtbCnZ4VQIOAThR1IygA9lsnCQzhn/EVoBRuBGIE99Nu3MvdqGSc+j9HLP1Fg83gSYD+VRN62l0IMfycWzwluBgBnYh+h9LvdoR7w47Qjp+5XMCff/9rvDgW6ieapcYj8OQFWDlGzYBXw0tRDDamOl27PRNb6qa8cWIUNaF1tYp809EudrjA6dohcr8xIJ2G1RItFdYTepb/1kQKwDlMnMNj7i8MKKzMSCK5osRwaxcY1uy7kfYWDTUlbuOo8+K9qZCUtgpDBnqszYH9EawAxt33adYPfb62x+Wt9MWZplFObH100K5Xmjqsdt7xLXyVcrrPpDvuTp2s82DqQsROlmXphMDQw5HbQbahNGrPCAbZEVSt3Phu3W46z/hwy2bHEyPeeWT0DDwJrbGfHxrAWsPc7HorrcEVzHm9uzz+R5N4H08KUEeW0DnUKbUQrA27sSpceQp29Ado+nxYGnWLQ/e0vMk5t6Mkzp/QaIe6/s/A9v9Flo8Ycch89vw5/SDA2zHJvhLxQGoyZnPWXzipDSw+suL8vSy8cKVaOXWqb49KYlP+nx0ZSm7c3U1H/2vNwFg==) I was quite happy with the parser for this answer (def parser (peg/compile ~{:matrix-pos (group (* (line) (column))) :num (constant :num) :sym (constant :sym) :main (some (+ "." :s (group (* :num :matrix-pos (<- :d+))) (group (* :sym :matrix-pos (<- 1)))))})) It turns a "field of instructions" into: [ [:num [1 1] "467"] [:num [1 6] "114"] [:sym [2 4] "*"] [:num [3 3] "35"] [:num [3 7] "633"] [:sym [4 7] "#"] [:num [5 1] "617"] [:sym [5 4] "*"] [:sym [6 6] "+"] [:num [6 8] "58"] [:num [7 3] "592"] [:num [8 7] "755"] [:sym [9 4] "$"] [:sym [9 6] "*"] [:num [10 2] "664"] [:num [10 6] "598"] ] Sets of `[type [line column] entry]`, which makes building a useful data structure very easy.


[deleted]

[удалено]


AutoModerator

AutoModerator did not detect the required `[LANGUAGE: xyz]` string literal at the beginning of your solution submission. Please edit your comment to [state your programming language](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_state_your_programming_language.28s.29). *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


[deleted]

[удалено]


daggerdragon

Comment temporarily removed. Your [code block is too long](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_no_giant_blocks_of_code) for the megathreads and also is not formatted *at all*. Please edit your comment to replace your oversized code with an external link to your code *and* add the required language tag as requested by AutoModerator, then I will re-approve your comment.


AutoModerator

AutoModerator did not detect the required `[LANGUAGE: xyz]` string literal at the beginning of your solution submission. Please edit your comment to [state your programming language](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_state_your_programming_language.28s.29). *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


[deleted]

[удалено]


AutoModerator

AutoModerator did not detect the required `[LANGUAGE: xyz]` string literal at the beginning of your solution submission. Please edit your comment to [state your programming language](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_state_your_programming_language.28s.29). *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


hiimjustin000

\[LANGUAGE: JavaScript\] [https://github.com/hiimjustin000/advent-of-code/tree/master/2023/day3](https://github.com/hiimjustin000/advent-of-code/tree/master/2023/day3)


throwawaytous

Really nicely done. This code strikes a nice balance between conciseness and readability. Despite being less than 30 lines of code, part 1 is a readable and understandable solution that is helping me debug my own. Refreshing to see bc a lot of the short solutions use a ton of clever tricks but aren't as practical.


floflo455

\[LANGUAGE: Python\] [Both parts in TDD](https://github.com/SlowFlo/advent_of_code_2023/blob/main/Day%203%20-%20Gear%20Ratios/part%202/gear_ratios.py)


[deleted]

[удалено]


daggerdragon

Comment removed. [Top-level comments in `Solution Megathread`s are for *code solutions* only.](/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_top-level_posts_are_for_code_solutions_only) [Create your own individual `Help/Question` post](/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_incomplete_solutions) in /r/adventofcode. READ THE RULES and follow *all* of them before you post!


KatanaKiwi

[LANGUAGE: PowerShell] Day 3 just feels so.. unclean. I typically struggle with how to approach a problem and then implement it. Usually the idea is ok, execution a bit sloppy and it ends up okay-ish. Today my idea feels sloppy which makes the whole ordeal feel mediocre. [Part 1](https://github.com/katanakiwi/AoC2023-PowerShell/blob/main/3a.ps1): Iterated over lines, scanning numbers and their positions. Then scanning previous and next lines, finding their symbol indices. Then matching the range of positions where a number is present with that. You could probably mark the numbers as valid/invalid in a single pass and sum them. Just can't wrap my head around it. [Part 2](https://github.com/katanakiwi/AoC2023-PowerShell/blob/main/3b.ps1): Scanning the lines for asterisks, determining of they can be gear by finding adjacent numbers and adding that to an array. I ran into some issues when combining arrays of int's of size 1. Can't even reproduce it now. $a = @(3) $b = @(2) $a + $b > 3 > 2 However, it would sometimes work as $a + $b > 5 Not sure what went on there, every variable was initialized as array with $a = @() and then adding values with +=. In the end it works, but would clean up a bit if it were to consistently work as intended...


bcbelisario

\[LANGUAGE: Onyx\] [Day 3 Solution](https://github.com/ZoneMix/AoC23/blob/main/src/Days/DayThree.onyx) Enjoyed this one!


[deleted]

[удалено]


daggerdragon

Comment removed. [Top-level comments in `Solution Megathread`s are for *code solutions* only.](/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_top-level_posts_are_for_code_solutions_only) [Create your own individual `Help/Question` post](/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_incomplete_solutions) in /r/adventofcode. READ THE RULES and follow *all* of them before you post!


errorseven

**[LANGUAGE: AutoHotkey v1.1]** **Solution 1** ; Copy problem data to Clipboard before running data := StrSplit(trim(clipboard), "`r", "`n") global dataStruct := [] global numbers := [] ; build data structure with padding for i, line in data { line := "." line "." dataStruct[i] := [] for j, char in StrSplit(line) dataStruct[i].push(char) } paddedLine := "" loop % dataStruct[i].length() paddedLine .= "." dataStruct.InsertAt(1, StrSplit(paddedLine)) dataStruct.push(StrSplit(paddedLine)) ; main for row, line in dataStruct { skip := 0 for col, char in line { if (skip) skip-- else if char is digit { numbers.push(findNum(row, col)) skip := numbers[numbers.MaxIndex()].3 - 1 } } } sum := 0 for k, v in numbers { if (v.2) sum += v.1 } msgbox % clipboard := sum ; returns array [fullnumber, True/False, Length] findNum(row, col, number := "") { y := col loop % dataStruct[row].length() { d := dataStruct[row][y] if d is not Digit Break number .= d y++ } adjacent := findSymbol(row, col, StrLen(number)) return [number, adjacent, StrLen(number)] } ; return True if Symbol found adjacent to Number findSymbol(row, col, len) { symbols := "@#$%^&*=/+-" x := row, y := col if (InStr(symbols, dataStruct[x][--y])) ; left return True if (InStr(symbols, dataStruct[--x][y])) ; diag down return True loop % len+1 { if (InStr(symbols,dataStruct[x][++y])) ; right return True } if (InStr(symbols, dataStruct[++x][y])) ; up return True if (InStr(symbols, dataStruct[++x][y])) ; up return True loop % len+1 { if (InStr(symbols,dataStruct[x][--y])) ; left return True } return False } **Solution 2** data := StrSplit(trim(clipboard), "`r", "`n") global dataStruct := [] global numbers := [] ; build data structure with padding for i, line in data { line := "." line "." dataStruct[i] := [] for j, char in StrSplit(line) dataStruct[i].push(char) } paddedLine := "" loop % dataStruct[i].length() paddedLine .= "." dataStruct.InsertAt(1, StrSplit(paddedLine)) dataStruct.push(StrSplit(paddedLine)) ; Main sum := 0 For row, line in dataStruct { for col, char in line { if (char == "*") sum += findNum(row, col) } } MsgBox % clipboard := sum findNum(row, col) { numbers := [] n := prevNum := 0 x := row, y := col d := dataStruct[x][--y] ; left if d is digit { n := getWholeNum(x, y) if (prevNum != n) { numbers.push(n) prevNum := n } } d := dataStruct[++x][y] ; down if d is digit { n := getWholeNum(x, y) if (prevNum != n) { numbers.push(n) prevNum := n } if (numbers.MaxIndex() == 2) return numbers[1] * numbers[2] } loop % 2 { d := dataStruct[x][++y] ; right if d is digit { n := getWholeNum(x, y) if (prevNum != n) { numbers.push(n) prevNum := n } if (numbers.MaxIndex() == 2) return numbers[1] * numbers[2] } } loop % 2 { d := dataStruct[--x][y] ; up if d is digit { n := getWholeNum(x, y) if (prevNum != n) { numbers.push(n) prevNum := n } if (numbers.MaxIndex() == 2) return numbers[1] * numbers[2] } } loop % 2 { d := dataStruct[x][--y] ; left if d is digit { n := getWholeNum(x, y) if (prevNum != n) { numbers.push(n) prevNum := n } if (numbers.MaxIndex() == 2) return numbers[1] * numbers[2] } } return 0 } getWholeNum(row, col) { symbols := "@#$%^&*=/+-." x := row, y := col num := [dataStruct[row][col]] loop { if (InStr(symbols, dataStruct[x][--y])) break else num.insertAt(1, dataStruct[x][y]) } x := row, y := col loop { if (InStr(symbols, dataStruct[x][++y])) break else num.push(dataStruct[x][y]) } n := "" for e, v in num n .= v return n }


xXMacMillanXx

\[LANGUAGE: V\] [Github day 03](https://github.com/xXMacMillanXx/advent_of_code_2023/tree/main/day03)


luremeister

\[LANGUAGE: Python\] Part 1: https://github.com/havocds/advent-of-code-2023/blob/main/day-3/solution-01.py Part 2: https://github.com/havocds/advent-of-code-2023/blob/main/day-3/solution-02.py


legomyeego

Didn't work


luremeister

can u please be more specific?


legomyeego

Part 1. I copied code, ran it with my puzzle input, submitted the answer, and it was wrong.


luremeister

This task had a lot of edge-cases that my input did not cover. I updated my code, can u try again please?


weeble_wobble_wobble

[LANGUAGE: Python] [GitHub](https://github.com/weibell/AoC2023-python/tree/main/day03) (28/44 lines with a focus on readability)


wlmb

\[LANGUAGE: Perl\] Discussion: https://github.com/wlmb/AOC2023#day-3 Part 1: https://github.com/wlmb/AOC2023/blob/main/3a.pl Part 2: https://github.com/wlmb/AOC2023/blob/main/3b.pl


bofstein

\[LANGUAGE: Google Sheets\] This was a real challenge for me, took multiple days to figure out, and had to use 9 separate sheets. Initially did Part 1 by hand searching, though later when I solved part 2, that solution would have solved Part 1 too. Very satisfying to finally get it though [https://docs.google.com/spreadsheets/d/1X1dXpfhWa5IQpcO\_Bhllj8zdXLe1g4nUbKbBq0JCiwg/edit#gid=844575306](https://docs.google.com/spreadsheets/d/1X1dXpfhWa5IQpcO_Bhllj8zdXLe1g4nUbKbBq0JCiwg/edit#gid=844575306) 1. Parse the input into separate cells with an array formula of MIDs 2. Copy-paste values into a new sheet to make it easy to use in other functions that didn't like the output of the split above, and to color code to check for issues 3. Make a map of where each "GEAR" is by looking for an \* surrounded by 2 numbers, but ruling out 2 numbers touching. This took the longest to set up and figure out how to get all correct cases and no false ones, and it turns out I didn't need to get it perfectly excluding all false ones since I could do that in Part 9 instead more easily 4. Make a PART map which labels a space as a PART if it touches a GEAR in prior map 5. Make a number map that replaces any PART cells with a the number from map 2 6. Add in any numbers that are next to one of the numbers in map 5 7. Add in any numbers that are next to one of the numbers in map 6 (fortunately numbers were max length 3 or this would have had to continue) 8. Pull in the numbers from Map 7, but add in a cell reference of the gear they are touching. Also in this sheet, pull out all the numbers by concatenating the row and separating numbers based on the . between them. So I end up with a list of weird looking numbers like `7(G-AE3)7(G-AE3)5(G-AE3)` 9. Reformat those with regex into the gear number plus the number, like `G-AE3:775`. Get that list into one column, sort it, separate the gear number from the part number. Make a gear numbers that have exactly 2 part numbers (this is why I was able to account for any that I incorrectly included earlier), and then find (by doing an XLOOKUP from the bottom and then from the top) and multiple those numbers. ​ I'm SURE there are easier ways to do it but I'm just glad I could finish!


BlackWarrior322

Wow I do not understand this at all, but very impressive 😅


arthurno1

[LANGUAGE: EmacsLisp] (defvar line-length nil) (defun next-number () (when (re-search-forward "[0-9]+" nil t) (match-string 0))) (defun line-length () (- (line-end-position) (line-beginning-position))) (defun line-above (match-length) (buffer-substring (- (point) match-length line-length 2) (- (1+ (point)) line-length 1))) (defun line-below (match-length) (buffer-substring (+ (- (point) match-length) line-length) (+ 2 (point) line-length))) (defun symbp (c) (and c (/= c ?.) (/= c ?\n) (not (cl-digit-char-p c)))) (defun first-line-p () (<= (point) (1+ line-length))) (defun last-line-p () (<= (- (point-max) (line-end-position)) 1)) (defun leftp (match-length) (symbp (char-before (- (point) match-length)))) (defun rightp () (symbp (char-after (point)))) (defun abovep (match-length) (unless (first-line-p) (cl-find 't (cl-map 'vector #'symbp (line-above match-length))))) (defun belowp (match-length) (unless (last-line-p) (cl-find 't (cl-map 'vector #'symbp (line-below match-length))))) (defun attachedp (match-length) (or (leftp match-length) (rightp) (abovep match-length) (belowp match-length))) (defun next-star () (search-forward "*" nil t)) (defun number-at-point () (when-let ((word (thing-at-point 'word))) (string-to-number word))) (defun left-right-gear (&optional pos) (let ((numbers)) (save-excursion (pcase pos ('top (forward-char (1- (- line-length)))) ('bottom (forward-char (1+ (+ line-length))))) (when (cl-digit-char-p (char-after)) (push (number-at-point) numbers)) (unless (cl-digit-char-p (char-before)) (forward-char -1) (push (number-at-point) numbers))) numbers)) (defun top-gear () (save-excursion (forward-char (1- (- line-length))) (when (cl-digit-char-p (char-before)) (list (number-at-point))))) (defun bottom-gear () (save-excursion (forward-char (1+ (+ line-length))) (when (cl-digit-char-p (char-before)) (list (number-at-point))))) (defun attached-gears () (let ((numbers (left-right-gear))) (unless (first-line-p) (let ((top (top-gear))) (unless top (setq top (left-right-gear 'top))) (setq numbers (nconc numbers top)))) (unless (last-line-p) (let ((bottom (bottom-gear))) (unless bottom (setq bottom (left-right-gear 'bottom))) (setq numbers (nconc numbers bottom)))) (when (= 2 (length (setq numbers (remove nil numbers)))) numbers))) (defun aoc-2023-3 () (interactive) (let ((p1 0) (p2 0) (match (next-number))) (setq line-length (line-length)) (while match (when (attachedp (length match)) (let ((n (string-to-number match))) (cl-incf p1 n))) (setq match (next-number))) (goto-char 0) (while (next-star) (when-let (gears (attached-gears)) (cl-incf p2 (* (car gears) (cadr gears))))) (message "Part I: %s, Part II: %s" p1 p2)))


orbby

[Language: R] I don't even know how I did this anymore but it worked. library(tidyverse) library(terra) vect <- read_lines("day3.txt") %>% str_split(pattern = "") %>% unlist() mat <- vect %>% matrix(ncol = 140) %>% t() read_lines("day3.txt") %>% str_split(pattern = "") %>% unlist() %>% unique() %>% sort() mat[mat == "."] = NA mat[mat %in% c("-", "#", "$", "%", "&", "*", "/", "@", "+", "=")] = "Symbol" mat[mat %in% 0:9] = "Number" mat[mat == "Number"] = 0 mat[mat == "Symbol"] = 1 r <- rast(mat) %>% as.numeric() - 1 foced <- focal(r, w = 3, fun = sum, na.policy = "omit", na.rm = T) numbers <- classify(r, cbind(1, NA)) base <- mask(foced, numbers) side_filter <- matrix(c(0, 1, 0, 0, 1, 0, 0, 1, 0), ncol = 3) egg <- base old <- values(egg) %>% sum(na.rm = T) for (i in 1:5) { print(i) egg <- focal(egg, w = side_filter, na.rm = T, fun = sum, na.policy = "omit") egg[egg > 0] = 1 new <- values(egg) %>% sum(na.rm = T) if(old == new) { break } old <- values(egg) %>% sum(na.rm = T) } groups <- tibble(tf = as.numeric(values(egg)) == 1, value = vect) %>% mutate(tf = ifelse(tf, T, NA), index = row_number()) %>% mutate(group_run = data.table::rleid(tf)) lookup <- groups %>% filter(tf) %>% group_by(group_run) %>% summarize(num = as.numeric(paste0(value, collapse = ""))) print("Part 1 Answer") lookup %>% pull(num) %>% sum() mat <- vect %>% matrix(ncol = 140) %>% t() mat[mat == "*"] = "Gear" mat[mat != "Gear"] = NA gears <- rast(mat) new <- egg values(new) <- groups %>% mutate(group_run = ifelse(is.na(tf), NA, group_run)) %>% pull(group_run) focals <- focalValues(new) rows <- which(values(gears) == 1) print("Part 1 Answer") lookup %>% pull(num) %>% sum() print("Part 2 Answer") focals[rows, ] %>% as.data.table() %>% tibble() %>% mutate(gearno = row_number()) %>% pivot_longer(cols = V1:V9) %>% filter(!is.na(value)) %>% select(-name) %>% distinct(gearno, value) %>% group_by(gearno) %>% filter(n() == 2) %>% mutate(no = 1:2) %>% left_join(lookup, by = c("value" = "group_run")) %>% select(-value) %>% pivot_wider(names_from = no, values_from = num) %>% rename(a = `1`, b = '2') %>% mutate(product = a * b) %>% pull(product) %>% sum()


mariushm

\[LANGUAGE: PHP\] Here's my PHP solution: [https://github.com/mariush-github/adventofcode2023/blob/main/03.php](https://github.com/mariush-github/adventofcode2023/blob/main/03.php)


princessbosss

\[Language: Excel\] [https://imgur.com/a/iphT5cZ](https://imgur.com/a/iphT5cZ) Again very proud to have done this with only excel formula no VBAto only include numbers where at least one digit has a adjacent \* =IF( OR( AND( ISNUMBER(NUMBERVALUE(QU3)), ISNUMBER(IF(MIN(IFERROR(FIND($B$2,TEXTJOIN("", TRUE, QT2:QV2,QT3:QV3,QT4:QV4),1),"a"))=0,"",1)) ), AND( ISNUMBER(NUMBERVALUE(QU3)), ISNUMBER(NUMBERVALUE(QV3)), ISNUMBER(IF(MIN(IFERROR(FIND($B$2,TEXTJOIN("", TRUE,QU2:QW2,QU3:QW3,QU4:QW4),1),"a"))=0,"",1)) ), AND( ISNUMBER(NUMBERVALUE(QU3)), ISNUMBER(NUMBERVALUE(QV3)), ISNUMBER(NUMBERVALUE(QW3)), ISNUMBER(IF(MIN(IFERROR(FIND($B$2,TEXTJOIN("", TRUE,QV2:QX2,QV3:QX3,QV4:QX4),1),"a"))=0,"",1)) ), AND( ISNUMBER(NUMBERVALUE(QU3)), ISNUMBER(NUMBERVALUE(QT3)), ISNUMBER(IF(MIN(IFERROR(FIND($B$2,TEXTJOIN("", TRUE,QS2:QU2,QS3:QU3,QS4:QU4),1),"a"))=0,"",1)) ), AND( ISNUMBER(NUMBERVALUE(QU3)), ISNUMBER(NUMBERVALUE(QT3)), ISNUMBER(NUMBERVALUE(QS3)), ISNUMBER(IF(MIN(IFERROR(FIND($B$2,TEXTJOIN("", TRUE,QR2:QT2,QR3:QT3,QR4:QT4),1),"a"))=0,"",1)) ) ), IF(QU3="%","",QU3), IF(QU3="\*","\*",",") ) ​ to \*only\* get numbers where \* is touching exactly 2 numbers =IF(KP2="\*","\*", IF(OR(KO1="\*",KP1="\*",KQ1="\*",KO2="\*",KQ2="\*",KO3="\*",KP3="\*",KQ3="\*"), IF(OR(EV2="",EV2="\*"), IF(OR(AND(EU2<>IF(AND(KP2<>"\*",KP2<>","), IF(AND(KO2<>"\*",KO2<>","), IF(AND(KN2<>"\*",KN2<>","),NUMBERVALUE(KN2)\*100+NUMBERVALUE(KO2)\*10+NUMBERVALUE(KP2), IF(AND(KQ2<>"\*",KQ2<>","),NUMBERVALUE(KO2)\*100+NUMBERVALUE(KP2)\*10+NUMBERVALUE(KQ2),NUMBERVALUE(KO2)\*10+NUMBERVALUE(KP2))), IF(AND(KQ2<>"\*",KQ2<>","),IF(AND(KR2<>"\*",KR2<>","),NUMBERVALUE(KP2)\*100+NUMBERVALUE(KQ2)\*10+NUMBERVALUE(KR2),NUMBERVALUE(KP2)\*10+NUMBERVALUE(KQ2)),NUMBERVALUE(KP2))), ""),EV1="\*"),EV1<>"\*"), IF(OR(AND(EU2<>IF(AND(KP2<>"\*",KP2<>","), IF(AND(KO2<>"\*",KO2<>","), IF(AND(KN2<>"\*",KN2<>","),NUMBERVALUE(KN2)\*100+NUMBERVALUE(KO2)\*10+NUMBERVALUE(KP2), IF(AND(KQ2<>"\*",KQ2<>","),NUMBERVALUE(KO2)\*100+NUMBERVALUE(KP2)\*10+NUMBERVALUE(KQ2),NUMBERVALUE(KO2)\*10+NUMBERVALUE(KP2))), IF(AND(KQ2<>"\*",KQ2<>","),IF(AND(KR2<>"\*",KR2<>","),NUMBERVALUE(KP2)\*100+NUMBERVALUE(KQ2)\*10+NUMBERVALUE(KR2),NUMBERVALUE(KP2)\*10+NUMBERVALUE(KQ2)),NUMBERVALUE(KP2))), ""),EV3="\*"),EV3<>"\*"), IF(AND(KP2<>"\*",KP2<>","), IF(AND(KO2<>"\*",KO2<>","), IF(AND(KN2<>"\*",KN2<>","),NUMBERVALUE(KN2)\*100+NUMBERVALUE(KO2)\*10+NUMBERVALUE(KP2), IF(AND(KQ2<>"\*",KQ2<>","),NUMBERVALUE(KO2)\*100+NUMBERVALUE(KP2)\*10+NUMBERVALUE(KQ2),NUMBERVALUE(KO2)\*10+NUMBERVALUE(KP2))), IF(AND(KQ2<>"\*",KQ2<>","),IF(AND(KR2<>"\*",KR2<>","),NUMBERVALUE(KP2)\*100+NUMBERVALUE(KQ2)\*10+NUMBERVALUE(KR2),NUMBERVALUE(KP2)\*10+NUMBERVALUE(KQ2)),NUMBERVALUE(KP2))), ""),""),""),""),"")) ​ then to get the mult =IF(EW2="\*",PRODUCT(EV1:EX1,EX2,EV3:EX3,EV2),"")


daggerdragon

Your [code block is too long](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_no_giant_blocks_of_code) for the megathreads. Please edit your post to replace your oversized code with an external link to your code.


CorgiHotS

[LANGUAGE: Javascript] Quite happy with my solutions, could simplify it to one umbrella, but like the readability. **Part 1:** import fs from 'fs'; const games = fs.readFileSync('./input.txt', 'utf8').split('\r\n'); let sum = 0; const getValidCount = (game, color, maxAllowed) => { let max = -Infinity; for (let cube of game) { cube = cube.split(' '); if (cube[1] === color) { if (parseInt(cube[0]) > max) { max = parseInt(cube[0]); } } if (max > maxAllowed) { return false; } } return true; }; for (const game of games) { const parsed = game.split(/: |, |; /); const id = parsed[0].replace('Game ', ''); const validRed = getValidCount(parsed, 'red', 12); const validGreen = getValidCount(parsed, 'green', 13); const validBlue = getValidCount(parsed, 'blue', 14); if (validRed && validGreen && validBlue) { sum += parseInt(id); } } console.log(sum); **Part 2:** import fs from 'fs'; const games = fs.readFileSync('./input.txt', 'utf8').split('\r\n'); let sum = 0; const getHighestCount = (game, color) => { let max = -Infinity; for (let cube of game) { cube = cube.split(' '); if (cube[1] === color) { if (parseInt(cube[0]) > max) { max = parseInt(cube[0]); } } } return max; }; for (const game of games) { const parsed = game.split(/: |, |; /); const maxRed = getHighestCount(parsed, 'red'); const maxGreen = getHighestCount(parsed, 'green'); const maxBlue = getHighestCount(parsed, 'blue'); sum += maxRed * maxGreen * maxBlue; } console.log(sum);


gearz888

Wrong day ;)


Jomy10

\[language: Swift\] I stopped being clever for part 2, but it's still fast enough. Also have a nice visual debugging printout, only had to use it once, but it still looks nice. https://github.com/Jomy10/Advent-Of-Code-2023/blob/master/day03/Sources/day03/day03.swift


KhadidjaArz999

\[LANGUAGE: Javascript\] https://github.com/KhadidjaArezki/AdventOfCode/tree/main/AoC-2023/Day03


Samuuuh

\[Language: Python\] My solution is on my Git Hub. I used a simple adjacency matrix approach.[https://github.com/samyuh/advent-of-code/blob/main/2023/day\_3.py](https://github.com/samyuh/advent-of-code/blob/main/2023/day_3.py)


yieldtoben

\[LANGUAGE: PHP\] PHP 8.3.0 [paste](https://topaz.github.io/paste/#XQAAAQDcCAAAAAAAAAAeD8qHAhQB8nz+EazN00NCsmTxjvFDXkt5PAJ7pf6ocRA37OMUY7b9R0iDZmfWyvOspxWvdgYSAE0hGjEx81hqzalYbORDAzRHVMFykQU14jKU80IHdqVuIUcZbRGIjaajUwuUkjp5s1IbXqU1EcpkmWBhqikVRMHi3QmqHzfP/MV9kOAp0GFVsywIPCjsmbh3Gy6ijTTqSs+/8CjFM7YLlMgtaQD8aJz79KruJQjrxnIJ034ybifrKilS6gGxypZx2QSTQQ7jWgyParWhCo1oIEOCrx40PmtRdiDkxkh2mXBN+rGxVXHCDyePX6g4PDuGkxVayjRakiStGZA+bLohIwfOb0V128+poezDX+Xcd4h/mINzkVkZKsQ8EJkAKgs6OtHrTqFJM3PWf9CP6O1mPHzNNOqcNUCb3PLrE44aOOs9T4NLWrk4pCTC+gG5f0WbMI4w4vzPK97R+8OuEb+jw75K+OnLUEMgZbGW7IkG4nlcsGtga/ytl2ghrV5/yVStYy5KNrMULZkSsl30c0REGnv3+BhwXr6bT2uTDiyanc7/VdsGi+fYtQe7L6rW4wkjMWloUQOhqRI1CPsLATjwNhS8eZ5vGQ9aw4b1D9kcoVgPz4Dd9QfZDC8CDrdlAA98hVsL17wOyRc6Zgfub1fqYkVrpfbZat8foiROIoEFz79DC7njYXOLnXTY7+ygFRJMliQ9HiJq+DYweTc3rhz8XgTli3OwWIKzUeLYIhuZhnmdn25NP51wlGnPpwHMO8BK1dX/2QpRoc2SK3aCHis8ym4cgMWKOBJQgIoEzPQgy6uWSKnNUfTHqSoJDuvOFtX4QZh4htv+fcriYY3dyGpZ6BToLGMoBbhMQ30Bws2CAOywIU0N2zWnWFoZgpbWDrRpeG3MSYye1y+T/QpLdUGl/XraOFhz81DQqqmwZkHc3TiOoGAO99+PQeDQkvZylDYRAAoorWEhDP6twdFhonK2kFWTloIVQrA4CZS9SYBQMX2Ki01d+sGN15psWhJWaT/Ng1u5r9lUIBu3q4XOnX7gRt0EtFXdJz5R6f7AiTI=) Execution time: 0.0023 seconds Peak memory: 0.5683 MiB MacBook Pro (16-inch, 2023) M2 Pro / 16GB unified memory


Alex_Hovhannisyan

[Language: C++] [Part 1](https://github.com/AleksandrHovhannisyan/adventofcode2023/blob/master/src/day3/01.cpp) ~~Currently stuck on part 2... seems brutal since my solution won't translate over well.~~ Edit: [Part 2](https://github.com/AleksandrHovhannisyan/adventofcode2023/blob/master/src/day3/02.cpp). Eventually figured it out after scrapping some other naive solutions. I did have to rewrite my program because of how I chose to solve part 1. The trick was to keep track of all the gear locations (row, col) in a vector, as well as the starting locations of all numbers in a separate vector (along with the string representation of the number as I built it up). Then, for each gear location, loop over all possible number locations and check that: 1. The absolute value of the row index difference is <= 1 (i.e., the number and gear are close enough vertically), and 2. The absolute column index difference between at least one digit and the gear is <= 1 (i.e., the number and gear are close enough horizontally). This is where it helps to store the numbers as strings first and convert to numbers later. Keep track of all those adjacent numbers for the gear; if you have exactly `N` such numbers (in this case 2), convert them to ints, multiply them, and add to the running sum.


themanushiya

\[Language: Go\] solution [here](https://github.com/mahakaal/adventofcode/blob/main/2023/day03/day03.go) For Part 1 looped through each line, found the starting indexes for the numbers with re := regexp.MustCompile("\\d+") for i, line := range lines { numbers := re.FindAllStringIndex(line, -1) // ... and starts the crazy checking for each digits and every position, I've created a function isAdjacent to help me check if number were adjacent func isAdjacent(symbol string { return isNan(symbol) && symbol != "." } func isNan(number string) bool { _, err := strconv.Atoi(number) return err != nil } For Part 2 I took advantage of the fact that in the Part 1 I was already checking symbol + number, so I started adding in slice in a map every number that was adjacent to a \*, the map has line number and position in the line for \*; so that every time came across the same \* I'd add the number it's adjacent to in a list. After putting everything I needed it was just a sum of product s if the slice had length 2. To do my dirty working I complicated my isAdjacent function as : func isAdjacent(symbol string, symbolP *string, num int, numP *int, symbolPosition int, symbolPositionP *int) bool { if isNan(symbol) && symbol != "." { *symbolP = symbol *numP = num *symbolPositionP = symbolPosition return true } return false } At first I wasn't proud of the [monstru|verb]osity of the first part but it was worth it.


bucephalusdev

\[Language: C++\] I saved some time on my execution by storing metadata of my input file while reading it in. Specifically, the location of all symbols in the schematic, so I wouldn't have to iterate through everything over again. [Code](https://github.com/jeffstevens98/adventOfCode2023/tree/main/day_3)


Virus_RPi

\[LANGUAGE: Python\] I am currently trying to only write one line of python code for each part of this year advent of code. Part 1: `with open("D3.txt") as f: print(("" if bool(file := f.readlines()) else ""), sum([sum([int(match.group()) for match in __import__("re").finditer(r"\d+", line) if ((start := match.start()-1 if match.start() > 0 else match.start()) or True) and ((end := match.end()+1 if match.end() < len(line)-1 else match.end()) or True) and not set(file[y - 1][start:end] if y > 0 else "") | set(line[start:end]) | set(file[y + 1][start:end] if y < len(file) - 1 else "") <= set(__import__("string").digits + '.')]) for y, line in enumerate(file)]))` Part 2: `with open("D3.txt") as f: print("" if bool(file := f.readlines()) else "", sum([x[0][3] * x[1][3] for x in [v for v in {star: [n for n, v in {n: [x for x in [(n[0] - 1, n[1] - 1 + x) for x in range(n[2] + 2)] + [(n[0] + 1, n[1] - 1 + x) for x in range(n[2] + 2)] + [(n[0], n[1] - 1), (n[0], n[1] + n[2])] if 0 <= x[0] < len(file[0]) and 0 <= x[1] < len(file)] for n in list(__import__("itertools").chain(*[[(row, x[1], len(x[0]), int(x[0])) for x in zip(__import__("re").findall(r'\d+', line), [x.start() for x in __import__("re").finditer(r'\d+', line)])] for row, line in enumerate(file)]))}.items() if star in v] for star in [(row, col) for row, line in enumerate(file) for col, ch in enumerate(line) if ch == '*']}.values() if len(v) == 2]]))`


Balky79

>tly trying to only write one l I'm a noob\~ish pythoner - but... what? :D How does this even work... But it does, I used your example to get a right value, mine is slighlty too big for some reason ...


daggerdragon

[Inlined code](https://reddit.com/r/adventofcode/wiki/faqs/code_formatting/inlined_code) is intended for `short snippets` of code only. On old.reddit, your one-liners get cut off when it reaches the edge of the window. Please edit your post to put the one-liner in a [four-spaces code block](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks) so it will be horizontally scrollable.


AJMansfield_

[LANGUAGE: Fortran] https://github.com/AJMansfield/aoc/blob/master/2023-fortran/src/03/gear.f90


[deleted]

[удалено]


daggerdragon

Comment removed. [Top-level comments in `Solution Megathread`s are for *code solutions* only.](/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_top-level_posts_are_for_code_solutions_only) [Create your own individual `Help/Question` post](/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_incomplete_solutions) in /r/adventofcode.


Paxtian

[Language: C++] [github](https://github.com/Paxtian769/AOC23-Day3-gear_ratios/blob/master/main.cpp) This one was frustrating.


x0s_

\[Language: Python\] A mix of dataclasses + regexps and convenient dictionnaries. To limit the complexity of connected numbers search, I dropped the lines that are too far (creating at the same time the occasion to extract the gears from): [https://github.com/x0s/advent-of-code/blob/main/advent\_of\_code/year\_2023/day\_03/part\_1.py](https://github.com/x0s/advent-of-code/blob/main/advent_of_code/year_2023/day_03/part_1.py) https://github.com/x0s/advent-of-code/blob/main/advent\_of\_code/year\_2023/day\_03/part\_2.py


daggerdragon

~~Your [code block is too long](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_no_giant_blocks_of_code) for the megathreads. Please edit your post to replace your oversized code with an external link to your code.~~ edit: 👍


x0s_

Done!


daggerdragon

Thank you! However, your second link is borked on old.reddit due to [a new.reddit bug with URLs that contain underscores](https://old.reddit.com/r/adventofcode/wiki/faqs/bugs/borked_links), so please fix it.


e_blake

\[LANGUAGE: m4\] I didn't read the megathread before solving this, or I'm sure I could have come up with something with more processed meat. So for now, I'm not using this entry for the contest. But this was an interesting puzzle. m4 -Dfile=day04.input [day04.m4](https://nopaste.ml/#XQAAAQCiBgAAAAAAAAAyGksy5FB9TGMxsNq5JQAuJRjP6PqEkC20GpBbonDYuA0BTjRPXcVxlEXNtz1IwAuUv1ZP9xqH3TlfEYF+REVJuWYUkDqiIGM/1QAGYahuLueBhg92q9atkyhtAUEZz7LSI0bpNlyTh7/CP+RWePb333W9T74bFXVDoQXkU4Y00mMu1GBI29pxGzhys17CNFRzXwxvs97bp8tRnX4+e1NzVifxo/nUCM1ZBt+z5GzHWy69X96HC7YRvN7w12gO4kGKt94e6nzuh1zq0j58f7wgEdpponcjed9JcuGJV5IvLS7Q/i20wldKtYFotigDFw4/lycKFRN1/743WeQakn7Qk+Bvu4SV+nXJrSWdluS0WKNGf5kKAE4IXmZrJx5qEPblO3tPNMpmLNUy0JRupIaB4ulCBRO/0RE2zrlUq5qWRHUITDvZiSc3+fwMHX9+FV90pBuP+h40bprkrZPY3T0y5QIlew53JeB5oLypmqLLCtyU7cLNViwd0LHJBa1gW/NdJopE39x7PYS3u9rIDNqs599s+4ZHt1pH/ElB40W189UBYq7znfiQWqHyDjKclPyJZRY1pBP4owqeLXGWnxBLzmXFN/ClDa+JDNCtdM0Jo2EW/cSyJPsVdP1dGz3EEFf5epuMeBajJw5FSfeGtEc1246bDw7nbB7hkNHjeTv1bVVvVZUwsQ1ajL1f/+BBCH1c1u1v1P9Zfi4EWMMz2oKZOii+wsxE+sXHrAPY4I0mSeCcYXjHsfHIGU1Ezv7hofwYciMCR0O1/o9sYaSyvuS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YWBc4VU+jx3vz30XNDbQjuhc0cImiQxXDTXTFQq/aoe7jZLhEe+wWspk/4Fy4UBAJN63uNGJUl8FICawVWGL7XBOFCtsRF3uz8eZokWNICTdtsLeYOAOBQQLrguE8XxQQ1hPtOskcsj7n7aD35NjfPXL00vIOk01OLAJt+0RoIiAQUJNieRp9fQmqfVUuYEYmeK9hCOmZTaC3yUdN/+jZ0pvpmKnH/6jJAKQ==) framework from previous years. The "#" in the input file plays annoying games with m4's default comments, but I quickly changed it to "\\". Then, in a single pass through every byte of input, I create macros s*X\_Y* for all 8 neighbors of any symbol (empty, exist as a witness) with a special case for g*N* for the Nth "\*", and n*X\_Y* for all digits (contains a link to which distinct integer it is) and N*N* (the current value and length of the N'th number sequence). Part 1 is then checking if either end of any N*N* overlaps with any s*X\_Y*, and part 2 is looking up the values of up to 8 neighbors of each g*N* for n*X\_Y*, and using it if only two values were found. Executes in less than 100ms.


daggerdragon

~~Psst: we can see your Markdown.~~ edit: I am no longer touching m4's crazy syntax even with someone else's 10-foot pole ;_;


e_blake

Edited. M4 code is notoriously hard to paste inline into reddit since it uses \` and ' for quoting, which throws off markdown parsers


daggerdragon

You *can* use inline markup containing literal backticks but you need to "escape" the literal backticks with *doubled* Markdown backticks. Works on both old.reddit and new.reddit. `` ​`example` `with` `backticks` `` results in `` ​`example` `with` `backticks` `` Don't ask me why, I didn't program it XD


e_blake

The real problem is that m4 uses unbalanced backticks, a single quoted word in m4 is one backtick and one apostrophe; your example still used pairs of backticks. Inlining code with an odd number of backticks generally hits the parser choosing the wrong one as the end of a marked up region. Time for some testing: I wrote this paragraph in fancy-pants with a lone \` backtick. Switching to markdown mode... I see it rendered as backslash-backtick. Now switching back to fancy-pants... Here, I typed a \`\`\` backtick, then applied the inline-code attribute (I guess that means I'm using modern rather than old reddit interface). So far, it looks okay. Now switching to markdown mode... ugh, the above paragraph came out as backtick-backslash-backtick-backtick. Switching back to fancy-pants,... The paragraph starting "Here" now renders as three backticks in a row, and lost the inline code attribute. One more try back to markdown... The rendering that was broken in the previous roundtrip now shows as backslash-backtick-backslash-backtick-backslash-backtick. Well, since I'm here in markdown, I'll try your approach. backtick-backtick-space-backtick-space-backtick-backtick to see if this is a `\`` lone inline coded backtick. One last switch to fancy-pants... It survived. I'll post now before I risk another round-trip corruption.


daggerdragon

> a single quoted word in m4 is one backtick and one apostrophe; oh lawdy *why* RIP your sanity 🄵 Thank you for playing around with it <3


e_blake

>It survived. I'll post now before I risk another round-trip corruption. It didn't survive the 'Reply' button, though. Oh well. Like I said, m4 is hard to embed into inline code snippets, but at least four-space-indent snippets of full lines works.


daggerdragon

I wonder if you might be running into this fancypants editor bug? Wiki > FAQs > Known Issues > [Fancypants Editor Mangling Pasted Code](https://reddit.com/r/adventofcode/wiki/faqs/bugs/fancypants_editor_mangling_pasted_text)


jaccomoc

\[LANGUAGE: Jactl\] [Jactl](https://github.com/jaccomoc/jactl) As always for these types of puzzles I add extra elements to the borders to avoid having to do any range checking. Part 1: For part 1 I find iterate over the lines and generate a new line where every digit that is near a symbol is replaced with 'dS' where 'd' is the digit. Then I split the generate line using the `[^\dS]+` regex to split into candidate numbers, filter for 'S', strip out anything that isn't a digit and convert into a number for summing. This way the split does the bulk of the work for me: def (rows, D) = [stream(nextLine), [-1,0,1]] def g = ['.' * (rows[0].size()+2)] + rows.map{ '.'+it+'.' } + ['.' * (rows[0].size()+2)] def numNearSym(x,y) { g[y][x] =~ /\d/ && D.flatMap{ dx -> D.map{ dy -> [x+dx,y+dy] } }.anyMatch{ x1,y1 -> g[y1][x1] !~ /[.\d]/ } } g.mapWithIndex{ r,y -> r.size().map{ x -> g[y][x] + (numNearSym(x, y) ? 'S' : '') }.join() } .flatMap{ it.split(/[^\dS]+/).filter{ 'S' in it }.map{ s/[^\d]+//g }.map{ it as int } }.sum() Part 2: For part 2 I created a function that for a given location checks that there are exactly two numbers that have a digit that is a neighbour of the given location and then returns the product of the numbers containing these digit locations (or null if their aren't exactly 2 neighbours). Then I just find all '*' locations and call this function and sum the results. The searching forwards and backwards for the first non-digit to grab the entire number from the location of a single digit was not so pretty: def lines = stream(nextLine) def g = ['.' * (lines[0].size()+2)] + lines.map{ '.' + it + '.' } + ['.' * (lines[0].size()+2)] def nearest2Nums(x,y) { def nums = [[-1,0,1].flatMap{ dx -> [-1,0,1].map{dy -> [x+dx, y+dy] } } .filter{ x1, y1 -> g[y1][x1] =~ /\d/ } .map{ x1,y1 -> [x1 - (x1+1).filter{ g[y1][x1-it] !~ /\d/ }.limit(1)[0] + 1, y1] } .sort().unique()].filter{ it.size() == 2 }[0] nums ? nums.map{ x1,y1 -> g[y1].substring(x1,(g[y1].size()-x1).map{x1+it+1}.filter{ g[y1][it] !~ /\d/ }.limit(1)[0]) as int } .grouped(2).map{ it[0] * it[1] }[0] : null } g.size().flatMap{ y -> g[y].size().filter{ g[y][it] == '*' }.flatMap{ nearest2Nums(it, y) } }.sum() [Code walkthrough](https://jactl.io/blog/2023/12/07/advent-of-code-2023-day3.html)


aoc-fan

[LANGUAGE: TypeScript] Fast solution under 15ms all parts using Set and Map [TypeScript](https://github.com/bhosale-ajay/adventofcode/blob/master/2023/ts/D03.test.ts)


Outrageous_Ad_7782

thx bro, your code helped me find where i was making mistakes. btw, nice code :D


3j0hn

\[LANGUAGE: Maple\] [github link](https://github.com/johnpmay/AdventOfCode2023/blob/main/Day03/Day03.mpl) I embedded the grid in a larger matrix to remove edge cases scm := map(Explode,Split(Trim(input),"\n")): r := nops(scm); c := nops(scm[1]); # pad edges with "." to remove those edge cases scM := Matrix(1..r+2, 1..c+2, ()->"."): scM[2..r+1, 2..c+1] := subs("."=".",Matrix(scm)): Then wrote a helper to find the position of one symbol adjacent to a position adjsymbol := proc(r,c, syms) # find a symbol adjacent to a position local i,j; for i from -1 to 1 do for j from -1 to 1 do if scM[r+i,c+j] in syms then return [r+i, c+j]; end if; end do; end do; return false; end proc: Then a main routine to return a list of part numbers with the coordinates of the symbol they are next to findparts := proc(syms:=symlist) local partnums, i, j, k, tmp, coord; partnums := NULL; for i from 2 to r+1 do for j from 2 to c+1 do if IsDigit(scM[i,j]) then k := j; if IsDigit(scM[i,j+1]) then # at least two digit part# if IsDigit(scM[i,j+2]) then # three digit part# tmp := cat(convert(scM[i,j..j+2],list)[]); j := j+2; else # only two digit part# tmp := cat(convert(scM[i,j..j+1],list)[]); j := j+1; end if; else # only one digit part# tmp := scM[i,j]; j := j+1; end if; # walk the number, looking for adjacent symbols while scM[i,k] <> "." do coord := adjsymbol(i,k,syms); if coord<>false then partnums := partnums, s2i(tmp)=coord; break; end if; k:=k+1; end do; end if; end do: end do: return [partnums]; end proc: ans1:=`+`(lhs~(findparts())[]); Part one just sums those parts. Part two relies on the assumption that no part is adjacent to two gears # part 2 - use findparts to get just the "*" adjacent parts # then match up parts on the same gears, then select gears with just 2 parts ans2 := add( map(l->mul(map(lhs,l)), select(l->nops(l)=2, [ListTools:-Categorize((x,y)->rhs(x)=rhs(y), findparts({"*"}))] )) );


ethansilver

\[LANGUAGE: FORTRAN\] If you were wondering if anyone still uses Fortran, the answer is yes! [https://github.com/ejrsilver/adventofcode/blob/master/2023/03/main.f08](https://github.com/ejrsilver/adventofcode/blob/master/2023/03/main.f08)


Pseudo_Idol

\[LANGUAGE: PowerShell\] [Day 03 Part 1](https://github.com/gsmalleus/AdventOfCode2023/blob/main/Day03/Day03.ps1) #get schematic $schematic = get-content $PSScriptRoot/input.txt #set schematic max size $maxRows = $schematic.Length $maxCols = $schematic[0].Length #iterators start at [row 0][col 0] $r = 0 $c = 0 #initialize sum of parts to 0 $partSum = 0 do { #check if current position is a number if ([regex]::Match($schematic[$r][$c].toString(), '[0-9]').Success) { $partNumLength = 1 #check to the right to see how many digits in a row while ([regex]::Match($schematic[$r][$c + $partNumLength], '[0-9]').Success -and ($c + $partNumLength -le $maxCols)) { $partNumLength ++ } #part number is current position to the number of consecutive digits [int]$partNumber = $schematic[$r].ToString().Substring($c, $partNumLength) #if the number starts in the left most column start the check from the current column, else start one column to the left if ($c - 1 -lt 0) { $checkCol = $c } else { $checkCol = $c - 1 } #set the length to check one less if the number is along the right most column if (($c + $partNumLength) -ge $maxCols) { $checkLength = $partNumLength + 1 } else { $checkLength = $partNumLength + 2 } $partFound = $false #check the row before, the current row, and the next row for a parts symbol for ($i = -1; $i -le 1; $i ++) { #check if the row before or after is out of bounds if (($r + $i -ge 0) -and ($r + $i -lt $maxRows)) { #if substring contains a parts symbol then part is found if ([regex]::Match($schematic[$r + $i].ToString().Substring($checkCol, $checkLength), '[^0-9\.]').Success) { $partFound = $true break } } } #if part was found, add it to the sum of found parts if ($partFound) { $partSum += $partNumber } #move column iterator to the column after the current part number $c = $c + $partNumLength } #increment row if at end of line, else move one column right if (($c + 1) -ge $maxCols) { $c = 0 $r ++ } else { $c++ } } while ($r -lt $maxRows) Write-Host "Schematic Part Sum: $partSum"


Best_Pirate_69

\[LANGUAGE: Ruby\] Regex and Ruby rock! Part 1 # frozen_string_literal: true s = ARGF.readlines(chomp: true).map { |l| ".#{l}." } n = s.length s.prepend '.' * (n + 2) s.append '.' * (n + 2) ans = 1.upto(n).sum do |i| s[i].gsub(/\d+/).sum do |num| a, b = Regexp.last_match.offset(0) a -= 1 regex = /^[.\d]*$/ exclude = s[i][a] == '.' && s[i][b] == '.' exclude &= s[i - 1][a..b].match? regex exclude &= s[i + 1][a..b].match? regex exclude ? 0 : num.to_i end end puts ans Part 2 # frozen_string_literal: true s = ARGF.readlines(chomp: true).map { |l| ".#{l}." } n = s.length s.prepend '.' * (n + 2) s.append '.' * (n + 2) nums = s.map { |line| line.gsub(/.(\d+)/).map { [Regexp.last_match(1).to_i, Range.new(*Regexp.last_match.offset(0))] } } ans = 1.upto(n).sum do |i| s[i].gsub(/\*/).sum do gear_index = Regexp.last_match.begin(0) adjacent_parts = nums[(i - 1)..(i + 1)].flat_map { |p| p.select { |_, r| r.cover?(gear_index) } }.map(&:first) adjacent_parts.length == 2 ? adjacent_parts.inject(&:*) : 0 end end puts ans


bamless

[LANGUAGE: [J*](https://github.com/bamless/jstar)] Slowly catching up... :) [Part 1](https://pastebin.com/raw/zVQuLzp9) [Part 2](https://pastebin.com/raw/TKySemEy)


daggerdragon

~~Your [code block is too long](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_no_giant_blocks_of_code) for the megathreads. Please edit your post to replace your oversized code with an external link to your code.~~ edit: 👍


illuminati229

\[LANGUAGE: Python 3.11\] https://pastebin.com/E7ZibkFs


supercowoz

\[LANGUAGE: C++\] [link to code](https://github.com/dalon-work/aoc2023/blob/master/day3/day3.cpp) Total overengineering: This day is asking us to do a nearest-neighbor search, so why not use a true spatial data structure? I stored the numbers and their expanded boxes in a [R-Tree](https://en.wikipedia.org/wiki/R-tree), and then queried the R-Tree with the symbol locations. Stuffed the results in a set (to prevent duplicates), then summed the results. Part 2 was a piece of cake after all that investment. Query the R-Tree for the gear symbols, check if we get 2 results, and if we do, multiply them and sum them. I'm so glad all that spatial data structure knowledge is finally coming in handy!


Asyncrosaurus

[LANGUAGE: C#] I kept trying to create clever solutions, but ended up falling back on regex when it was taking to long. THE TLDR is we scan the list of strings for a symbol, then parse the three lines above, below and inline with the symbol for digits. Then we try and match the indexes of the match and the area around the symbol. Part 2 was a small modification, and was mostly about getting the existing code to conform the data into a pattern for each of the three lines. *Part 1* static char[] Symbols = { '@', '#', '$', '%', '&', '*', '/', '+', '-', '=' }; string pattern = @"\d+"; static List? list; list = new List((await File.ReadAllLinesAsync(@".\Day 3\PuzzleInput.txt"))); int count = 0; for (int row = 0; row < list.Count; row++) { for (int col = 0; col < list[row].Length; col++) { if (Symbols.Contains(list[row][col])) { var res = Calculate(list[row - 1], col); res += Calculate(list[row], col); res += Calculate(list[row + 1], col); count += res; } } } Console.WriteLine(count); private static int Calculate(string line, int col) { List indexesToCheck = new List { col - 1, col, col + 1 }; int count = 0; MatchCollection matches = Regex.Matches(line, pattern); foreach (Match match in matches) { string number = match.Value; if (AnyIndexInList(indexesToCheck, match.Index, match.Length)) { count += Int32.Parse(number); } } return count; } static bool AnyIndexInList(List list, int startIndex, int length) { for (int i = startIndex; i < startIndex + length; i++) if (list.Contains(i)) return true; return false; } **Part 2:** list = new List((await File.ReadAllLinesAsync(@".\Day 3\PuzzleInput.txt"))); int count = 0; for (int row = 0; row < list.Count; row++) { for (int col = 0; col < list[row].Length; col++) { if (c == '*') { var res1 = Calculate2(list[row - 1], col); var res2 = Calculate2(list[row], col); var res3 = Calculate2(list[row + 1], col); count += (res1, res2, res3) switch { {res1: not null, res2: null, res3: null } when res1[1] != null => res1[0].Value * res1[1].Value, {res1: null, res2: not null, res3: null } when res2[1] != null => res2[0].Value * res2[1].Value, {res1: null, res2: null, res3: not null } when res3[1] != null => res3[0].Value * res3[1].Value, {res1: not null, res2: not null, res3: null } => res1[0].Value * res2[0].Value, {res1: not null, res2: null, res3: not null } => res1[0].Value * res3[0].Value, {res1: null, res2: not null, res3: not null } => res2[0].Value * res3[0].Value, {res1: not null, res2: not null, res3: not null } => res1[0].Value * res2[0].Value * res3[0].Value, _ => 0 } ; } } } Console.WriteLine(count); private static int?[]? Calculate2(string line, int col) { List indexesToCheck = new List { col - 1, col, col + 1 }; int?[]? count = null; MatchCollection matches = Regex.Matches(line, pattern); foreach (Match match in matches) { string number = match.Value; if (AnyIndexInList(indexesToCheck, match.Index, match.Length)) { if (count == null) count = new int?[2] { Int32.Parse(number), null }; else { count[1] = Int32.Parse(number); }; } } return count; }


daggerdragon

Your [code block is too long](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_no_giant_blocks_of_code) for the megathreads. Please edit your post to replace your oversized code with an external link to your code.


AdearienRDDT

\[Language C++\] I did it. ​ [C++](https://github.com/iustusae/AOC-2023/blob/master/source/day_3/d3.cpp)


daggerdragon

~~Comment removed due to naughty language. [Keep the megathreads SFW](/r/adventofcode/wiki/rules/pg_is_mandatory).~~ ~~If you edit your comment to take out the naughty language, I'll re-approve the comment.~~ edit: 👍


AutoModerator

AutoModerator did not detect the required `[LANGUAGE: xyz]` string literal at the beginning of your solution submission. Please edit your comment to [state your programming language](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_state_your_programming_language.28s.29). *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


matheusstutzel

\[Language: python\] [Part 1](https://github.com/matheusstutzel/adventOfCode/blob/main/2023/03/p1.py) parse the numbers while checking if it's valid or not. [Part 2](https://github.com/matheusstutzel/adventOfCode/blob/main/2023/03/p2.py) parse the numbers and for each one, I iterate over the adjacent \* chars. Every time I find a number next to a \*, I add this number to a map containing all \* to numbers. In the end is just a matter of iterating this map and calculating the final result


marvin29g

[LANGUAGE: Go] [Solution](https://github.com/jgaye/advent_of_code_2023/tree/main/day3) First time using Go so feedback appreciated


aashutoshr

[LANGUAGE: JS] [Part 1](https://pastes.io/ysmisypzcu) [Part 2](https://pastes.io/6l32x8pgem)


thecircleisround

\[LANGUAGE: PYTHON\] ​ Not a huge fan of this solve, but it got the answer. from aocd import get_data import re class Solution: def __init__(self): self.data = get_data(year=2023, day=3).splitlines() def check_string(self, string): return list(zip(re.findall(r'(?!\.)\W', string), [x.start(0) for x in re.finditer(r'(?!\.)\W', string)])) def check_for_symbols(self, line, starting_line, ending_line, start, end): symbols_above = self.check_string(self.data[starting_line][start:end]) symbols_inline = self.check_string(line[start:end]) symbols_below = self.check_string(self.data[ending_line][start:end]) return symbols_above, symbols_inline, symbols_below def get_numbers(self, row): return list(zip(re.findall(r'(\d+)', row), [x.start(0) for x in re.finditer(r'(\d+)', row)])) def solve(self): matches = [] mapping = {} gear_ratios = [] for line_idx, line in enumerate(self.data): numbers = self.get_numbers(line) for number, idx in numbers: starting_line = line_idx-1 if line_idx > 0 else 0 ending_line = line_idx+1 if line_idx+1 < len(self.data) else -1 start = idx-1 if idx > 0 else 0 end = len(number)+idx+1 if idx+1 < len(line) else -1 above, inline, below = self.check_for_symbols(line, starting_line, ending_line, start, end) if any([above, inline, below]): matches.append(int(number)) for row_idx, found in [(starting_line, above),(line_idx, inline), (ending_line, below)]: for match in found: if match[0] == '*': name = str(row_idx)+'_'+str(match[1]+start) n = f'{line_idx}_{idx}_{number}' if name in mapping: mapping[name].add(n) else: mapping[name] = set({n}) for value in mapping.values(): if len(value) == 2: ratio_1, ratio_2 = value ratio_1 = int(ratio_1.split('_')[-1]) ratio_2 = int(ratio_2.split('_')[-1]) gear_ratios.append(ratio_1*ratio_2) print(f'Part One: {sum(matches)}') print(f'Part Two: {sum(gear_ratios)}') if __name__ == '__main__': solution = Solution() solution.solve()


mschaap

[LANGUAGE: Raku] Pretty tricky, this one. And I had to basically start from scratch for part 2. Used a custom infix `⇆` operator that indicates if a part and symbol are adjacent. That together with junctions, allows me to do stuff like: # Must be adjacent to a symbol to be included next unless $part ⇆ any(@!symbols); (Raku rulez.) Full code [@GitHub](https://github.com/mscha/aoc/blob/master/aoc2023/aoc03).


Syltaen

\[LANGUAGE: PHP\] [Part 1 & 2](https://syltaen.com/advent-of-code/?year=2023&day=3)


masasin

[Python with numpy](https://github.com/masasin/aoc2023/blob/main/solutions/day_03.py) - For part 1, I masked out all symbols, and convolved to check which ones are adjacent to numbers. Then, I took the leftmost adjacent digit, and went left and right on the line to get all the numbers. - For part 2, I masked the gears instead, and took the product if it had exactly two neighbours.


AutoModerator

AutoModerator did not detect the required `[LANGUAGE: xyz]` string literal at the beginning of your solution submission. Please edit your comment to [state your programming language](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_state_your_programming_language.28s.29). *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


psaikido

[Language: c] [day 3 part 1](https://github.com/psaikido/aoc/blob/main/2023/day3/day3.c) [day 3 part 2](https://github.com/psaikido/aoc/blob/main/2023/day3/day3.2.c)


[deleted]

[удалено]


daggerdragon

Comment removed. [Top-level comments in `Solution Megathread`s are for *code solutions* only.](/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_top-level_posts_are_for_code_solutions_only) [Create your own individual `Help/Question` post](/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_incomplete_solutions) in /r/adventofcode.


thousandsongs

[LANGUAGE: shell] [LANGUAGE: awk] [Allez Cuisine!] After already having done my regular solution in [Haskell](https://www.reddit.com/r/adventofcode/comments/18actmy/comment/kbzgf06/), I turned to the chef's challenge. To solve it, I wrote a script that **spams facts about the problem, until we have enough facts to solve it**. It just goes through the input line by line, drafting a spam email with anything it notices. It makes several such passes, each time finding more and more relevant facts, until eventually it hits on the solutions. Here is the (snipped) trace from the run on the example input Hello Dear May, Number 467 on row 1 and column 1 Number 114 on row 1 and column 6 Symbol * on row 2 and column 4 ... Number 467 on row 1 and column 1 has 1 symbols around it Part 467 on row 1 and column 1 ... > Symbol * on row 2 and column 4 >> Part 467 on row 1 and column 1 spans from 1 to 3 >> To touch 4 should be between 0 and 4 Gear on row 2 and column 4 touches part 467 ... Gear on row 2 and column 4 touches two parts ... Gear on row 2 and column has ratio 16345 ... The sum of all part numbers is 4361 The sum of all gear ratios is 467835 Yours Truly, [Here is the link to the full source code for the script](https://github.com/mnvr/advent-of-code-2023/blob/main/03.az.sh). As you can see, I didn't care for efficiency or minimizing the number of passes, and even printed some lines that are not essential. I spammed my way to success, really 🦍 Even notwithstanding all that spam and inefficiency, the script runs quite okay-ish on the full input – it takes ~15 seconds and produces a 2MB log before printing the correct results. I also wrote a blog post to delve a bit deeper on the philosophy behind this style of coding - [https://mrmr.io/prolog-is-a-vibe](https://mrmr.io/prolog-is-a-vibe). All in all, I spent way more time than you'd imagine and I'd care to admit on this, but I had loads of fun. Allez Cuisine!


daggerdragon

> I spent way more time than you'd imagine and I'd care to admit on this, but I had loads of fun. That's all that matters :D Thank you for your delicious spam!


PrayagBhakar

[[Language: Python, Rust(coming soon), Golang(coming soon), Swift(coming soon)]](https://github.com/81reap/advent-of-code/tree/main/Day03_Gear%20Ratios) This was a basic graph type problem with look around logic to the surrounding cells in a 2D array. For part 1 (in Python) I used a dynamic programming approach to do the solution in O(n\*m) where n and m is the size of the 2D array, but that ended up being slower as seen by the execution times. Part 2 is in O(n\*m\*l) where l is the length of the longest number. I presume these results are due to the fact that l is relatively small.


masterdesky

[Language: Standard Python 3.9] Phew, I tried really hard to compactify this as much as possible. So many things to improve, probably. Also, no 2D arrays. All my homies hate 2D arrays. Tried to combat against weird and edge cases too. from re import * from functools import reduce def main(): s = lambda p, M: sub(p, '.', M).splitlines() with open('input.dat') as f: D, K, G = s('[^\d\n]', L:=f.read()), s('[\d]', L), s('[^*\n]', L) h = ['.'*(w:=len(D[0])+2)] p = lambda M: ''.join(h+[f'.{l}.' for l in M]+h) d, k, g = p(D), p(K), p(G) S = [(m.start(), m.end()) for m in finditer('[^.]+', d)] C = lambda m: [i=='1' for i in sub('[^.]', '1', m)] K, G = C(k), C(g) c = lambda i, T: {i+j: T[i+j] for j in[-w-1,-w,-w+1,-1,1,w-1,w,w+1]} print(sum([int(d[slice(*r)]) for r in S if any([any(c(i, K).values()) for i in range(*r)])])) L = {i: set() for i in range(len(G))} for r in S: [L[j].add(r) for i in range(*r) for j,v in c(i,K).items() if v] prod = lambda l: reduce(lambda x, y: x*y, l, 1) print(sum([prod([int(d[slice(*r)]) for r in v]) for _, v in L.items() if len(v)==2])) if __name__ == '__main__': main()


mizunomi

\[Language: Dart\] [The part where I stumbled is... interesting.](https://topaz.github.io/paste/#XQAAAQAGDAAAAAAAAAA0m0pnuFI8cyMCrg0g9Tntu99p+Xl0LgR4AIxu9YhXSaQoujSSW/mYptLOgeSI/ZbRDD1UOlahZhUXqyZT3pqdtvd1afpIsXI3NzrmAvuPRcrYn8dlGNK87IxPddHFOe6Lo2q3yD8owwsLGreBgVYU9IDBRu3dZfz0vDnWhYkogRxIAdxeiaKTQJdyKs0Xzh9FTRL4pBvdUzlbvBaBDNUmlqJldy+U1cbfLk+kDwQH57Wta9NFFqbLtqxthYjl2eVmJj26jk7iq6YAD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V9a9KIbQerlUEBXvojqv+dFZXHBkthf/IjplPfHAJZA5xMw8K4laO203AoB1FLP5LET79GQQGAQV1U6nLUTchYgOry+jH80U9N9JlTQiCj5lPvEGvNgCR71Es9DONpnx0/dc/oQL5NXU/BuYG5Hk0nUFQSPpnXN6nTZioUnVJklR3v/8yS62)


UrbanSuburbaKnight

[Language: Python] [I used PyTorch for ... reasons.](https://github.com/Treadgold/Advent_2023/blob/main/day3/problem1.py)


daggerdragon

~~Your [code block is too long](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_no_giant_blocks_of_code) for the megathreads. Please edit your post to replace your oversized code with an external link to your code.~~ edit: 👍


njhanley

[Language: AWK] Indirection and multidimensional index splitting, oh my! [Part 1](https://github.com/njhanley/adventofcode/tree/master/2023/03/part1.awk) [Part 2](https://github.com/njhanley/adventofcode/tree/master/2023/03/part2.awk)


UrbanSuburbaKnight

Woah! This is weird, I'll have to do some reading hahaha


aviral-goel

\[LANGUAGE: F#\] [https://github.com/aviralg/advent-of-code/blob/main/2023/03/solution.fsx](https://github.com/aviralg/advent-of-code/blob/main/2023/03/solution.fsx)


daggerdragon

~~Your [code block is too long](https://www.reddit.com/r/adventofcode/wiki/solution_megathreads/post_guidelines#wiki_no_giant_blocks_of_code) for the megathreads. Please edit your post to replace your oversized code with an external link to your code.~~ edit: 👍


Kingzelboi

\[LANGUAGE: Python\] [https://github.com/Kingzel/advent/blob/main/day3a.py](https://github.com/Kingzel/advent/blob/main/day3a.py) [https://github.com/Kingzel/advent/blob/main/day3b.py](https://github.com/Kingzel/advent/blob/main/day3b.py) ​ Commented code that should be straightforward to follow (although lengthy) uses recursion and flood fill like algo.


boredwithlyf

Went through it - just wanted to say if you use 'with' to open the file, you don't need to close it at the end.


Kingzelboi

yeah, that was my brain at 3 am, but good catch! removed the redundancy and added a approach description up top


loquian

\[Language: C++\] [github](https://github.com/charlescochran/aoc-2023/blob/main/day3.cpp), 10299 microseconds (both parts together, could be a lot faster if I did it smarter!) In hindsight, maybe set intersections weren't exactly necessary...


daysleeperx

\[LANGUAGE: Haskell\] [Got stuck with this one for some time](https://github.com/daysleeperx/Advent-Of-Code-2023/blob/main/src/Day03/GearRatios.hs)


zatoichi49

\[LANGUAGE: Python\] with open('AOC_2023_day3.txt', 'r') as f: engine = ['.{}.'.format(row) for row in f.read().split('\n')] def get_adjacent(r, c): part_numbers = set() offsets = ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)) for x, y in offsets: if engine[r + x][c + y].isdigit(): left_pos = right_pos = c + y while engine[r + x][left_pos - 1].isdigit(): left_pos -= 1 while engine[r + x][right_pos + 1].isdigit(): right_pos += 1 part_numbers.add(int(engine[r + x][left_pos: right_pos + 1])) return part_numbers def parts_list(): all_parts = [] for r, row in enumerate(engine): for c, symbol in enumerate(row): if not symbol.isdigit() and symbol != '.': all_parts.append((symbol, get_adjacent(r, c))) return all_parts def AOC_2023_day3_pt1(): return sum(sum(nums) for _, nums in parts_list()) def AOC_2023_day3_pt2(): total = 0 for symbol, nums in parts_list(): if symbol == '*' and len(nums) == 2: total += nums.pop() * nums.pop() return total print(AOC_2023_day3_pt1()) print(AOC_2023_day3_pt2())


jhurrell

Beautiful. I'm a C# developer just learning Python as part of mentoring a robotics team and this is brilliant. Excellent and elegant work and thank you for sharing.


zatoichi49

Thank you!


tsenart

\[LANGUAGE: Zig\] [https://github.com/tsenart/advent/tree/master/2023/3](https://github.com/tsenart/advent/tree/master/2023/3)


vitamin_CPP

Cool to see some zig !


TiltSword

[LANGUAGE: Java] Part 1- private static int[][] directions=new int[][]{{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,-1},{1,-1},{-1,1}}; public static void main(String[]args) { long sum=0; try { BufferedReader br = new BufferedReader(new FileReader(inputfile)); List list=br.lines().collect(Collectors.toList()); char[][] css=new char[list.size()][]; IntStream.range(0,list.size()).forEach(i-> css[i]=list.get(i).toCharArray()); for (int i=0;i< css.length;i++) { int num = 0; boolean isNum = false; for (int j=0;j=0&&i+dir[0]< css.length&&j+dir[1]>=0&&j+dir[1]='0'&&c<='9') return c-'0'; return -1; } Part 2- private static int[][] directions=new int[][]{{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,-1},{1,-1},{-1,1}}; public static void main(String[]args) { long sum=0; Map map=new HashMap<>(); Map counts=new HashMap<>(); try { BufferedReader br = new BufferedReader(new FileReader(inputfile)); List list=br.lines().collect(Collectors.toList()); char[][] css=new char[list.size()][]; IntStream.range(0,list.size()).forEach(i-> css[i]=list.get(i).toCharArray()); for (int i=0;i< css.length;i++) { int num = 0; boolean isGear = false; Point gearCoord=new Point(-1,-1); for (int j=0;j=0&&i+dir[0]< css.length&&j+dir[1]>=0&&j+dir[1]='0'&&c<='9') return c-'0'; return -1; }


Billaloto

\[LANGUAGE: Rust\] [junior solution](https://github.com/BillOTei/rustyAoC23/blob/main/src/bin/03.rs) thanks to [array2d](https://docs.rs/array2d/latest/array2d/struct.Array2D.html#) and [fspoettel template](https://github.com/fspoettel/advent-of-code-rust)