-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay03.cs
74 lines (62 loc) · 2 KB
/
Day03.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
namespace AdventOfCode._2021
{
public class Day03
{
string[] _binaryNums;
public Day03()
{
_binaryNums = File.ReadAllLines("Inputs/Day03.txt");
}
[Fact]
public void Part1()
{
int length = _binaryNums[0].Length;
int gamma = 0;
int bit = 0;
while (bit < length)
{
int count = _binaryNums.Count(num => num[bit] == '1');
if (count > _binaryNums.Length / 2)
gamma |= 1 << length - bit - 1;
bit++;
}
int epsilon = ~gamma & ((1 << length) - 1);
int answer = gamma * epsilon;
Assert.Equal(1307354, answer);
}
[Fact]
public void Part2()
{
string[] nums = _binaryNums;
int bit = 0;
while (nums.Length != 1)
{
nums = OxygenFilter(bit, nums);
bit++;
}
int oxygen = Convert.ToInt32(nums[0], 2);
nums = _binaryNums;
bit = 0;
while (nums.Length != 1)
{
nums = CarbonFilter(bit, nums);
bit++;
}
int carbon = Convert.ToInt32(nums[0], 2);
int answer = oxygen * carbon;
Assert.Equal(482500, answer);
string[] OxygenFilter(int bit, string[] starting)
{
int count = starting.Count(num => num[bit] == '1');
char target = count >= starting.Length - count ? '1' : '0';
return starting.Where(num => num[bit] == target).ToArray();
}
string[] CarbonFilter(int bit, string[] starting)
{
int count = starting.Count(num => num[bit] == '0');
char target = count <= starting.Length - count ? '0' : '1';
return starting.Where(num => num[bit] == target).ToArray();
}
}
}
}