-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventOfCode2021day10part1.linq
154 lines (128 loc) · 2.51 KB
/
adventOfCode2021day10part1.linq
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<Query Kind="Program" />
Stack<char> stack;
void Main()
{
stack = new Stack<char>();
var inputList = new List<int>();
"Sample should be 26397".Dump();
Simulate(GetSampleList());
"Real".Dump();
Simulate(ReadMyFile(@"C:\Users\Andreas Andersson\Downloads\input10.txt"));
}
private void Simulate(List<string> commands)
{
var test = commands[2];
var points = 0;
foreach (var line in commands)
{
points += TestString(line);
}
points.Dump();
//TestString(test);
}
private int TestString(string line)
{
//line.Dump();
var ponts = 0;
foreach (var st in line)
{
if (IsEnd(st) && st == GetEndingFor(stack.Peek()))
{
//$"{stack.Peek()}{st}".Dump();
stack.Pop();
}
else if (IsEnd(st))
{
//$"found wrong ending expected {GetEndingFor(stack.Peek())} but found {st}".Dump();
ponts += GetPointFor(st);
stack.Pop();
break;
}
if (IsOpen(st))
{
stack.Push(st);
}
}
//stack.Dump();
return ponts;
}
private int GetPointFor(char ch)
{
switch (ch)
{
case ')':
return 3;
case ']':
return 57;
case '}':
return 1197;
case '>':
return 25137;
}
throw new ArgumentException("Invalid open " + ch);
}
private bool IsEnd(char end)
{
switch (end)
{
case ')':
case '}':
case ']':
case '>':
return true;
}
return false;
}
private bool IsOpen(char open)
{
switch (open)
{
case '(':
case '{':
case '[':
case '<':
return true;
}
return false;
}
private char GetEndingFor(char open)
{
switch (open)
{
case '(':
return ')';
case '{':
return '}';
case '[':
return ']';
case '<':
return '>';
}
throw new ArgumentException("Invalid open " + open);
}
private List<string> ReadMyFile(string uri)
{
var newList = new List<string>();
string line;
System.IO.StreamReader reader = new StreamReader(uri);
while ((line = reader.ReadLine()) != null)
{
newList.Add(line);
}
return newList;
}
private List<string> GetSampleList()
{
var newList = new List<string>();
newList.Add("[({(<(())[]>[[{[]{<()<>>");
newList.Add("[(()[<>])]({[<{<<[]>>(");
newList.Add("{([(<{}[<>[]}>{[]{[(<()>");
newList.Add("(((({<>}<{<{<>}{[]{[]{}");
newList.Add("[[<[([]))<([[{}[[()]]]");
newList.Add("[{[{({}]{}}([{[{{{}}([]");
newList.Add("{<[[]]>}<{[{[{[]{()[[[]");
newList.Add("[<(<(<(<{}))><([]([]()");
newList.Add("<{([([[(<>()){}]>(<<{{");
newList.Add("<{([{{}}[<[[[<>{}]]]>[]]");
return newList;
}