@@ -8,11 +8,16 @@ namespace AdventOfCode.Y2022.Day05;
8
8
[ ProblemName ( "Supply Stacks" ) ]
9
9
class Solution : Solver {
10
10
11
+ // The input is parsed into some stacks of 'crates', and move operations
12
+ // that is to be applied on them. There is a crane which takes some number
13
+ // of crates from one stack and puts them on the top of an other stack.
14
+ // Part one and two differs in how this crane works, which is implemented
15
+ // by the two 'crateMover' functions.
16
+ record struct Move ( int count , Stack < char > source , Stack < char > target ) ;
17
+
11
18
public object PartOne ( string input ) => MoveCrates ( input , CrateMover9000 ) ;
12
19
public object PartTwo ( string input ) => MoveCrates ( input , CrateMover9001 ) ;
13
20
14
- record struct Move ( int count , Stack < char > source , Stack < char > target ) ;
15
-
16
21
void CrateMover9000 ( Move move ) {
17
22
for ( var i = 0 ; i < move . count ; i ++ ) {
18
23
move . target . Push ( move . source . Pop ( ) ) ;
@@ -28,27 +33,35 @@ void CrateMover9001(Move move) {
28
33
29
34
string MoveCrates ( string input , Action < Move > crateMover ) {
30
35
var parts = input . Split ( "\n \n " ) ;
31
-
36
+
32
37
var stackDefs = parts [ 0 ] . Split ( "\n " ) ;
33
-
34
- // process each line by 4 character wide columns
38
+ // [D]
39
+ // [N] [C]
40
+ // [Z] [M] [P]
41
+ // 1 2 3
42
+
35
43
// last line defines the number of stacks:
36
- var stacks =
37
- stackDefs . Last ( ) . Chunk ( 4 ) . Select ( i => new Stack < char > ( ) ) . ToArray ( ) ;
44
+ var stacks = stackDefs
45
+ . Last ( )
46
+ . Chunk ( 4 )
47
+ . Select ( _ => new Stack < char > ( ) )
48
+ . ToArray ( ) ;
38
49
39
- // bottom-up: push the next element to the the correspoing stack;
40
- // ' ' means no more elements.
50
+ // Each input line is processed in 4 character long chunks in bottom up
51
+ // order. Push the next element into the next stack (note how the chunk
52
+ // and the stack is paired up using the zip function). ' ' means no more
53
+ // elements to add, just go to the next chunk.
41
54
foreach ( var line in stackDefs . Reverse ( ) . Skip ( 1 ) ) {
42
55
foreach ( var ( stack , item ) in stacks . Zip ( line . Chunk ( 4 ) ) ) {
43
- // item is ~ "[A] "
44
56
if ( item [ 1 ] != ' ' ) {
45
57
stack . Push ( item [ 1 ] ) ;
46
58
}
47
59
}
48
60
}
49
61
50
- // parse the 'moves' with regex, and use ' crateMover' on them:
62
+ // now parse the move operations and crateMover on them:
51
63
foreach ( var line in parts [ 1 ] . Split ( "\n " ) ) {
64
+ // e.g. "move 6 from 4 to 3"
52
65
var m = Regex . Match ( line , @"move (.*) from (.*) to (.*)" ) ;
53
66
var count = int . Parse ( m . Groups [ 1 ] . Value ) ;
54
67
var from = int . Parse ( m . Groups [ 2 ] . Value ) - 1 ;
@@ -60,8 +73,7 @@ string MoveCrates(string input, Action<Move> crateMover) {
60
73
) ) ;
61
74
}
62
75
63
- // assuming that the stacks are not empty at the end, concatenate the
64
- // top of each:
76
+ // collect the top of each stack:
65
77
return string . Join ( "" , stacks . Select ( stack => stack . Pop ( ) ) ) ;
66
78
}
67
79
}
0 commit comments