-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventOfCode2021day3part1.linq
120 lines (95 loc) · 1.76 KB
/
adventOfCode2021day3part1.linq
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<Query Kind="Program" />
void Main()
{
var inputList = new List<int>();
"Sample should be 198".Dump();
Simulate(GetSampleList());
"Real".Dump();
Simulate(ReadMyFile(@"G:\Git\linq\adventofcode2021\input3.txt"));
}
private void Simulate(List<string> commands)
{
int width = commands[0].Length;
int height = commands.Count;
var array = new char[height, width];
int x = 0;
int y = 0;
foreach (var command in commands)
{
foreach (var by in command)
{
array[y, x] = by;
x++;
}
x = 0;
y++;
}
string gamma = "";
string epsilon = "";
x = 0;
y = 0;
for (int i = 0; i < width; i++)
{
int zeroCount = 0;
int oneCount = 0;
for (int ii = 0; ii <= height - 1; ii++)
{
if (array[ii, x] == '0')
zeroCount++;
else
oneCount++;
}
x++;
if (zeroCount > oneCount)
{
gamma += "0";
epsilon += "1";
}
else
{
gamma += "1";
epsilon += "0";
}
}
(ByteToString(gamma)*ByteToString(epsilon)).Dump();
}
private int ByteToString(string str){
int number=0;
int inc=1;
foreach (var b in str.Reverse())
{
if(b=='1')
number+=inc;
inc=inc*2;
}
return number;
}
private List<string> ReadMyFile(string uri)
{
var newList = new List<string>();
string line;
System.IO.StreamReader reader = new StreamReader(uri);
while ((line = reader.ReadLine()) != null)
{
newList.Add(line);
}
return newList;
}
private List<string> GetSampleList()
{
var newList = new List<string>();
newList.Add("00100");
newList.Add("11110");
newList.Add("10110");
newList.Add("10111");
newList.Add("10101");
newList.Add("01111");
newList.Add("00111");
newList.Add("11100");
newList.Add("10000");
newList.Add("11001");
newList.Add("00010");
newList.Add("01010");
return newList;
}
// Define other methods and classes here