-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day2.cs
91 lines (73 loc) · 2.27 KB
/
Day2.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode2018
{
class Day2 : Day
{
public override bool Test()
{
return Utils.Test(Part1, "abcdef\nbababc\nabbcde\nabcccd\naabcdd\nabcdee\nababab" , "12" ) &&
Utils.Test(Part2, "abcde\nfghij\nklmno\npqrst\nfguij\naxcye\nwvxyz", "fgij");
}
public override string Part1(string input, dynamic options)
{
int twos = 0, threes = 0;
foreach (var s in Utils.splitLines(input))
{
LineChecksum(s, out int two, out int three);
twos += two;
threes += three;
}
return (twos * threes).ToString();
}
public override string Part2(string input, dynamic options)
{
int i = 0;
while (true)
{
var match = Utils.splitLines((string)input).Select(l => l.Remove(i, 1)).GroupBy(l => l).Where(g => g.Count() == 2).FirstOrDefault();
if (match != null)
{
return match.Key;
}
i++;
}
}
private void LineChecksum(string s, out int twos, out int threes)
{
twos = 0;
threes = 0;
string alphabet = "abcdefghijklmnopqrstuvwxyz";
foreach (var letter in alphabet)
{
int found = 0;
foreach (var c in s)
{
if (c == letter)
{
found++;
}
if (found > 3)
{
break;
}
}
if (found == 2)
{
twos = 1;
}
else if (found == 3)
{
threes = 1;
}
if (twos == 1 && threes == 1)
{
break;
}
}
}
}
}