Day1.cs
· 3.7 KiB · C#
Raw
using System;
using System.IO;
using BenTek.AdventOfCode.TwentyTwentyFive.Interfaces;
namespace BenTek.AdventOfCode.TwentyTwentyFive.Days
{
class Day1 : IPuzzle
{
/// <inheritdoc />
public string ExpectedTestValue1 => "5";
public int StartingPosition => 50;
public int Limit => 99;
public string[] WrongAnswers => ["461", "5572", "7139", "7214", "7255", "6563", "8854", "6793", "8102", "7823", "7815"]; // 6684
public string TestDataPath =>
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "Day1", "sample_input.txt");
public string RealDataPath =>
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "Day1", "input.txt");
public bool Test()
{
return true;
}
public string Run()
{
string input = Utilities.FileLoader.LoadFile(RealDataPath);
return Logic(input);
}
/// <inheritdoc />
public string Logic(string input)
{
string output1 = string.Empty;
string[] lines = input.Split("\n");
if (lines == null)
{
throw new Exception("Cannot split test input data");
}
int[] values = new int[lines.Length];
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
string sign = line.Substring(0, 1);
string value = line.TrimStart(sign.ToCharArray());
int parsedValue = int.Parse(value);
int coefficient = sign.Contains("R") ? 1 : -1;
values[i] = parsedValue * coefficient;
}
int zeroCount = 0;
int wrapCount = 0;
int currentValue = StartingPosition;
for (int i = 0; i < values.Length; i++)
{
currentValue = Wrap(currentValue, values[i], ref wrapCount);
// Console.WriteLine($"The dial is rotated {values[i]} to point at {currentValue}");
if (currentValue == 0)
{
zeroCount++;
}
}
output1 = zeroCount.ToString();
string output2 = (wrapCount).ToString();
if (!CheckOutput(output1))
{
return $"Error - {output1} is wrong answer for 1";
}
if (!CheckOutput(output2))
{
return $"Error - {output2} is wrong answer for 2";
}
return $"1: {output1}, 2: {output2}";
}
private int Wrap(int currentState, int delta, ref int wrapCount)
{
bool positive = delta > 0;
int absDelta = (int)MathF.Abs(delta);
for (int i = 0; i < absDelta; i++)
{
if (positive)
{
currentState++;
}
else
{
currentState--;
}
if (currentState < 0)
{
currentState += 100;
}
else if (currentState > Limit)
{
currentState -= 100;
}
if (currentState == 0)
{
wrapCount++;
}
}
return currentState;
}
private bool CheckOutput(string output)
{
if (WrongAnswers.Contains(output))
{
return false;
}
return true;
}
}
}
| 1 | using System; |
| 2 | using System.IO; |
| 3 | using BenTek.AdventOfCode.TwentyTwentyFive.Interfaces; |
| 4 | |
| 5 | namespace 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 |