mirror of
https://github.com/jcreek/advent-of-code.git
synced 2026-07-13 03:03:46 +00:00
feat(2023-01): Add day 1
This commit is contained in:
@@ -34,6 +34,13 @@
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Y2023\Data\**" />
|
||||
<Content Include="Y2023\Data\**">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Creek.HelpfulExtensions" Version="1.4.3" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,156 @@
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace AOC.Tests.Y2023
|
||||
{
|
||||
[TestFixture, Parallelizable(ParallelScope.All)]
|
||||
public class Day01
|
||||
{
|
||||
protected string GetThisClassName() { return this.GetType().Name; }
|
||||
private string[] realData;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
realData = File.ReadAllLines(Path.Combine(TestContext.CurrentContext.TestDirectory, "Y2023", "Data", $"{GetThisClassName()}.dat"));
|
||||
}
|
||||
|
||||
[TestCase(@"1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet", 142)]
|
||||
[TestCase(null, 53080)] // The actual answer
|
||||
public void Part1(string input, int? expected)
|
||||
{
|
||||
string[] lines = input != null ? input.Split("\n") : realData;
|
||||
|
||||
int result = 0;
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
// Get all characters that are digits
|
||||
string digits = new(line.Where(char.IsDigit).ToArray());
|
||||
if (digits.Length == 1)
|
||||
{
|
||||
digits += digits;
|
||||
}
|
||||
else if (digits.Length > 2)
|
||||
{
|
||||
digits = $"{digits[0]}{digits[digits.Length - 1]}";
|
||||
}
|
||||
result += int.Parse(digits);
|
||||
}
|
||||
|
||||
if (expected != null)
|
||||
{
|
||||
Assert.That(result, Is.EqualTo(expected.Value));
|
||||
}
|
||||
|
||||
Console.WriteLine($"Part 1: {result}");
|
||||
}
|
||||
|
||||
[TestCase(@"two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen", 281)]
|
||||
[TestCase("three2six8two5", 35)]
|
||||
[TestCase("eightjzqzhrllg1oneightfck", 88)]
|
||||
[TestCase(null, 53268)] // The actual answer
|
||||
public void Part2(string input, int? expected)
|
||||
{
|
||||
string[] lines = input != null ? input.Split("\n") : realData;
|
||||
|
||||
int result = 0;
|
||||
foreach (string line in lines)
|
||||
{
|
||||
result += GetNumber(line);
|
||||
}
|
||||
|
||||
if (expected != null)
|
||||
{
|
||||
Assert.That(result, Is.EqualTo(expected.Value));
|
||||
}
|
||||
|
||||
Console.WriteLine($"Part 2: {result}");
|
||||
}
|
||||
|
||||
private record FoundMatch
|
||||
{
|
||||
public string Number { get; set; }
|
||||
public List<int> Indexes { get; set; }
|
||||
};
|
||||
|
||||
private int GetNumber(string input)
|
||||
{
|
||||
// Find every instance of a match for these strings, along with the index of their first character
|
||||
List<string> stringsToMatch = new()
|
||||
{
|
||||
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "1", "2", "3", "4", "5", "6", "7", "8", "9"
|
||||
};
|
||||
|
||||
List<FoundMatch> foundMatches = new();
|
||||
|
||||
foreach (string number in stringsToMatch)
|
||||
{
|
||||
if (!input.Contains(number))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
FoundMatch foundMatch = new()
|
||||
{
|
||||
Number = number,
|
||||
Indexes = new(),
|
||||
};
|
||||
|
||||
// Find each instance of the string within the string, and record the index of the first character for each
|
||||
int index = input.IndexOf(number, StringComparison.Ordinal);
|
||||
while (index != -1)
|
||||
{
|
||||
foundMatch.Indexes.Add(index);
|
||||
index = input.IndexOf(number, index + 1, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
foundMatches.Add(foundMatch);
|
||||
}
|
||||
|
||||
// Get the first occuring number in foundMatches, and the last occurring
|
||||
FoundMatch firstMatch = foundMatches.OrderBy(x => x.Indexes[0]).First();
|
||||
FoundMatch lastMatch = foundMatches.OrderBy(x => x.Indexes[^1]).Last();
|
||||
|
||||
Dictionary<string, string> numbersToConvert = new()
|
||||
{
|
||||
{"one", "1"},
|
||||
{"two", "2"},
|
||||
{"three", "3"},
|
||||
{"four", "4"},
|
||||
{"five", "5"},
|
||||
{"six", "6"},
|
||||
{"seven", "7"},
|
||||
{"eight", "8"},
|
||||
{"nine", "9"},
|
||||
};
|
||||
|
||||
// Get the firstDigit and lastDigit as ints, converting any words using the numbersToConvert dictionary
|
||||
string firstDigit = firstMatch.Number;
|
||||
if (numbersToConvert.ContainsKey(firstMatch.Number))
|
||||
{
|
||||
firstDigit = numbersToConvert[firstMatch.Number];
|
||||
}
|
||||
string lastDigit = lastMatch.Number;
|
||||
if (numbersToConvert.ContainsKey(lastMatch.Number))
|
||||
{
|
||||
lastDigit = numbersToConvert[lastMatch.Number];
|
||||
}
|
||||
|
||||
int finalNumber = int.Parse($"{firstDigit}{lastDigit}");
|
||||
Console.WriteLine(finalNumber);
|
||||
|
||||
return finalNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
--- Day 1: Trebuchet?! ---
|
||||
Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems.
|
||||
|
||||
You've been doing this long enough to know that to restore snow operations, you need to check all fifty stars by December 25th.
|
||||
|
||||
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
|
||||
|
||||
You try to ask why they can't just use a weather machine ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a trebuchet ("please hold still, we need to strap you in").
|
||||
|
||||
As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been amended by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.
|
||||
|
||||
The newly-improved calibration document consists of lines of text; each line originally contained a specific calibration value that the Elves now need to recover. On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.
|
||||
|
||||
For example:
|
||||
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
||||
In this example, the calibration values of these four lines are 12, 38, 15, and 77. Adding these together produces 142.
|
||||
|
||||
Consider your entire calibration document. What is the sum of all of the calibration values?
|
||||
|
||||
--- Part Two ---
|
||||
Your calculation isn't quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid "digits".
|
||||
|
||||
Equipped with this new information, you now need to find the real first and last digit on each line. For example:
|
||||
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
||||
In this example, the calibration values are 29, 83, 13, 24, 42, 14, and 76. Adding these together produces 281.
|
||||
|
||||
What is the sum of all of the calibration values?
|
||||
Reference in New Issue
Block a user