```{r}
library(reticulate)
```
Day 5
python
Part 1
I think for preprocessing the text, I’m going to start off with python.
```{python}
with open("2022-12-5_assets/input.txt", "r") as data:
= data.readlines()
lines ```
Setting up the initial state
```{python}
= [x.replace("\n", "")
initial_state for x in lines
if "move" not in x and len(x) > 1]
initial_state```
['[N] [C] [Q] ', '[W] [J] [L] [J] [V]', '[F] [N] [D] [L] [S] [W]', '[R] [S] [F] [G] [R] [V] [Z]', '[Z] [G] [Q] [C] [W] [C] [F] [G]', '[S] [Q] [V] [P] [S] [F] [D] [R] [S]', '[M] [P] [R] [Z] [P] [D] [N] [N] [M]', '[D] [W] [W] [F] [T] [H] [Z] [W] [R]', ' 1 2 3 4 5 6 7 8 9 ']
There’s not a fixed delimiter between every column. Each column is 3 characters wide, with a space in between.
```{python}
len(x) for x in initial_state]
[```
[35, 35, 35, 35, 35, 35, 35, 35, 35]
```{python}
def three_split(x):
= []
out = 0
start = False
done while not done:
= start + 3
end = x[start:end].strip()
item
out.append(item)= end + 1
start if start > len(x):
= True
done return(out)
```
```{python}
= [three_split(x) for x in initial_state]
state_rows
state_rows```
[['[N]', '', '[C]', '', '', '', '', '[Q]', ''], ['[W]', '', '[J]', '[L]', '', '', '', '[J]', '[V]'], ['[F]', '', '[N]', '[D]', '', '[L]', '', '[S]', '[W]'], ['[R]', '[S]', '[F]', '[G]', '', '[R]', '', '[V]', '[Z]'], ['[Z]', '[G]', '[Q]', '[C]', '', '[W]', '[C]', '[F]', '[G]'], ['[S]', '[Q]', '[V]', '[P]', '[S]', '[F]', '[D]', '[R]', '[S]'], ['[M]', '[P]', '[R]', '[Z]', '[P]', '[D]', '[N]', '[N]', '[M]'], ['[D]', '[W]', '[W]', '[F]', '[T]', '[H]', '[Z]', '[W]', '[R]'], ['1', '2', '3', '4', '5', '6', '7', '8', '9']]
I had a big hang up here, because I created the list of empty lists with [[]] * 9
, which copies the empty list, so when I appended a value to columns[0]
, it appended that value to all columns.
```{python}
= [[] for _ in range(len(state_rows[0]))]
columns for ridx, row in enumerate(state_rows):
for cidx, col in enumerate(state_rows[ridx]):
if len(col) > 0 :
columns[cidx].append(col)
print(columns)
```
[['[N]', '[W]', '[F]', '[R]', '[Z]', '[S]', '[M]', '[D]', '1'], ['[S]', '[G]', '[Q]', '[P]', '[W]', '2'], ['[C]', '[J]', '[N]', '[F]', '[Q]', '[V]', '[R]', '[W]', '3'], ['[L]', '[D]', '[G]', '[C]', '[P]', '[Z]', '[F]', '4'], ['[S]', '[P]', '[T]', '5'], ['[L]', '[R]', '[W]', '[F]', '[D]', '[H]', '6'], ['[C]', '[D]', '[N]', '[Z]', '7'], ['[Q]', '[J]', '[S]', '[V]', '[F]', '[R]', '[N]', '[W]', '8'], ['[V]', '[W]', '[Z]', '[G]', '[S]', '[M]', '[R]', '9']]
Setting up the move function
```{python}
= [x.strip() for x in lines if "move" in x]
move_instructions ```
```{python}
import re
= [re.findall(r'\d+', line)
move_list for line in move_instructions]
= [[int(x[0]), int(x[1])-1, int(x[2])-1]
move_int for x in move_list]
```
```{python}
def move_fun(m):
= m[0]
n = move[1]
fro = move[2]
to for i in range(n):
0, columns[fro].pop(0))
columns[to].insert(```
```{python}
for move in move_int:
move_fun(move)```
```{python}
"".join([x[0].replace("[", "").replace("]", "") for x in columns])
```
'FWNSHLDNZ'
Part 2
reset initial state
```{python}
= [[] for _ in range(len(state_rows[0]))]
columns for ridx, row in enumerate(state_rows):
for cidx, col in enumerate(state_rows[ridx]):
if len(col) > 0 :
columns[cidx].append(col)```
```{python}
def move_fun9001(m):
= m[0]
n = move[1]
fro = move[2]
to = columns[fro][0:n] + columns[to]
columns[to] = columns[fro][n:]
columns[fro] ```
```{python}
for move in move_int:
move_fun9001(move)```
```{python}
"".join([x[0].replace("[", "").replace("]", "") for x in columns])
```
'RNRGDNFQG'