Last active 5 hours ago

My implementation of solving Day 1 of 2025's Advent of Code.

Day1.cs Raw
1using System;
2using System.IO;
3using BenTek.AdventOfCode.TwentyTwentyFive.Interfaces;
4
5namespace BenTek.AdventOfCode.TwentyTwentyFive.Days
6{
7 class Day1 : IPuzzle
8 {
9 /// <inheritdoc />
10 public string ExpectedTestValue1 => "5";
11
12 public int StartingPosition => 50;
13 public int Limit => 99;
14 public string[] WrongAnswers => ["461", "5572", "7139", "7214", "7255", "6563", "8854", "6793", "8102", "7823", "7815"]; // 6684
15
16 public string TestDataPath =>
17 Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "Day1", "sample_input.txt");
18 public string RealDataPath =>
19 Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "Day1", "input.txt");
20
21 public bool Test()
22 {
23 return true;
24 }
25
26 public string Run()
27 {
28 string input = Utilities.FileLoader.LoadFile(RealDataPath);
29 return Logic(input);
30 }
31
32 /// <inheritdoc />
33 public string Logic(string input)
34 {
35 string output1 = string.Empty;
36
37 string[] lines = input.Split("\n");
38 if (lines == null)
39 {
40 throw new Exception("Cannot split test input data");
41 }
42
43 int[] values = new int[lines.Length];
44
45 for (int i = 0; i < lines.Length; i++)
46 {
47 string line = lines[i];
48 string sign = line.Substring(0, 1);
49 string value = line.TrimStart(sign.ToCharArray());
50 int parsedValue = int.Parse(value);
51 int coefficient = sign.Contains("R") ? 1 : -1;
52 values[i] = parsedValue * coefficient;
53 }
54
55 int zeroCount = 0;
56 int wrapCount = 0;
57 int currentValue = StartingPosition;
58
59 for (int i = 0; i < values.Length; i++)
60 {
61 currentValue = Wrap(currentValue, values[i], ref wrapCount);
62
63 // Console.WriteLine($"The dial is rotated {values[i]} to point at {currentValue}");
64
65 if (currentValue == 0)
66 {
67 zeroCount++;
68 }
69 }
70
71 output1 = zeroCount.ToString();
72 string output2 = (wrapCount).ToString();
73
74 if (!CheckOutput(output1))
75 {
76 return $"Error - {output1} is wrong answer for 1";
77 }
78
79 if (!CheckOutput(output2))
80 {
81 return $"Error - {output2} is wrong answer for 2";
82 }
83
84 return $"1: {output1}, 2: {output2}";
85 }
86
87 private int Wrap(int currentState, int delta, ref int wrapCount)
88 {
89 bool positive = delta > 0;
90 int absDelta = (int)MathF.Abs(delta);
91
92 for (int i = 0; i < absDelta; i++)
93 {
94 if (positive)
95 {
96 currentState++;
97 }
98 else
99 {
100 currentState--;
101 }
102
103 if (currentState < 0)
104 {
105 currentState += 100;
106 }
107 else if (currentState > Limit)
108 {
109 currentState -= 100;
110 }
111
112 if (currentState == 0)
113 {
114 wrapCount++;
115 }
116 }
117
118 return currentState;
119 }
120
121 private bool CheckOutput(string output)
122 {
123 if (WrongAnswers.Contains(output))
124 {
125 return false;
126 }
127
128 return true;
129 }
130 }
131}
132