#Day 3 Gear Ratios

1 messages · Page 1 of 1 (latest)

thorn sparrow
prisma dock
#

this is the stupidest fucking engine schematic ive ever seen

#

elves are terrible engineers

thorn sparrow
#

kek no surprise, they ruin christmas every year

tall lance
#

Oh another parsing exercise..

nova meteor
#

Wow. This problem looks hard enough to do in a normal language.

still trout
#

Oh honey new the AoC problem is up

#

I had sleep plans but I guess I can be up when it's almost morning

#

hooooly mother

#

More parsing indeed

nova meteor
#

If they keep on this trajectory, I might have to drop out now. I'm pretty sure I could do this, but these are taking me hours and I really need to do other things.

prisma dock
#

historically speaking, they definitely will

nova meteor
#

But they feel a lot harder this year. Unless I'm just remembering poorly.

still trout
#

It's my first time doing AoC but so far this is the only one that looks fairly difficult

worn ingot
#

i love string manipulation so much

thorn sparrow
#

both parts C++

#

I should make an mdspan LayoutPolicy to handle oob access for these

tall lance
#

I need a shower after writing this code. At least it's done.

haughty steppe
#

both parts in python
||

#!/usr/bin/env python3

import math


def getnum(s: str, i: int) -> int:
    l, r = i, i
    while (0 <= l - 1) and (s[l - 1].isdigit()):
        l -= 1
    while (r + 1 < W) and (s[r + 1].isdigit()):
        r += 1
    return int(s[l : r + 1])


p1, p2 = 0, 0
adj = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
G = open("input.txt").read().splitlines()
H, W = len(G), len(G[0])
for y in range(H):
    for x in range(W):
        sym = G[y][x]
        if sym in ".0123456789":
            continue
        nums = set()
        for i, j in adj:
            i, j = i + y, j + x
            if G[i][j].isdigit():
                nums.add(getnum(G[i], j))
        p1 += sum(nums)
        p2 += math.prod(nums) if len(nums) == 2 and sym == "*" else 0
print(p1, p2)

||

#

^ won't work if there are multiple numbers of same value adjacent to a symbol .. e.g. 123$123

quartz badge
knotty ermine
#

both parts, Kotlin

#

I love Advent of Parsing

still trout
#

Only part 1 for now, in Mr. Krabs' language, would not recommend looking because it was made in only 30min and it's just horrible. will fix it after I sleep 😴

inner coyote
#

Abusing LINQ
||```cs
var nums = world.Where(n => n.typ == otype.num);
var sum2 = world.Where(c => c.typ == otype.sym && (char)c.contents == '*')
.Select(c => c.left_coord)
.Select(g => nums.Where(n => n.surrounding().Contains(g)).Select(n => n.contents).ToArray())
.Where(gn => gn.Length == 2)
.Select(gn => gn[0] * gn[1])
.Sum();
Console.WriteLine("Part 2: " + sum2);

still trout
#

Just looked at part 2, will have to rewrite my code anyway because it sucks LOL

tall lance
still trout
#

I guess I just wanted to make a dumb and naive solution to validate my first idea

knotty ermine
#

improved simplified solution

haughty steppe
#

@knotty ermine for part2 .filter shouldn't it be .. numbers.count() == 2 ?

#

nevermind, just checked my own input ... there are no symbols that have more than 2 adjacent numbers

knotty ermine
#

technically yes but yeah it never happens in the input

#

||I’m surprised there’s no gotcha in the input where a number is attached to two gears||

haughty steppe
#

my script would be ok with that, .. but wouldn't be able to handle this || 123*123|||

quartz badge
haughty steppe
#

😨 that would be cruel

quartz badge
#

hehe cruelty wouldn't stop AoC from doing it.

haughty steppe
#

then we should also add numbers that span vertically 😂

quartz badge
#

ha yeah I feared that too! since again, the instructions didn't mention them one way or another.

tall lance
#

Something like that will definitely happen further down the month.. It always does

quartz badge
#

I'm just wondering when the first graph-theory problem will happen.

tall lance
#

We'll get some BFS/Dijkstra again probably too. At least now I have a nice crate for that 🙂

#

Remainder theorem..

#

This is what I mean by it gets easier the more years you do 😆 You start recognizing the problems

magic spindle
#

Rust day today

brittle quiver
#

wow my code for this is a mess

tall lance
#

Is that Rust with some custom macro wrapper?

brittle quiver
thorny musk
#

The problem from the first day is still the hardest one yet tbh

shell edge
#

ugly haskell part 1 and 2

still trout
#

Finally woke up and finished part 2, too long to post here lol. Will improve later

magic spindle
#

Colleague was really stuck on this one so I helped (||added a if after a loop||) because I was starting to feel bad cringeharold He did day 4 easily though

#

Although it sounds like an easy fix, that was not easy to reason out

#

Not with the way he structured the code