|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.IO; |
| 4 | +using System.Linq; |
4 | 5 | using System.Runtime; |
5 | 6 | using System.Security.Cryptography.X509Certificates; |
6 | 7 | using System.Text.RegularExpressions; |
@@ -45,6 +46,60 @@ public void SolvePart1() |
45 | 46 |
|
46 | 47 | public void SolvePart2() |
47 | 48 | { |
48 | | - throw new NotImplementedException(); |
| 49 | + //var inputFilePath = GetInputFilePath("testtwo"); |
| 50 | + var inputFilePath = GetInputFilePath(); |
| 51 | + |
| 52 | + string[] lines = File.ReadAllLines(inputFilePath); |
| 53 | + |
| 54 | + string regexpattern = @"(?:\w*mul\(\d{1,3},\d{1,3}\))|(?:\b(?:un)?do\(\))|(?:\bdon't\(\))"; |
| 55 | + |
| 56 | + List<string> allmatches = new(); |
| 57 | + |
| 58 | + long totalSum = 0; |
| 59 | + bool doit = true; |
| 60 | + foreach (var line in lines) |
| 61 | + { |
| 62 | + var matches = Regex.Matches(line, regexpattern); |
| 63 | + |
| 64 | + foreach(Match match in matches) |
| 65 | + { |
| 66 | + allmatches.Add(match.Value); |
| 67 | + |
| 68 | + if(match.ToString().Contains("mul")) |
| 69 | + { |
| 70 | + if (doit) |
| 71 | + { |
| 72 | + string temp = match.Value; |
| 73 | + |
| 74 | + if (temp.Contains("mul")) |
| 75 | + { |
| 76 | + // Normalize anything ending in "mul" (e.g., xmul, _mul) to "mul" |
| 77 | + temp = Regex.Replace(temp, @"\w*mul", "mul"); |
| 78 | + } |
| 79 | + |
| 80 | + string item = temp.Replace("mul(", "").Replace(")", ""); |
| 81 | + |
| 82 | + long x = long.Parse(item.Split(',')[0]); |
| 83 | + long y = long.Parse(item.Split(',')[1]); |
| 84 | + |
| 85 | + totalSum += x * y; |
| 86 | + } |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + if (match.ToString().ToLower().Contains("don't")) |
| 91 | + { |
| 92 | + doit = false; |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + doit = true; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + Console.WriteLine($"total sum is: {totalSum}"); |
49 | 104 | } |
50 | 105 | } |
0 commit comments