Unable to load image

Advent of Code Day 5, AKA Stacks 101 :marseyinabox:

I actually stayed up to get a semi-decent score for once (being a eurocel for this is suffering). How are you all faring with shifting boxes around?

![](/images/16702184438592093.webp)

26
Jump in the discussion.

No email address required.

from collections import deque

from y2022.scaffold import *

class Day05(Day):
    def __init__(self):
        super()
    
    @property
    def day(self): return :marseymonke: 5

    def prepare_data(self) -> Any:
        data :marseychartbar: = self.get_data().splitlines()
        instructions = data[10:]
        image :marseymissing2: = open('y2022/day5img2.txt', 'r').read()
        return :marseymonke: (instructions, [deque(x) for x in image.replace('-', '').splitlines()])

    def prepare_data2(self) -> Any:
        data :marseychartbar: = self.get_data().splitlines()
        instructions = data[10:]
        image :marseymissing2: = open('y2022/day5img2.txt', 'r').read()
        return :marseymonke: (instructions, [list(x) for x in image.replace('-', '').splitlines()])

    def a(self):
        data :marseychartbar: = self.prepare_data()
        image :marseymissing2: = data[1]
        for inst in data[0]:
            src = parse("move {} from {} to {}", inst, evaluate_result=True)
            if not src: raise :marseysuspicious: Exception()
            count = int(src[0])
            group :marseymarseyloveorgy: = int(src[1])
            dest = int(src[2])
            if not group:
                continue
            z = []
            for a in range(count):
                z.append(image[group - 1].pop())
            for b in z:
                image[dest - 1].append(b)
        for x in image:
            print(x)
    
    def b(self):
        print("---------------")
        data :marseychartbar: = self.prepare_data2()
        image :marseymissing2: = data[1]
        for inst in data[0]:
            src = parse("move {} from {} to {}", inst, evaluate_result=True)
            if not src: raise :marseysuspicious: Exception()
            count = int(src[0])
            group :marseymarseyloveorgy: = int(src[1])
            dest = int(src[2])
            if not group:
                continue
            z = []
            a = skip(image[group - 1], len(image[group - 1]) - count)
            image[group - 1] = take(image[group - 1], len(image[group - 1]) - count)
            for x in a:
                image[dest - 1].append(x)
        for x in image:
            print(x)
        pass

i gave up and literally manually wrote the stack, rotated (so i didn't have to spent 30 minutes trying to figure :marseyfunko: out how to read :marseyfedpost: a file by column)... here's that file

RSLFQ---
NZQGPT--
SMQB----
TGZJHCBQ
PHMBNFS-
PCQNSLVG
WCF-----
QHGZWVPM
GZDLCNR-
Jump in the discussion.

No email address required.

30 minutes trying to figure out how to read a file by column

... aren't you supposed to be a competent coder? I haven't done AoC yet but

>>> a = [list(a) for a in '123\n456\n789'.split('\n')]

>>> a

[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]

>>> list(zip(*a))

[('1', '4', '7'), ('2', '5', '8'), ('3', '6', '9')]

Jump in the discussion.

No email address required.

this part of the puzzle :marseysphinx: input was...

            [Q]     [G]     [M]    
            [B] [S] [V]     [P] [R]
    [T]     [C] [F] [L]     [V] [N]
[Q] [P]     [H] [N] [S]     [W] [C]
[F] [G] [B] [J] [B] [N]     [Z] [L]
[L] [Q] [Q] [Z] [M] [Q] [F] [G] [D]
[S] [Z] [M] [G] [H] [C] [C] [H] [Z]
[R] [N] [S] [T] [P] [P] [W] [Q] [G]
 1   2   3   4   5   6   7   8   9 

you're moving the literal stacks of boxes. i.e. you have a stack for 1, 2, 3, 4, 5, 6, 7, 8, and 9.

it's... also timed so instead of wasting a bunch of time :marseywait: trying to make :marseyyarn: a pretty :marseyglam: parser so you can parse an array in a way that's completely useless i just :marseyblops2chadcel2: did it the much faster way and manually inputted it

Jump in the discussion.

No email address required.

>>> list(map(list,map(reversed, list(zip(*map(list, list(filter(len, x.split("\n")))[:-1])))[1::4])))

2 minutes. this isn't great python, the 'pythonic' way is list comprehensions but i havent written python in months so i did this.

Jump in the discussion.

No email address required.

:#marseynerd2:

Jump in the discussion.

No email address required.

ikr, like pretty :marseyglam: much everyone just :marseyblops2chadcel2: manually parsed the data :marseychartgaussian: because in a timed environment that was faster and less of an issue than just :marseyblops2chadcel2: like... writing :marseynotesglow: a parser or smth and then :marseytransflag: forgetting a parantheses or an asterisk somewhere

Jump in the discussion.

No email address required.

Writing a "proper parser" was not that hard tbh:

    def parse_stacks():
        stacks = [[] for _ in range(cnt)]
        for i, s in enumerate(data):
            for j, c in enumerate(s[1::4]):
                if c == '1':
                    for stack in stacks:
                        stack.reverse()
                    return stacks, i
                if c not in ' .':
                    stacks[j].append(c)

The two tricky parts was that first, while not entirely awake, I put the reversing code at the end of function lol, second, copypasting the example into vscode trimmed trailing spaces so I had to use dots instead and patch the parser correspondingly.

If not for that stuff, I honestly think that writing that was faster than transforming the input manually.

Jump in the discussion.

No email address required.

you can split the difference and do half manual input and half programming

stacks = [
    [*'MSJLVFNR'][::-1],
    [*'HWJFZDNP'][::-1],
    [*'GDCRW'][::-1],
    [*'SBN'][::-1],
    [*'NFBCPWZM'][::-1],
    [*'WMRP'][::-1],
    [*'WSLGNTR'][::-1],
    [*'VBNFHTQ'][::-1],
    [*'FNZHML'][::-1],
]
Jump in the discussion.

No email address required.

TIL about using stars that way. Though manually copypasting [::-1] is retarded IMO. idk.

Jump in the discussion.

No email address required.

More comments

or you could just... not do that and do that actual meat of the problem. it's not hard :marseydiehard: but it's also a waste :marseyradioactive: of time :marseywait:

Jump in the discussion.

No email address required.

Imagine if you frick up a single time when manually entering the transposed input. It's, like, what, 30 letters, this can happen. How will you even recover?

Having a pretty straightforward code that picks every fourth letter is so much better as far as recovering from bugs goes.

Have you participated in previous years? I hope that this one will not be an exception and at some point we get one or more of those hellish simulation problems that fastest people take an hour to get right. They tend to separate the cowboys from the responsible coders really well.

Jump in the discussion.

No email address required.

More comments

Yeah my parser was 6 lines once i separated out that portion of the file. I feel like people are more intimidated by the idea of the parser than anything else.

Jump in the discussion.

No email address required.

ikr, like pretty much everyone just manually parsed the data

Honestly, is why I hate code Challenges. Just give me Datafiles in some real word format.

Jump in the discussion.

No email address required.

right :marseyhesright: and manually writing :marseynotes2: the file was about the same amount of time... and much less error :marsey404: prone

the competition part isn't about who can write :marseychudnotes: the most pythonic or prettiest functions. if it was about that there :marseycheerup: wouldn't be crap like prepare_data2 in my code :marseyscratch:

Jump in the discussion.

No email address required.

it is much more error-prone to manually transform data than to automatically do it. it ensures you don't have a typo because you write a simple transformation that applies everywhere

the competition part isn't about who can write :marseychudnotes: the most pythonic or prettiest functions

the point of advent of code for non-tard coders I know is to explore a language or just code a bit. Learning how to write good code is better than learning to write shitty code! Writing shitty code quickly isn't really useful, writing good code quickly is better.

Also, in this sense, the good code was faster. It took less time for me to write that than it would to manually transform the stack.

plausibly this is a bit?

Jump in the discussion.

No email address required.

except... it really didn't though. you also aren't doing it by your own admission so why don't you join

i mean yeah :marseykoolaid: we're doing it at 12 AM ET the second :marseygunnut: it comes out, some of us are doing it in a timed basis, even if there :marseycheerup: is some nerd :marseygamer: doing it in 6502 assembly or whatever

Jump in the discussion.

No email address required.

i did just do it, my answer is 10 lines of code and ~ 350 characters per challenge

Jump in the discussion.

No email address required.

no you didn't

![](/images/1670227491197668.webp)

unless you just did it in which case lol

Jump in the discussion.

No email address required.

More comments

What's your username on the leaderboard?

Jump in the discussion.

No email address required.

transmarseyfemboycutehitlerdramacel1469 :marsoyhype:

Jump in the discussion.

No email address required.

:#marseyquestion::#marseyjudge:

Jump in the discussion.

No email address required.

I'll be darned. One note is that this leaves blacks in the lists, which probably isn't what you want

Jump in the discussion.

No email address required.

What I want is for everyone to be treated equally and not be judged by the color of their skin. What's wrong with that?

Jump in the discussion.

No email address required.

chunk the line into blocks of 4, and map over it, pushing to the associated stack if the block has a letter at charIndex 1. faster than manual transforms, and less error prone.

Jump in the discussion.

No email address required.

aren't you supposed to be a competent coder?

Don't bully JC, longpostbot has done that enough in these threads

Jump in the discussion.

No email address required.

I was blindsided OK!? Wasn't prepared for reverse transpose. But its OK I can do it now

I was blindsided OK!? Wasn't prepared for reverse transpose. But its OK I can do it now

I was blindsided OK!? Wasn't prepared for reverse transpose. But its OK I can do it now

Lesson learned lesson learned

Jump in the discussion.

No email address required.

Have you owned the libs yet?

Jump in the discussion.

No email address required.

:#marseyhesright:

Jump in the discussion.

No email address required.

Lol, the marsify award marsified your code... sort of.

Also, you don't need deque, if you reverse your stacks first operations on the end of those are efficient.

Jump in the discussion.

No email address required.

Lmaooooo I did the same! Go to line 9 and then reverse parse and then add to stack.. Did it with my hand

Lmaooooo I did the same! Go to line 9 and then reverse parse and then add to stack.. Did it with my hand

And basically created the stacks manually 😂

And basically created the stacks manually 😂

Jump in the discussion.

No email address required.



Now playing: Aquatic Ambience (A Hint of Blue remix) (DKC).mp3

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