d10p2
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
cdf3674e4e
commit
682d0545aa
|
@ -13,7 +13,7 @@ public class DayTests
|
||||||
[DataRow(typeof(Day07), "345035", "97038163")]
|
[DataRow(typeof(Day07), "345035", "97038163")]
|
||||||
[DataRow(typeof(Day08), "362", "1020159")]
|
[DataRow(typeof(Day08), "362", "1020159")]
|
||||||
[DataRow(typeof(Day09), "478", "1327014")]
|
[DataRow(typeof(Day09), "478", "1327014")]
|
||||||
[DataRow(typeof(Day10), "288291", "")]
|
[DataRow(typeof(Day10), "288291", "820045242")]
|
||||||
public void CheckAllDays(Type dayType, string part1, string part2)
|
public void CheckAllDays(Type dayType, string part1, string part2)
|
||||||
{
|
{
|
||||||
var s = Stopwatch.StartNew();
|
var s = Stopwatch.StartNew();
|
||||||
|
|
|
@ -1,17 +1,19 @@
|
||||||
using System.Collections;
|
namespace aoc2021;
|
||||||
|
|
||||||
namespace aoc2021;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Day 10: <see href="https://adventofcode.com/2021/day/10"/>
|
/// Day 10: <see href="https://adventofcode.com/2021/day/10"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class Day10 : Day
|
public sealed class Day10 : Day
|
||||||
{
|
{
|
||||||
private readonly List<string> _input;
|
private static readonly Dictionary<char, char> MatchedBrackets = new()
|
||||||
private readonly char[] _openers = { '(', '[', '{', '<' };
|
{
|
||||||
private readonly char[] _closers = { ')', ']', '}', '>' };
|
{'(', ')'},
|
||||||
|
{'[', ']'},
|
||||||
|
{'{', '}'},
|
||||||
|
{'<', '>'}
|
||||||
|
};
|
||||||
|
|
||||||
private readonly Dictionary<char, int> _scores = new()
|
private static readonly Dictionary<char, long> Scores = new()
|
||||||
{
|
{
|
||||||
{ ')', 3 },
|
{ ')', 3 },
|
||||||
{ ']', 57 },
|
{ ']', 57 },
|
||||||
|
@ -19,45 +21,57 @@ public sealed class Day10 : Day
|
||||||
{ '>', 25137 }
|
{ '>', 25137 }
|
||||||
};
|
};
|
||||||
|
|
||||||
private readonly Dictionary<char, int> _scoresClosing = new()
|
private static readonly Dictionary<char, long> ScoresPart2 = new()
|
||||||
{
|
{
|
||||||
{ '(', 1 },
|
{ '(', 1 },
|
||||||
{ '[', 2 },
|
{ '[', 2 },
|
||||||
{ '{', 3 },
|
{ '{', 3 },
|
||||||
{ '<', 4 }
|
{ '<', 4 }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private readonly long _score1;
|
||||||
|
private readonly List<long> _scores2 = new();
|
||||||
|
|
||||||
public Day10() : base(10, "Syntax Scoring")
|
public Day10() : base(10, "Syntax Scoring")
|
||||||
{
|
{
|
||||||
_input = Input.ToList();
|
_score1 = 0L;
|
||||||
}
|
foreach (var line in Input)
|
||||||
|
|
||||||
public override string Part1()
|
|
||||||
{
|
|
||||||
var s = new Stack<char>();
|
|
||||||
var score = 0;
|
|
||||||
foreach (var line in _input)
|
|
||||||
{
|
{
|
||||||
|
var corrupt = false;
|
||||||
|
var s = new Stack<char>();
|
||||||
|
|
||||||
foreach (var c in line)
|
foreach (var c in line)
|
||||||
{
|
{
|
||||||
if (_openers.Contains(c))
|
if (ScoresPart2.ContainsKey(c))
|
||||||
{
|
{
|
||||||
s.Push(c);
|
s.Push(c);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var y = s.Pop();
|
if (c == MatchedBrackets[s.Pop()]) continue;
|
||||||
if (Math.Abs(y - c) <= 3) continue;
|
_score1 += Scores[c];
|
||||||
score += _scores[c];
|
corrupt = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return score.ToString();
|
if (corrupt) continue;
|
||||||
|
var score2 = 0L;
|
||||||
|
while (s.Any())
|
||||||
|
{
|
||||||
|
score2 *= 5;
|
||||||
|
score2 += ScoresPart2[s.Pop()];
|
||||||
|
}
|
||||||
|
|
||||||
|
_scores2.Add(score2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string Part1() => $"{_score1}";
|
||||||
|
|
||||||
public override string Part2()
|
public override string Part2()
|
||||||
{
|
{
|
||||||
|
var sorted = _scores2.OrderBy(i => i).ToList();
|
||||||
|
return $"{sorted[sorted.Count / 2]}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue