-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventOfCode2021day3part2.linq
147 lines (117 loc) · 2.34 KB
/
adventOfCode2021day3part2.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<Query Kind="Program" />
void Main()
{
var inputList = new List<int>();
"Sample should be 230(23*10)".Dump();
Simulate(GetSampleList());
"Real".Dump();
Simulate(ReadMyFile(@"G:\Git\linq\adventofcode2021\input3.txt"));
}
private void Simulate(List<string> commands)
{
var list2=new List<string>();
list2.AddRange(commands);
var res = Dig(commands, 0, true).Dump();
var oxygen = ByteToString(res).Dump();
var res2 = Dig(list2, 0, false).Dump();
var scrub = ByteToString(res2).Dump();
(oxygen*scrub).Dump();
}
private string Dig(List<string> commands, int level, bool oxy)
{
if (commands.Count == 1)
return commands.FirstOrDefault();
var array = GetArray(commands);
int zeroCount = 0;
int oneCount = 0;
for (int ii = 0; ii <= commands.Count - 1; ii++)
{
if (array[ii, level] == '0')
zeroCount++;
else
oneCount++;
}
if (oxy)
{
if (oneCount >= zeroCount)
{
commands.RemoveAll(w => w[level] != '1');
}
else if (oneCount < zeroCount)
{
commands.RemoveAll(w => w[level] != '0');
}
}
else
{
if (zeroCount <= oneCount )
{
commands.RemoveAll(w => w[level] != '0');
}
else
{
commands.RemoveAll(w => w[level] != '1');
}
}
return Dig(commands, level + 1, oxy);
}
private char[,] GetArray(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++;
}
return array;
}
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