mirror of
https://github.com/parnic/advent-of-code-2023.git
synced 2025-06-16 16:50:14 -05:00
Day 1
It's messy and it took me a frighteningly long time to get part 2 correct (not a good sign...) but it is finished.
This commit is contained in:
@ -26,6 +26,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Remove="inputs\01.txt" />
|
<None Remove="inputs\01.txt" />
|
||||||
<None Remove="inputs\01a.txt" />
|
<None Remove="inputs\01a.txt" />
|
||||||
|
<None Remove="inputs\01b.txt" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@ -33,4 +34,8 @@
|
|||||||
<EmbeddedResource Include="inputs\01a.txt" />
|
<EmbeddedResource Include="inputs\01a.txt" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<EmbeddedResource Include="inputs\01b.txt" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
3251
inputs/01.txt
3251
inputs/01.txt
File diff suppressed because it is too large
Load Diff
@ -1,14 +1,4 @@
|
|||||||
1000
|
1abc2
|
||||||
2000
|
pqr3stu8vwx
|
||||||
3000
|
a1b2c3d4e5f
|
||||||
|
treb7uchet
|
||||||
4000
|
|
||||||
|
|
||||||
5000
|
|
||||||
6000
|
|
||||||
|
|
||||||
7000
|
|
||||||
8000
|
|
||||||
9000
|
|
||||||
|
|
||||||
10000
|
|
7
inputs/01b.txt
Normal file
7
inputs/01b.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
two1nine
|
||||||
|
eightwothree
|
||||||
|
abcone2threexyz
|
||||||
|
xtwone3four
|
||||||
|
4nineeightseven2
|
||||||
|
zoneight234
|
||||||
|
7pqrstsixteen
|
82
src/01.cs
82
src/01.cs
@ -2,22 +2,92 @@
|
|||||||
|
|
||||||
internal class Day01 : Day
|
internal class Day01 : Day
|
||||||
{
|
{
|
||||||
|
private readonly List<string> numStrs = new()
|
||||||
|
{
|
||||||
|
"1",
|
||||||
|
"one",
|
||||||
|
"2",
|
||||||
|
"two",
|
||||||
|
"3",
|
||||||
|
"three",
|
||||||
|
"4",
|
||||||
|
"four",
|
||||||
|
"5",
|
||||||
|
"five",
|
||||||
|
"6",
|
||||||
|
"six",
|
||||||
|
"7",
|
||||||
|
"seven",
|
||||||
|
"8",
|
||||||
|
"eight",
|
||||||
|
"9",
|
||||||
|
"nine",
|
||||||
|
};
|
||||||
|
|
||||||
|
private readonly Dictionary<string, string> strs = new()
|
||||||
|
{
|
||||||
|
{"one", "1" },
|
||||||
|
{"two", "2" },
|
||||||
|
{"three", "3" },
|
||||||
|
{"four", "4" },
|
||||||
|
{"five", "5" },
|
||||||
|
{"six", "6" },
|
||||||
|
{"seven", "7" },
|
||||||
|
{"eight", "8" },
|
||||||
|
{"nine", "9" },
|
||||||
|
};
|
||||||
|
|
||||||
|
private readonly List<int> nums = new();
|
||||||
|
private readonly List<int> num2 = new();
|
||||||
internal override void Parse()
|
internal override void Parse()
|
||||||
{
|
{
|
||||||
var lines = Util.Parsing.ReadAllLines($"{GetDay()}");
|
var lines = Util.Parsing.ReadAllLines($"{GetDay()}");
|
||||||
|
foreach (var line in lines)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var firstNum = line.First(c => c >= '1' && c <= '9');
|
||||||
|
var lastNum = line.Last(c => c >= '1' && c <= '9');
|
||||||
|
nums.Add(int.Parse($"{firstNum}{lastNum}"));
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
List<int> digits = new();
|
||||||
|
for (int i = 0; i < line.Length; i++)
|
||||||
|
{
|
||||||
|
var s = line[..(i+1)];
|
||||||
|
foreach (var ns in numStrs)
|
||||||
|
{
|
||||||
|
if (s.EndsWith(ns))
|
||||||
|
{
|
||||||
|
s = ns;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (numStrs.Contains(s))
|
||||||
|
{
|
||||||
|
if (strs.TryGetValue(s, out var str))
|
||||||
|
{
|
||||||
|
s = str;
|
||||||
|
}
|
||||||
|
digits.Add(s[0] - '0');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
num2.Add((digits.First() * 10) + digits.Last());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override string Part1()
|
internal override string Part1()
|
||||||
{
|
{
|
||||||
|
long total = nums.Sum();
|
||||||
|
return $"Calibration sum for literal digits: <+white>{total}";
|
||||||
return $"<+white>";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override string Part2()
|
internal override string Part2()
|
||||||
{
|
{
|
||||||
|
long total = num2.Sum();
|
||||||
|
return $"Calibration sum for spelled and literal digits: <+white>{total}";
|
||||||
return $"<+white>";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user