Skip to content

Commit 780a66b

Browse files
committed
2022/05 comments
1 parent 672d35c commit 780a66b

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

2022/Day05/Solution.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ namespace AdventOfCode.Y2022.Day05;
88
[ProblemName("Supply Stacks")]
99
class Solution : Solver {
1010

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+
1118
public object PartOne(string input) => MoveCrates(input, CrateMover9000);
1219
public object PartTwo(string input) => MoveCrates(input, CrateMover9001);
1320

14-
record struct Move(int count, Stack<char> source, Stack<char> target);
15-
1621
void CrateMover9000(Move move) {
1722
for (var i = 0; i < move.count; i++) {
1823
move.target.Push(move.source.Pop());
@@ -28,27 +33,35 @@ void CrateMover9001(Move move) {
2833

2934
string MoveCrates(string input, Action<Move> crateMover) {
3035
var parts = input.Split("\n\n");
31-
36+
3237
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+
3543
// 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();
3849

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.
4154
foreach (var line in stackDefs.Reverse().Skip(1)) {
4255
foreach (var (stack, item) in stacks.Zip(line.Chunk(4))) {
43-
// item is ~ "[A] "
4456
if (item[1] != ' ') {
4557
stack.Push(item[1]);
4658
}
4759
}
4860
}
4961

50-
// parse the 'moves' with regex, and use 'crateMover' on them:
62+
// now parse the move operations and crateMover on them:
5163
foreach (var line in parts[1].Split("\n")) {
64+
// e.g. "move 6 from 4 to 3"
5265
var m = Regex.Match(line, @"move (.*) from (.*) to (.*)");
5366
var count = int.Parse(m.Groups[1].Value);
5467
var from = int.Parse(m.Groups[2].Value) - 1;
@@ -60,8 +73,7 @@ string MoveCrates(string input, Action<Move> crateMover) {
6073
));
6174
}
6275

63-
// assuming that the stacks are not empty at the end, concatenate the
64-
// top of each:
76+
// collect the top of each stack:
6577
return string.Join("", stacks.Select(stack => stack.Pop()));
6678
}
6779
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy