Day 10 + small perf checks

This commit is contained in:
2020-12-10 15:05:09 -06:00
parent 921c59ffca
commit 78e2ce126d
5 changed files with 182 additions and 0 deletions

90
10input.txt Normal file
View File

@ -0,0 +1,90 @@
114
51
122
26
121
90
20
113
8
138
57
44
135
76
134
15
21
119
52
118
107
99
73
72
106
41
129
83
19
66
132
56
32
79
27
115
112
58
102
64
50
2
39
3
77
85
103
140
28
133
78
34
13
61
25
35
89
40
7
24
33
96
108
71
11
128
92
111
55
80
91
31
70
101
14
18
12
4
84
125
120
100
65
86
93
67
139
1
47
38

View File

@ -38,6 +38,9 @@
<None Update="09input.txt"> <None Update="09input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="10input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -4,6 +4,7 @@
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
var start = System.DateTime.Now;
Q01.Go(); Q01.Go();
Q02.Go(); Q02.Go();
Q03.Go(); Q03.Go();
@ -13,6 +14,8 @@
Q07.Go(); Q07.Go();
Q08.Go(); Q08.Go();
Q09.Go(); Q09.Go();
Q10.Go();
System.Diagnostics.Debug.WriteLine($"Total time={(System.DateTime.Now - start).TotalMilliseconds}ms");
} }
} }
} }

8
Q09.cs
View File

@ -13,9 +13,17 @@ namespace _2020
public static void Go() public static void Go()
{ {
var start = DateTime.Now;
MakeList(); MakeList();
Debug.WriteLine($"Q09 MakeList took {(DateTime.Now - start).TotalMilliseconds}ms");
var p1start = DateTime.Now;
Part1(); Part1();
Debug.WriteLine($"Q09 part1 took {(DateTime.Now - p1start).TotalMilliseconds}ms");
var p2start = DateTime.Now;
Part2(); Part2();
Debug.WriteLine($"Q09 part2 took {(DateTime.Now - p2start).TotalMilliseconds}ms");
Debug.WriteLine($"Q09 took {(DateTime.Now - start).TotalMilliseconds}ms");
} }
static void MakeList() static void MakeList()

78
Q10.cs Normal file
View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace _2020
{
class Q10
{
static List<long> list = new List<long>();
static List<long> sortedList = null;
public static void Go()
{
var start = DateTime.Now;
MakeList();
Debug.WriteLine($"Q10 MakeList took {(DateTime.Now - start).TotalMilliseconds}ms");
var p1start = DateTime.Now;
Part1();
Debug.WriteLine($"Q10 part1 took {(DateTime.Now - p1start).TotalMilliseconds}ms");
var p2start = DateTime.Now;
Part2();
Debug.WriteLine($"Q10 part2 took {(DateTime.Now - p2start).TotalMilliseconds}ms");
Debug.WriteLine($"Q10 took {(DateTime.Now - start).TotalMilliseconds}ms");
}
static void MakeList()
{
list.Add(0);
foreach (var line in File.ReadAllLines("10input.txt"))
{
list.Add(Convert.ToInt64(line));
}
sortedList = new List<long>(list);
sortedList.Sort();
sortedList.Add(sortedList[^1] + 3);
}
static void Part1()
{
var diffs = new int[3];
for (int i = 1; i < sortedList.Count; i++)
{
diffs[sortedList[i] - sortedList[i - 1] - 1]++;
}
Debug.WriteLine($"Q10Part1: device joltage={sortedList[^1]}, diffs by 1={diffs[0]}, diffs by 3={diffs[2]}, multiplied={diffs[0] * diffs[2]}");
}
static void Part2()
{
var pathsToIndices = new List<long>();
pathsToIndices.Add(1);
for (int i = 1; i < sortedList.Count; i++)
{
long pathLen = 0;
for (int j = i - 1; j >= 0; j--)
{
if (sortedList[j] + 3 >= sortedList[i])
{
pathLen += pathsToIndices[j];
}
else
{
break;
}
}
pathsToIndices.Add(pathLen);
}
Debug.WriteLine($"Q10Part2: combinations={pathsToIndices[^1]}");
}
}
}