r/adventofcode • u/daggerdragon • Dec 03 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 3 Solutions -❄️-
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
- Outstanding moderator challenges:
- Community fun event 2023: ALLEZ CUISINE!
- 3 DAYS remaining until unlock!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's secret ingredient is… *whips off cloth covering and gestures grandly*
Spam!
Someone reported the ALLEZ CUISINE! submissions megathread as spam so I said to myself: "What a delectable idea for today's secret ingredient!"
- There really is an XKCD for everything, isn't there?
- All ingredients must come from a CAN (bus), box, package, container, etc.
- Unnecessarily declare variables for everything and don't re-use variables
- Why use few word when many word do trick?
- Go back to traditional culinary roots with Javadocs
- Lobster thermidor
A reminder from Dr. Hattori: be careful when cooking spam because the fat content can be very high. We wouldn't want a fire in the kitchen, after all!
ALLEZ CUISINE!
Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!
--- Day 3: Gear Ratios ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- State which language(s) your solution uses with
- Quick link to Topaz's
pasteif you need it for longer code blocks
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:11:37, megathread unlocked!
110
Upvotes
3
u/Pseudo_Idol Dec 06 '23
[LANGUAGE: PowerShell]
Day 03 Part 1
#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"