43 lines
1.0 KiB
Python
43 lines
1.0 KiB
Python
![]() |
#/usr/bin/env python
|
||
|
|
||
|
from collections import defaultdict
|
||
|
|
||
|
import re
|
||
|
RE_MOVE = re.compile(r"^move (\d+) from (\d+) to (\d+)$")
|
||
|
|
||
|
def read_cargo(f):
|
||
|
cargo = defaultdict(list)
|
||
|
for line in f:
|
||
|
line = line.rstrip()
|
||
|
if len(line) == 0:
|
||
|
break
|
||
|
pos = 1
|
||
|
while (pos-1)*4+1 < len(line):
|
||
|
c = line[(pos-1)*4+1]
|
||
|
if c == ' ':
|
||
|
pos += 1
|
||
|
continue
|
||
|
cargo[pos].append(c)
|
||
|
pos += 1
|
||
|
return cargo
|
||
|
|
||
|
def read_moves(f):
|
||
|
for line in f:
|
||
|
line = line.strip()
|
||
|
m = RE_MOVE.match(line)
|
||
|
if not m:
|
||
|
continue
|
||
|
yield (int(m.group(1)), int(m.group(2)), int(m.group(3)))
|
||
|
|
||
|
|
||
|
with open("input.txt", "r") as f:
|
||
|
cargo = read_cargo(f)
|
||
|
for move in read_moves(f):
|
||
|
slice = cargo[move[1]][:move[0]]
|
||
|
slice.reverse()
|
||
|
cargo[move[1]] = cargo[move[1]][move[0]:]
|
||
|
cargo[move[2]] = slice + cargo[move[2]]
|
||
|
|
||
|
for k in sorted(cargo.keys()):
|
||
|
print(cargo[k][0], end ="")
|
||
|
print()
|