From 78e2ce126d670cdd1cb0e2c5b566de515935a706 Mon Sep 17 00:00:00 2001 From: Parnic Date: Thu, 10 Dec 2020 15:05:09 -0600 Subject: [PATCH] Day 10 + small perf checks --- 10input.txt | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2020.csproj | 3 ++ Program.cs | 3 ++ Q09.cs | 8 +++++ Q10.cs | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 182 insertions(+) create mode 100644 10input.txt create mode 100644 Q10.cs diff --git a/10input.txt b/10input.txt new file mode 100644 index 0000000..706664b --- /dev/null +++ b/10input.txt @@ -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 \ No newline at end of file diff --git a/2020.csproj b/2020.csproj index 13127c9..a655d06 100644 --- a/2020.csproj +++ b/2020.csproj @@ -38,6 +38,9 @@ PreserveNewest + + PreserveNewest + diff --git a/Program.cs b/Program.cs index 08b9000..82c9a70 100644 --- a/Program.cs +++ b/Program.cs @@ -4,6 +4,7 @@ { static void Main(string[] args) { + var start = System.DateTime.Now; Q01.Go(); Q02.Go(); Q03.Go(); @@ -13,6 +14,8 @@ Q07.Go(); Q08.Go(); Q09.Go(); + Q10.Go(); + System.Diagnostics.Debug.WriteLine($"Total time={(System.DateTime.Now - start).TotalMilliseconds}ms"); } } } diff --git a/Q09.cs b/Q09.cs index 638e1e1..94afa7c 100644 --- a/Q09.cs +++ b/Q09.cs @@ -13,9 +13,17 @@ namespace _2020 public static void Go() { + var start = DateTime.Now; MakeList(); + Debug.WriteLine($"Q09 MakeList took {(DateTime.Now - start).TotalMilliseconds}ms"); + var p1start = DateTime.Now; Part1(); + Debug.WriteLine($"Q09 part1 took {(DateTime.Now - p1start).TotalMilliseconds}ms"); + var p2start = DateTime.Now; Part2(); + Debug.WriteLine($"Q09 part2 took {(DateTime.Now - p2start).TotalMilliseconds}ms"); + + Debug.WriteLine($"Q09 took {(DateTime.Now - start).TotalMilliseconds}ms"); } static void MakeList() diff --git a/Q10.cs b/Q10.cs new file mode 100644 index 0000000..66f2729 --- /dev/null +++ b/Q10.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; + +namespace _2020 +{ + class Q10 + { + static List list = new List(); + static List 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(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(); + 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]}"); + } + } +}