-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay05.cs
108 lines (89 loc) · 3.22 KB
/
Day05.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Text.RegularExpressions;
namespace AdventOfCode2022;
internal partial class Day05
{
private readonly List<List<char>> stacks;
private readonly List<(int Quantity, int From, int To)> instructions;
public Day05(string input)
{
stacks = new List<List<char>>();
instructions = new List<(int Quantity, int From, int To)>();
var stackData = input.Split(Environment.NewLine + Environment.NewLine)[0].Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
var instructionData = input.Split(Environment.NewLine + Environment.NewLine)[1].Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
var numberOfCrates = stackData.Last().Split(' ', StringSplitOptions.RemoveEmptyEntries).Length;
for (var i = 0; i < numberOfCrates; i++)
{
stacks.Add(new List<char>());
}
foreach (var level in stackData.SkipLast(1).Reverse())
{
for (var i = 0; i < numberOfCrates; i++)
{
var crate = level[4 * i + 1];
if (crate != ' ')
{
stacks[i].Add(crate);
}
}
}
foreach (var instruction in instructionData)
{
var match = InstructionPattern().Match(instruction);
instructions.Add((int.Parse(match.Groups["quantity"].Value), int.Parse(match.Groups["from"].Value), int.Parse(match.Groups["to"].Value)));
}
}
public string PartOne()
{
var state = stacks.Select(x => new Stack<char>(x)).ToList();
foreach (var (quantity, from, to) in instructions)
{
for (var i = 0; i < quantity; i++)
{
var crate = state[from - 1].Pop();
state[to - 1].Push(crate);
}
}
return string.Concat(state.Select(x => x.Peek()));
}
public string PartTwo()
{
var state = stacks.Select(x => new Stack<char>(x)).ToList();
foreach (var (quantity, from, to) in instructions)
{
var crates = new List<char>();
for (var i = 0; i < quantity; i++)
{
var crate = state[from - 1].Pop();
crates.Add(crate);
}
foreach (var crate in crates.Reverse<char>())
{
state[to - 1].Push(crate);
}
}
return string.Concat(state.Select(x => x.Peek()));
}
[GeneratedRegex("move (?<quantity>\\d+) from (?<from>\\d+) to (?<to>\\d+)")]
private static partial Regex InstructionPattern();
}
public class Day05Test
{
private const string InputFile = "Day05.Input.txt";
private const string ExampleFile = "Day05.Example.txt";
[Theory]
[FileData(ExampleFile, "CMZ")]
[FileData(InputFile, "FWSHSPJWM")]
public void TestPartOne(string input, string expected)
{
var solution = new Day05(input);
Assert.Equal(expected, solution.PartOne());
}
[Theory]
[FileData(ExampleFile, "MCD")]
[FileData(InputFile, "PWPWHGFZS")]
public void TestPartTwo(string input, string expected)
{
var solution = new Day05(input);
Assert.Equal(expected, solution.PartTwo());
}
}