-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay5.cs
116 lines (105 loc) · 3.83 KB
/
Day5.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
109
110
111
112
113
114
115
116
namespace advent_of_code_2022;
public class Day5
{
public static void Part1(string[] args)
{
string[] lines = File.ReadAllLines("day5-input.txt");
int indexOfLineWithStackIndexes = 0;
string lineWithStackIndexes = lines[indexOfLineWithStackIndexes];
for (var i = 1; i < lines.Length; i++)
{
if (string.IsNullOrEmpty(lines[i]))
{
break;
}
indexOfLineWithStackIndexes = i;
lineWithStackIndexes = lines[indexOfLineWithStackIndexes];
}
string[] stackIndexes = lineWithStackIndexes.Split(" ");
SortedDictionary<int, Stack<char>> stacks = new SortedDictionary<int, Stack<char>>();
for (var i = 0; i < stackIndexes.Length; i++)
{
int stackIndex = int.Parse(stackIndexes[i]);
int matrixIndex = lineWithStackIndexes.IndexOf(Char.Parse(stackIndexes[i].Trim()));
stacks[stackIndex] = new Stack<char>();
for (var j = indexOfLineWithStackIndexes - 1; j >= 0; j--)
{
var currentCh = lines[j][matrixIndex];
if (char.IsLetter(currentCh))
{
stacks[stackIndex].Push(currentCh);
}
}
}
for (int i = indexOfLineWithStackIndexes + 2; i < lines.Length; i++)
{
var commandParts = lines[i].Split(" ");
int count = int.Parse(commandParts[1]);
int from = int.Parse(commandParts[3]);
int to = int.Parse(commandParts[5]);
for (int j = 0; j < count; j++)
{
var val = stacks[from].Pop();
stacks[to].Push(val);
}
}
foreach (var (key, value) in stacks)
{
Console.Write(value.Pop());
}
Console.WriteLine();
}
public static void Solve(string[] args)
{
string[] lines = File.ReadAllLines("day5-input.txt");
int indexOfLineWithStackIndexes = 0;
string lineWithStackIndexes = lines[indexOfLineWithStackIndexes];
for (var i = 1; i < lines.Length; i++)
{
if (string.IsNullOrEmpty(lines[i]))
{
break;
}
indexOfLineWithStackIndexes = i;
lineWithStackIndexes = lines[indexOfLineWithStackIndexes];
}
string[] stackIndexes = lineWithStackIndexes.Split(" ");
SortedDictionary<int, Stack<char>> stacks = new SortedDictionary<int, Stack<char>>();
for (var i = 0; i < stackIndexes.Length; i++)
{
int stackIndex = int.Parse(stackIndexes[i]);
int matrixIndex = lineWithStackIndexes.IndexOf(Char.Parse(stackIndexes[i].Trim()));
stacks[stackIndex] = new Stack<char>();
for (var j = indexOfLineWithStackIndexes - 1; j >= 0; j--)
{
var currentCh = lines[j][matrixIndex];
if (char.IsLetter(currentCh))
{
stacks[stackIndex].Push(currentCh);
}
}
}
for (int i = indexOfLineWithStackIndexes + 2; i < lines.Length; i++)
{
var commandParts = lines[i].Split(" ");
int count = int.Parse(commandParts[1]);
int from = int.Parse(commandParts[3]);
int to = int.Parse(commandParts[5]);
Stack<char> tmp = new Stack<char>();
for (int j = 0; j < count; j++)
{
var val = stacks[from].Pop();
tmp.Push(val);
}
while (tmp.Count > 0)
{
stacks[to].Push(tmp.Pop());
}
}
foreach (var (key, value) in stacks)
{
Console.Write(value.Pop());
}
Console.WriteLine();
}
}