prefer expression body
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Ben Harris 2021-11-12 17:56:41 -05:00
parent b599190409
commit 3ba6c967f8
23 changed files with 58 additions and 157 deletions

View File

@ -1,5 +1,5 @@
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
namespace aoc2020.test;

View File

@ -7,10 +7,8 @@ public sealed class Day01 : Day
{
private readonly ImmutableHashSet<int> _entries;
public Day01() : base(1, "Report Repair")
{
public Day01() : base(1, "Report Repair") =>
_entries = Input.Select(int.Parse).ToImmutableHashSet();
}
public override string Part1()
{

View File

@ -7,20 +7,14 @@ public sealed class Day02 : Day
{
private readonly ImmutableList<Password> _passwords;
public Day02() : base(2, "Password Philosophy")
{
public Day02() : base(2, "Password Philosophy") =>
_passwords = Input.Select(p => new Password(p)).ToImmutableList();
}
public override string Part1()
{
return $"{_passwords.Count(p => p.IsValid)}";
}
public override string Part1() =>
$"{_passwords.Count(p => p.IsValid)}";
public override string Part2()
{
return $"{_passwords.Count(p => p.IsValidByIndex)}";
}
public override string Part2() =>
$"{_passwords.Count(p => p.IsValidByIndex)}";
private class Password
{

View File

@ -24,10 +24,7 @@ public sealed class Day03 : Day
return hits;
}
public override string Part1()
{
return $"{CountSlope(3, 1)}";
}
public override string Part1() => $"{CountSlope(3, 1)}";
public override string Part2()
{

View File

@ -27,15 +27,9 @@ public sealed class Day04 : Day
if (a.Any()) _passports.Add(Passport.Parse(a));
}
public override string Part1()
{
return $"{_passports.Count(p => p.IsValid)}";
}
public override string Part1() => $"{_passports.Count(p => p.IsValid)}";
public override string Part2()
{
return $"{_passports.Count(p => p.ExtendedValidation())}";
}
public override string Part2() => $"{_passports.Count(p => p.ExtendedValidation())}";
private class Passport
{

View File

@ -7,23 +7,16 @@ public sealed class Day05 : Day
{
private readonly ImmutableHashSet<int> _ids;
public Day05() : base(5, "Binary Boarding")
{
public Day05() : base(5, "Binary Boarding") =>
_ids = Input
.Select(s =>
Convert.ToInt32(s.Replace('F', '0').Replace('B', '1').Replace('L', '0').Replace('R', '1'), 2))
.OrderBy(i => i)
.ToImmutableHashSet();
}
public override string Part1()
{
return $"{_ids.Last()}";
}
public override string Part1() => $"{_ids.Last()}";
public override string Part2()
{
public override string Part2() =>
// arithmetic sum of full series
return $"{(_ids.Count + 1) * (_ids.First() + _ids.Last()) / 2 - _ids.Sum()}";
}
$"{(_ids.Count + 1) * (_ids.First() + _ids.Last()) / 2 - _ids.Sum()}";
}

View File

@ -38,13 +38,7 @@ public sealed class Day06 : Day
}
}
public override string Part1()
{
return $"{_countPart1}";
}
public override string Part1() => $"{_countPart1}";
public override string Part2()
{
return $"{_countPart2}";
}
public override string Part2() => $"{_countPart2}";
}

View File

@ -7,9 +7,9 @@ public sealed class Day07 : Day
{
private readonly Dictionary<string, IEnumerable<(int Weight, string Name)?>> _rules;
public Day07() : base(7, "Handy Haversacks")
{
_rules = Input.Select(rule =>
public Day07() : base(7, "Handy Haversacks") =>
_rules = Input
.Select(rule =>
{
var spl = rule.Split(" bags contain ", 2);
var outer = string.Join(' ', spl[0].Split(' ').Take(2));
@ -17,7 +17,6 @@ public sealed class Day07 : Day
return (outer, inner);
})
.ToDictionary(t => t.outer, t => t.inner);
}
private static (int, string)? ParseQuantity(string arg)
{
@ -51,8 +50,5 @@ public sealed class Day07 : Day
return $"{p.Count}";
}
public override string Part2()
{
return $"{Weight("shiny gold") - 1}";
}
public override string Part2() => $"{Weight("shiny gold") - 1}";
}

View File

@ -9,10 +9,8 @@ public sealed class Day08 : Day
private int _accumulator;
private int _currentInstruction;
public Day08() : base(8, "Handheld Halting")
{
public Day08() : base(8, "Handheld Halting") =>
_instructions = Input.Select(ParseLine).ToArray();
}
private static (string, int) ParseLine(string line)
{

View File

@ -8,10 +8,8 @@ public sealed class Day09 : Day
private readonly long[] _list;
private long _part1;
public Day09() : base(9, "Encoding Error")
{
public Day09() : base(9, "Encoding Error") =>
_list = Input.Select(long.Parse).ToArray();
}
public override string Part1()
{

View File

@ -48,8 +48,5 @@ public sealed class Day10 : Day
return $"{ones * threes}";
}
public override string Part2()
{
return $"{Connections(0)}";
}
public override string Part2() => $"{Connections(0)}";
}

View File

@ -64,10 +64,7 @@ public sealed class Day11 : Day
_w = Grid[0].Length;
}
private LifeGame()
{
Grid = Array.Empty<char[]>();
}
private LifeGame() => Grid = Array.Empty<char[]>();
public int TotalSeated =>
Grid.Sum(l => l.Count(c => c == '#'));
@ -115,7 +112,7 @@ public sealed class Day11 : Day
return next;
}
private int CanSee(int y, int x) =>
private int CanSee(int y, int x) =>
new[]
{
TraceRay(y, x, -1, -1), TraceRay(y, x, -1, +0), TraceRay(y, x, -1, +1),

View File

@ -9,10 +9,7 @@ public sealed class Day12 : Day
{
}
private static void Swap(ref int x, ref int y)
{
(y, x) = (x, y);
}
private static void Swap(ref int x, ref int y) => (y, x) = (x, y);
private (int x, int y, int sx, int sy) ProcessInstructions()
{

View File

@ -5,14 +5,12 @@ namespace aoc2020;
/// </summary>
public sealed class Day17 : Day
{
private readonly Dictionary<(int x, int y, int z), char> _plane;
private readonly Dictionary<(int x, int y, int z, int w), char> _plane4;
private readonly Dictionary<(int x, int y, int z), char> _plane = new();
private readonly Dictionary<(int x, int y, int z, int w), char> _plane4 = new();
public Day17() : base(17, "Conway Cubes")
{
_plane = new Dictionary<(int x, int y, int z), char>();
_plane4 = new Dictionary<(int x, int y, int z, int w), char>();
var input = Input.ToList();
for (var x = 0; x < 32; x++)

View File

@ -7,10 +7,8 @@ public sealed class Day18 : Day
{
private readonly List<string> _expressions;
public Day18() : base(18, "Operation Order")
{
public Day18() : base(18, "Operation Order") =>
_expressions = Input.Select(line => line.Replace(" ", "")).ToList();
}
private static long Calculate(string expr, Func<char, int> precedence)
{
@ -63,13 +61,9 @@ public sealed class Day18 : Day
return expressionStack.Pop();
}
public override string Part1()
{
return $"{_expressions.Sum(expr => Calculate(expr, c => c == '+' || c == '*' ? 1 : 0))}";
}
public override string Part1() =>
$"{_expressions.Sum(expr => Calculate(expr, c => c == '+' || c == '*' ? 1 : 0))}";
public override string Part2()
{
return $"{_expressions.Sum(expr => Calculate(expr, c => c switch { '+' => 2, '*' => 1, _ => 0 }))}";
}
public override string Part2() =>
$"{_expressions.Sum(expr => Calculate(expr, c => c switch { '+' => 2, '*' => 1, _ => 0 }))}";
}

View File

@ -17,14 +17,16 @@ public sealed class Day19 : Day
val: a[1].Split('|', StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Split(' ', StringSplitOptions.RemoveEmptyEntries)).ToArray()))
.ToDictionary(a => a.key, a => a.val);
_messages = Input.Skip(_rules.Count + 1).ToArray();
_stack = new Stack<string>();
_stack = new();
}
private string MakeRegexExpression(string key)
{
if (_stack.Count(s => s == key) > 10) return "x";
_stack.Push(key);
var sub = string.Join("|", _rules[key].Select(test => test.Length switch
{
1 => test[0][0] == '"' ? test[0].Trim('"') : MakeRegexExpression(test[0]),

View File

@ -111,15 +111,11 @@ public sealed class Day20 : Day
return roughness - seaMonsterCount * seaMonsterTiles;
}
public override string Part1()
{
return $"{_topLefts.Select(t => t.TileId).Distinct().Aggregate(1L, (acc, next) => acc * next)}";
}
public override string Part1() =>
$"{_topLefts.Select(t => t.TileId).Distinct().Aggregate(1L, (acc, next) => acc * next)}";
public override string Part2()
{
return $"{_topLefts.Select(Roughness).First(r => r > 0)}";
}
public override string Part2() =>
$"{_topLefts.Select(Roughness).First(r => r > 0)}";
private record Tile(int TileId, char[][] Pixels)
{
@ -129,23 +125,14 @@ public sealed class Day20 : Day
internal int LeftId => GetId(z => (0, z));
internal int RightId => GetId(z => (Size - 1, z));
private int GetId(Func<int, (int x, int y)> selector)
{
return Enumerable.Range(0, Size)
private int GetId(Func<int, (int x, int y)> selector) => Enumerable.Range(0, Size)
.Select(selector)
.Select((c, i) => (Pixels[c.x][c.y] == '#' ? 1 : 0) << i)
.Aggregate(0, (acc, next) => acc | next);
}
private Tile RotateClockwise()
{
return Transform((x, y, newPixels) => newPixels[x][Size - 1 - y] = Pixels[y][x]);
}
private Tile RotateClockwise() => Transform((x, y, newPixels) => newPixels[x][Size - 1 - y] = Pixels[y][x]);
private Tile Flip()
{
return Transform((x, y, newPixels) => newPixels[y][Size - 1 - x] = Pixels[y][x]);
}
private Tile Flip() => Transform((x, y, newPixels) => newPixels[y][Size - 1 - x] = Pixels[y][x]);
private Tile Transform(Action<int, int, char[][]> transformFunc)
{
@ -170,9 +157,6 @@ public sealed class Day20 : Day
}
}
public string Format()
{
return $"Tile {TileId}:\n{string.Join("\n", Pixels.Select(p => new string(p)))}";
}
public string Format() => $"Tile {TileId}:\n{string.Join("\n", Pixels.Select(p => new string(p)))}";
}
}

View File

@ -12,7 +12,7 @@ public sealed class Day21 : Day
{
_parsedFoods = Input.Select(line => line.TrimEnd(')').Split(" (contains "))
.Select(split => (Allergens: split[1].Split(", "), Ingredients: split[0].Split(' ')));
_dangerousFoods = _parsedFoods
.SelectMany(i => i.Allergens.Select(Allergen => (Allergen, i.Ingredients)))
.GroupBy(
@ -38,7 +38,7 @@ public sealed class Day21 : Day
var part1 = _parsedFoods
.SelectMany(i => i.Ingredients)
.Count(i => !_dangerousFoods.Select(t => t.Ingredient).Contains(i));
return $"{part1}";
}
@ -47,7 +47,7 @@ public sealed class Day21 : Day
var part2 = _dangerousFoods
.OrderBy(i => i.Allergen)
.Select(i => i.Ingredient);
return $"{string.Join(',', part2)}";
}
}

View File

@ -9,13 +9,7 @@ public sealed class Day22 : Day
{
}
public override string Part1()
{
return "";
}
public override string Part1() => "";
public override string Part2()
{
return "";
}
public override string Part2() => "";
}

View File

@ -9,13 +9,7 @@ public sealed class Day23 : Day
{
}
public override string Part1()
{
return "";
}
public override string Part1() => "";
public override string Part2()
{
return "";
}
public override string Part2() => "";
}

View File

@ -9,13 +9,7 @@ public sealed class Day24 : Day
{
}
public override string Part1()
{
return "";
}
public override string Part1() => "";
public override string Part2()
{
return "";
}
public override string Part2() => "";
}

View File

@ -9,13 +9,7 @@ public sealed class Day25 : Day
{
}
public override string Part1()
{
return "";
}
public override string Part1() => "";
public override string Part2()
{
return "";
}
public override string Part2() => "";
}

View File

@ -14,13 +14,7 @@ public static class Extensions
/// </summary>
/// <param name="stopwatch"></param>
/// <returns></returns>
public static double ScaleMilliseconds(this Stopwatch stopwatch)
{
return 1_000 * stopwatch.ElapsedTicks / (double)Stopwatch.Frequency;
}
public static double ScaleMilliseconds(this Stopwatch stopwatch) => 1_000 * stopwatch.ElapsedTicks / (double)Stopwatch.Frequency;
public static bool Contains(this Range range, int i)
{
return i >= range.Start.Value && i <= range.End.Value;
}
public static bool Contains(this Range range, int i) => i >= range.Start.Value && i <= range.End.Value;
}