Avent of Code - Day 05 - Supply Stacks
Tags: adventofcode, programming, day5, algorithms
Published: 2022-12-05 19:40:15

[Advent of Code](https://adventofcode.com/) 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](https://cdn.steemitimages.com/DQmbL3F8AiTYgQZtaF1tYCbETYp2Jyfkx3NQPXoRJUveFjh/image.png)

### Q1
```python
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))
```

### Q2
```python
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))
```

Today is about the [Stacks](https://helloacm.com/teaching-kids-programming-greedy-simulation-algorithm-to-validate-stack-sequences/) and [DefaultDict](https://helloacm.com/teaching-kids-programming-count-unreachable-pairs-of-nodes-in-an-undirected-graph-recursive-depth-first-search-algorithm/). But the hardest part is to handle the input (how to process the raw data and read in the initial stacks).

<hr/>

### [Steem](https://steemyy.com) to the Moon!