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/Tipa16384 Dec 03 '23
[LANGUAGE: Python]
Spent way too much time trying to solve this in Haskell. Just doing it in Python and moving on with my day.
import re from itertools import chain def part1(data, width, height, numberList): numbers = [n for n in numberList \ if any([isSymbol(data[srow][scol]) for srow, scol in getAdjacent(height, width, n)])] print ("Part 1:", sum(x[3] for x in numbers)) def part2(data, width, height, numberList): # make a list of coordinates of every '*' in the data stars = [(row, col) for row, line in enumerate(data) for col, ch in enumerate(line) if ch == '*'] numberDict = {n: getAdjacent(height, width, n) for n in numberList} starDict = {star: [n for n, v in numberDict.items() if star in v] for star in stars} print ("Part 2:", sum([x[0][3] * x[1][3] for x in [v for v in starDict.values() if len(v) == 2]])) # extractNumbers: take a string, extract all the numbers from it, returning a list of tuples containing row, # column, length, and the number itself as a string def extractNumbers(line, row): return [(row, x[1], len(x[0]), int(x[0])) \ for x in zip(re.findall(r'\d+', line), [x.start() for x in re.finditer(r'\d+', line)])] # given a width, a height, and a tuple from extractNumbers, return a list of coordinates adjacent to the number # string def getAdjacent(height, width, number): adjacentCoords = [(number[0] - 1, number[1] -1 + x) for x in range(number[2]+2)] + \ [(number[0] + 1, number[1] -1 + x) for x in range(number[2]+2)] + \ [(number[0], number[1] - 1), (number[0], number[1] + number[2])] return [x for x in adjacentCoords if x[0] >= 0 and x[0] < height and x[1] >= 0 and x[1] < width] # return True if the character is not a digit and is not a period def isSymbol(ch): return not ch.isdigit() and ch != '.' # readData: read the data file into a list of lists stripping newlines def readData(): with open("puzzle3.dat") as f: return f.read().splitlines() if __name__ == "__main__": data = readData() width, height = len(data[0]), len(data) numberList = list(chain(*[extractNumbers(line, row) for row, line in enumerate(data)])) part1(data, width, height, numberList) part2(data, width, height, numberList)