-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day3.cs
61 lines (46 loc) · 1.93 KB
/
Day3.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace cs
{
class Day3
{
public static void Part1()
{
List<string> data = File.ReadAllLines(@"../../../../../data/day3.txt").ToList();
int gama = 0, epsilon = 0;
for (int i = 0; i < data[0].Length; i++)
{
int[] counter = new int[] { 0, 0 };
foreach (string row in data) counter[row[i] - '0']++;
gama = (gama << 1) + (counter[0] > counter[1] ? 0 : 1);
epsilon = (epsilon << 1) + (counter[0] > counter[1] ? 1 : 0);
}
Console.WriteLine("Day: 3 | Part: 1 | Result: " + (gama * epsilon));
}
public static void Part2()
{
List<string> data_og = File.ReadAllLines(@"../../../../../data/day3.txt").ToList();
List<string> data_co2s = new List<string>(data_og);
for (int i = 0; i < data_og[0].Length; i++)
{
if (data_og.Count > 1)
{
int[] counter = new int[] { 0, 0 };
foreach (string row in data_og) counter[row[i] - '0']++;
data_og = data_og.Where(r => r[i] == (counter[1] >= counter[0] ? '1' : '0')).ToList();
}
if (data_co2s.Count > 1)
{
int[] counter = new int[] { 0, 0 };
foreach (string row in data_co2s) counter[row[i] - '0']++;
data_co2s = data_co2s.Where(r => r[i] == (counter[1] >= counter[0] ? '0' : '1')).ToList();
}
if (data_og.Count == 1 && data_co2s.Count == 1)
break;
}
Console.WriteLine("Day: 3 | Part: 2 | Result: " + (Convert.ToInt32(data_og[0], 2) * Convert.ToInt32(data_co2s[0], 2)));
}
}
}