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.

tripleposting my comment.... do you think you passed the exam?

First a hint: my input parser was broken because I forgot that a number could end at the end of the line

Here's my solution, the two functions left off are an ugly parser which spit out sets containing tuples (symbol, i, j) and (value, i, range[j]) and to_search(i,j) spits out adjacent squares (I did skip 0,0 but just 1-indexed instead of checking -ves, using max(x-1, 0. is kewl). If I was smarter I would've used a map/dict somehow, which would've made .contains() viable which might've been more performant, but having to search by symbol for p2 would negate that probably. On the bright side I'm learning to use iterators over loops which might help my ongoing transition to Rust programmer.

https://i.rdrama.net/images/17015920751163921.webp

Jump in the discussion.

No email address required.

pls post the ugly functions I wanna see it

Jump in the discussion.

No email address required.

late, but 4u https://i.rdrama.net/images/17016894359648337.webp

Jump in the discussion.

No email address required.

thank you :marseyexcited:

Jump in the discussion.

No email address required.

wtf is a fold


:!marseybooba:

Jump in the discussion.

No email address required.

The docs have an autie explanation with steps which might help - https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.fold

But I would just say that return nums.fold(1, |n, acc| n + acc)

would be a bit like

acc = 1
for n in nums {
   acc = n + acc
} 

return acc

and fold with default value is called reduce which sounds more like something recognisable

Jump in the discussion.

No email address required.

Yeah that's exactly what javascript reduce does

Jump in the discussion.

No email address required.

Short for "femaloid".

Jump in the discussion.

No email address required.

I wasted a lot of time because first I stored the numbers and symbols in a stupid way, and my first "solution" worked on the small example they gave but not on the data on the big file, there were some weird edge cases I didn't count, but I managed to overcome it and finally store symbols and numbers separately, and the second task ended up being much easier

https://i.rdrama.net/images/17016037740635526.webp

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.

https://i.rdrama.net/images/1701611432517094.webp

The edge case of a number being at the end of each line, annoyed for wait to long, eventually just added an additional dot to every line

Jump in the discussion.

No email address required.

https://i.rdrama.net/images/17016152645606391.webp

Difficulty progression seems about the same as last year. I think a lot of people thought it might be harder this year after day 1.

Jump in the discussion.

No email address required.

I had to parse the digits :marseycheckem2: by hand because I couldn't get the findBounds to work in Nim :/.

Step 1: I drew a 3x3 square :marseyminimalism: around every symbol, then for every number I checked if it was within a square.

https://i.rdrama.net/images/17016073013043065.webp

Step 2: I drew a rect around every number, then for every symbol :marseyokay: I checked if it was within two rects.

https://i.rdrama.net/images/17016073018145485.webp

Jump in the discussion.

No email address required.

Solving problems with NES collision detection algorithm :marseycopter:

Jump in the discussion.

No email address required.

Tried to improve on this, but I couldn't really get anywhere. (77 lines total)

https://i.rdrama.net/images/17016226810302017.webp

This time, I'm using parseUInt from parseutils. strutils throws when it finds an invalid character, parseutils simply returns what it found.

Jump in the discussion.

No email address required.

Man you are fricking crazy!

I love that you keep it linear and just add or subtract an entire row to go to the same point one line below or above, took me forever to figure out. Also didn't know you could do ranges over chars, that's neat. Also had no idea there was a "result" variable defined automatically for all procs.

I don't know if you know and prefer the str.len-1 way, but you can index iterables with [^1], [^2], ... to get items from the end.

You could also do this for the mask, that way you don't need the comments or the superfluous items to keep the indexing right:

https://i.rdrama.net/images/17016315629559743.webp

Though I guess it's a matter of style.

Jump in the discussion.

No email address required.

@TriHard What you got today?

Jump in the discussion.

No email address required.

:#marseygift:

Jump in the discussion.

No email address required.

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