-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay5.cs
68 lines (55 loc) · 1.76 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace AdventOfCode2018
{
class Day5 : Day
{
public override bool Test()
{
return Utils.Test(Part1, "dabAcCaCBAcCcaDA", "10" ) &&
Utils.Test(Part2, "dabAcCaCBAcCcaDA", "4");
}
public override string Part1(string input, dynamic options)
{
return Collapse(input, '@').ToString(); // any non letter for filter does nothing
}
public override string Part2(string input, dynamic options)
{
var min = int.MaxValue;
foreach (char c in "abcdefghjiklmnopqrstuvwxyz")
{
var result = Collapse(input, c);
min = Math.Min(min, result);
}
return min.ToString();
}
private int Collapse(string input, char filter)
{
Stack<char> result = new Stack<char>();
foreach (char next in (string)input)
{
if (char.ToLower(next) == filter)
{
continue;
}
if (result.Count > 0)
{
char head = result.Peek();
if (char.ToLower(head) == char.ToLower(next) && head != next)
{
result.Pop();
continue;
}
}
result.Push(next);
}
return result.Count;
}
}
}