DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DoctorLai
DoctorLai

Posted on

Avent of Code - Day 05 - Supply Stacks

Advent of Code occurs at Dec 01 to 25 where each day, you will need to solve a puzzle. It is Festival and the problem statement is mostly related to Christmas.

Day 5 - Supply Stacks

https://adventofcode.com/2022/day/5

image.png

Q1

import sys
from collections import defaultdict, deque

file1 = open(sys.argv[1], "r")

data = defaultdict(deque)
inst = []

while True:
        line = file1.readline()
        if not line:
                break
        arr = line.split()
        if len(arr) == 0:
                continue
        if arr[0] == "move":
                inst.append((int(arr[1]), int(arr[3]), int(arr[5])))
        elif "[" not in line:
                continue
        else:
                n = len(line)
                i = 0
                while i < n:
                        while i < n and line[i] == " ":
                                i += 1
                        if line[i] == '[':
                                data[i//4+1].appendleft(line[i+1])
                                i += 3
                        else:
                                i += 1
print(data)
print(inst)

for n, a, b in inst:
        for _ in range(n):
                data[b].append(data[a].pop())

minid = min(data.keys())
maxid = max(data.keys())
ans = []
for r in range(minid, maxid + 1):
        ans.append(data[r][-1])

print("".join(ans))
Enter fullscreen mode Exit fullscreen mode

Q2

import sys
from collections import defaultdict, deque

file1 = open(sys.argv[1], "r")

data = defaultdict(deque)
inst = []

while True:
        line = file1.readline()
        if not line:
                break
        arr = line.split()
        if len(arr) == 0:
                continue
        if arr[0] == "move":
                inst.append((int(arr[1]), int(arr[3]), int(arr[5])))
        elif "[" not in line:
                continue
        else:
                n = len(line)
                i = 0
                while i < n:
                        while i < n and line[i] == " ":
                                i += 1
                        if line[i] == '[':
                                data[i//4+1].appendleft(line[i+1])
                                i += 3
                        else:
                                i += 1
print(data)
print(inst)

for n, a, b in inst:
        x = []
        for _ in range(n):
                x.append(data[a].pop())
        for i in x[::-1]:
                data[b].append(i)

minid = min(data.keys())
maxid = max(data.keys())
ans = []
for r in range(minid, maxid + 1):
        ans.append(data[r][-1])

print("".join(ans))
Enter fullscreen mode Exit fullscreen mode

Today is about the Stacks and DefaultDict. But the hardest part is to handle the input (how to process the raw data and read in the initial stacks).


Steem to the Moon!

Top comments (0)

Build Anything...


Use any Linode offering to create something for the DEV x Linode Hackathon 2022. A variety of prizes are up for grabs, inculding $1,000 USD. πŸ‘€

β†’ Join the Hackathon <-