-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
88 lines (71 loc) · 2.11 KB
/
Program.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
using AdventOfCode.Common;
var lines = Resources.GetInputFileLines();
int numOfBits = lines.First().Length;
static bool OneIsMostFrequent(IEnumerable<string> numbers, int position)
{
var numWithOne = numbers.Count(n => n[position] == '1');
var half = (float)numbers.Count() / 2;
if (numWithOne == half)
{
throw new InvalidOperationException("Equal frequencies of 0 and 1");
}
return numWithOne > half;
}
// Part 1
int gammaRate = 0;
int epsilonRate = 0;
for (int i = 0; i < numOfBits; i++)
{
if (OneIsMostFrequent(lines, numOfBits - i - 1))
{
gammaRate |= 1 << i;
}
else
{
epsilonRate |= 1 << i;
}
}
Console.WriteLine($"Part 1: {gammaRate * epsilonRate}");
// Part 2
static string[] FilterByBit(IEnumerable<string> numbers, int position, bool keepWithOne)
{
return numbers.Where(x => x[position] == (keepWithOne ? '1' : '0')).ToArray();
}
int BinToDec(string binary)
{
int dec = 0;
for (int i = 0; i < numOfBits; i++)
{
dec |= (binary[numOfBits - i - 1] - '0') << i;
}
return dec;
}
string FindLifeSupportRating(IEnumerable<string> numbers, bool keepMostCommon, bool keepWithOneWhenEqual)
{
for (int position = 0; position < numOfBits; position++)
{
try
{
bool more1s = OneIsMostFrequent(numbers, position);
// More 1s
// + most common => keep 1s
// + least common => keep 0s
// More 0s
// + most common => keep 0s
// + least common => keep 1s
numbers = FilterByBit(numbers, position, keepWithOne: more1s == keepMostCommon);
}
catch (InvalidOperationException)
{
numbers = FilterByBit(numbers, position, keepWithOne: keepWithOneWhenEqual);
}
if (numbers.Count() == 1)
{
return numbers.Single();
}
}
return numbers.Single();
}
var oxygenRate = BinToDec(FindLifeSupportRating(lines, true, true));
var co2Rate = BinToDec(FindLifeSupportRating(lines, false, false));
Console.WriteLine($"Part 1: {oxygenRate * co2Rate}");