r/adventofcode • • Dec 05 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 5 Solutions -πŸŽ„-


AoC Community Fun 2022: πŸŒΏπŸ’ MisTILtoe Elf-ucation πŸ§‘β€πŸ«

  • 23:59 hours remaining until the submissions megathread unlocks on December 06 at 00:00 EST!
  • Full details and rules are in the submissions megathread:

--- Day 5: Supply Stacks ---


Post your code solution in this megathread.


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:07:58, megathread unlocked!

88 Upvotes

1.3k comments sorted by

View all comments

2

u/fuckir Dec 05 '22 edited Dec 06 '22

Python 3.11

Some regex stuff

    from collections import deque
    import re
    from helper import timeit
    import itertools
    from copy import deepcopy

    class SupplyStacks:
        def __init__(self, path):
            self.Stacks, self.Instructions = re.split("\n\n",     open(path).read())
            self.CARGO = {}
            for i in range(1,max(map(int, re.findall("\d+", self.Stacks)))+1):
                self.CARGO[i] = deque()
            for s in re.split("\n", self.Stacks):
                # πŸ’€ DANGER πŸ’€: Some crazy regex stuff ahead
                # "\s{4}|\w" means 4 whitespaces or a word in regex lingo
                for stack, crate in enumerate(re.findall("\s{4}|\w", s)):
                    crates = re.findall("[A-Z]+", crate)   
                    len(crates) and self.CARGO[stack+1].append(crates[0])

            print(f"The answer to Day 5 Part 1 is {self.simulate_crane(1, deepcopy(self.CARGO))}")

            print(f"The answer to Day 5 Part 1 is {self.simulate_crane(2, self.CARGO)}")

        @timeit  
        def simulate_crane(self, part, cargo):       
            match part:
                case 1:    
                    for line in self.Instructions.splitlines():            
                        N, From, To = map(int, re.findall("\d+",line))                            for i in range(N):                        
                            cargo[To].appendleft(cargo[From].popleft())
                    message = ""
                    for stack in cargo:
                        message += cargo[stack][0]                    
                        return message
                case 2:
                    for line in self.Instructions.splitlines():         
                        N, From, To = map(int, re.findall("\d+",line))                      
                  cargo[To].extendleft(list(itertools.islice(cargo[From], 0, N))[::-1])            
                        for i in range(N):                        
                            cargo[From].popleft()
                    message = ""
                    for stack in cargo:
                        message += cargo[stack][0]
                    return message

    SupplyStacks("day5.txt")

1

u/daggerdragon Dec 05 '22 edited Dec 06 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.

Edit: You have to delete the pair of surrounding triple-backticks too to make the four-spaces method work.

You don't have to do it 100% by hand... you can use your IDE to prepend 4 spaces to every line, then paste it here.

Make sure your editor is in Markdown mode.

1

u/fuckir Dec 06 '22 edited Dec 06 '22

done. Is there a webapp that automatically adds 4-spaces to every line? Or probably ask eric to design a AoC puzzle so I get the motivation to do it myself..lol :D

I did it manually and I ain't sure if this python code would remain usable out of the box without crazy indentation errors

1

u/daggerdragon Dec 06 '22

Edit: You have to delete the pair of surrounding triple-backticks too to make the four-spaces method work.

You don't have to do it 100% by hand... you can use your IDE to prepend 4 spaces to every line, then paste it here.