-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventOfCode2021day7part2.linq
67 lines (45 loc) · 1.22 KB
/
adventOfCode2021day7part2.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
<Query Kind="Program" />
void Main()
{
var inputList = new List<int>();
"Sample should be 168".Dump();
Simulate(GetSampleList());
"Real ".Dump();
Simulate(ReadMyFile(@"G:\Git\linq\adventofcode2021\input7.txt"));
}
private void Simulate(List<string> commands)
{
var positions = commands.FirstOrDefault().Split(',').Select(int.Parse).ToList();
GetCheapestPosition(positions, false).Dump();
}
private int GetCheapestPosition(List<int> positions, bool burnAtConstantRate) =>
Enumerable.Range(0, positions.Max() + 1)
.Aggregate(int.MaxValue, (lowestFuel, pos) => Math
.Min(lowestFuel, positions
.Where(x => x != pos)
.Aggregate(0, (a, b) =>
{
var distance = Math.Abs(pos - b);
return a + (burnAtConstantRate
? distance
: (distance * (distance + 1)) / 2);
}))
);
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("16,1,2,0,4,2,7,1,2,14");
return newList;
}
// Define other methods and classes here