-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventOfCode2021day2part1.linq
73 lines (57 loc) · 1.15 KB
/
adventOfCode2021day2part1.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
<Query Kind="Program" />
void Main()
{
var inputList = new List<int>();
"Sample should be 150".Dump();
Simulate(GetSampleList());
"Real".Dump();
Simulate(ReadMyFile(@"G:\Git\linq\adventofcode2021\input2.txt"));
}
private void Simulate(List<string> commands){
int horPos=0;
int depth=0;
foreach (var command in commands)
{
var str= command.Split(' ');
if(int.TryParse(str[1],out int steps))
{
if(str[0]=="forward"){
horPos+=steps;
}
if (str[0] == "up")
{
depth-= steps;
}
if (str[0] == "down")
{
depth += steps;
}
}
}
horPos.Dump();
depth.Dump();
(horPos*depth).Dump();
}
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("forward 5");
newList.Add("down 5");
newList.Add("forward 8");
newList.Add("up 3");
newList.Add("down 8");
newList.Add("forward 2");
return newList;
}
// Define other methods and classes here