-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventOfCode2021day6part2.linq
94 lines (71 loc) · 1.47 KB
/
adventOfCode2021day6part2.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
<Query Kind="Program" />
void Main()
{
var inputList = new List<int>();
"Sample should be 26984457539".Dump();
Simulate(GetSampleList());
"Real".Dump();
Simulate(ReadMyFile(@"G:\Git\linq\adventofcode2021\input6.txt"));
}
private void Simulate(List<string> commands)
{
int day = 0;
var initialState = commands.FirstOrDefault();
var dic = new Dictionary<int, long>();
for (int i = 0; i <= 8; i++)
{
dic.Add(i, 0);
}
var split = initialState.Split(',');
foreach (var state in split)
{
var con = int.Parse(state);
if (dic.ContainsKey(con))
{
dic[con] = dic[con] + 1;
}
else
{
dic.Add(con, 1);
}
}
for (int i = 0; i < 256; i++)
{
DoDay(dic, 8, 0);
}
dic.Dump();
}
private void DoDay(Dictionary<int, long> dic, int stage, long toAdd)
{
bool goToNextIfZero=dic[0]>0;
if (stage == 0)
{
dic[6] = dic[6] + dic[0];
dic[8] = dic[8] + dic[0];
dic[0] = toAdd;
return;
}
var temp = dic[stage];
dic[stage] = dic[stage] + toAdd;
dic[stage] = dic[stage] - temp;
if((stage==0 && goToNextIfZero) || stage>0)
DoDay(dic, stage - 1, temp);
}
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("3,4,3,1,2");
return newList;
}
// Define other methods and classes here