File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ import copy
2+ import re
3+
4+ with open ("input.txt" ) as f :
5+ raw_input_string = f .read ().strip ()
6+
7+ initial_crate_config_string , operations_string = raw_input_string .split ("\n \n " )
8+
9+ stacks = []
10+
11+ initial_crate_config_lines = initial_crate_config_string .split ("\n " )
12+ # 3 characters in between stacks
13+ for _ in range ((len (initial_crate_config_lines [- 1 ]) + 1 ) // 4 ):
14+ stacks .append ([])
15+
16+ for line in initial_crate_config_lines [- 2 ::- 1 ]:
17+ for stack , crate in zip (stacks , line [1 ::4 ]):
18+ if crate != " " :
19+ stack .append (crate )
20+
21+ operations = []
22+ for line in operations_string .split ("\n " ):
23+ operations .append (
24+ tuple (
25+ map (int , re .match ("^move (\\ d+) from (\\ d+) to (\\ d+)$" , line ).groups ())
26+ )
27+ )
28+
29+ # save a copy for part 2
30+ _stacks = copy .deepcopy (stacks )
31+
32+ # part 1:
33+ for cnt , src , dst in operations :
34+ for _ in range (cnt ):
35+ stacks [dst - 1 ].append (stacks [src - 1 ].pop (- 1 ))
36+
37+ print ("" .join (stack [- 1 ] for stack in stacks ))
38+
39+
40+ # part 2:
41+ stacks = _stacks
42+
43+ for cnt , src , dst in operations :
44+ stacks [dst - 1 ].extend (stacks [src - 1 ][- cnt :])
45+ del stacks [src - 1 ][- cnt :]
46+
47+ print ("" .join (stack [- 1 ] for stack in stacks ))
You can’t perform that action at this time.
0 commit comments