Unable to load image

Advent of Code day 3: you guys had ONE JOB edition

I get out of my certification exam and there's only one thread and they put the wrong day so the admins couldn't find it.

This is why we can't have anything nice. I'll just copy yesterday's OP

Summary for those just joining us:

Advent of Code is an annual Christmas themed coding challenge that runs from December 1st until christmas. Each day the coding problems get progressively harder. We have a leaderboard and pretty good turnout, so feel free to hop in at any time and show your stuff!

Whether you have a single line monstrosity or a beautiful phone book sized stack of OOP code, you can export it in a nice little image for sharing at https://carbon.vercel.app

What did you think about today's problem?

https://adventofcode.com/2023

Our Code is 2416137-393b284c (No need to share your profile, you have the option to join anonymously if you don't want us to see your github)

16
Jump in the discussion.

No email address required.

This took me so long :marseyitsover: Luckily part two was a simple change

import re
with open("3.txt") as infile:
   data = infile.read()
data = data.splitlines()

gears = {}

def is_part(row, start, end):
   for r in range(row-1, row+2):
       for c in range(start-1, end+1):
           try:
               char = data[r][c]
           except IndexError:
               continue
           if char.isdigit():
               continue
           if char == '.':
               continue
           if char == '*':
               return True, (r, c)
           return True,0
   return False,0

part_one = 0
part_two = 0
for row, line in enumerate(data):
   for match in  re.finditer(r'(\d+)', line):
       c1, c2 = match.span()
       p, g = is_part(row, c1, c2)
       if p:
           part_one += int(match.group(0))
       if g:
           gears.setdefault(g, set()).add(int(match.group(0)))

for nums in gears.values():
   if len(nums) == 2:
       part_two += (nums.pop() * nums.pop())
print(f'{part_one=}')
print(f'{part_two=}')

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.