day 15
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ben Harris 2020-12-15 11:39:10 -05:00
parent 44bfb2f8ff
commit b1ca76acdc
3 changed files with 50 additions and 0 deletions

View File

@ -22,6 +22,7 @@ namespace aoc2020.test
[DataRow(typeof(Day12), "1710", "62045")]
[DataRow(typeof(Day13), "171", "539746751134958")]
[DataRow(typeof(Day14), "17481577045893", "4160009892257")]
[DataRow(typeof(Day15), "257", "8546398")]
public void CheckAllDays(Type dayType, string part1, string part2)
{
// create day instance

48
aoc2020/Day15.cs Normal file
View File

@ -0,0 +1,48 @@
using System.Linq;
namespace aoc2020
{
/// <summary>
/// Day 15: <see href="https://adventofcode.com/2020/day/15">Rambunctious Recitation</see>
/// </summary>
public sealed class Day15 : Day
{
private readonly int[] _turns;
private int _current;
private int _i;
public Day15() : base(15)
{
var initial = Input.First().Split(',').Select(int.Parse).ToArray();
_turns = new int[30_000_000];
// seed array with initial values
for (_i = 1; _i < initial.Length + 1; _i++)
_turns[initial[_i - 1]] = _i;
}
public override string Part1()
{
for (; _i != 2020; _i++)
{
var next = _turns[_current] > 0 ? _i - _turns[_current] : 0;
_turns[_current] = _i;
_current = next;
}
return $"{_current}";
}
public override string Part2()
{
for (; _i != 30_000_000; _i++)
{
var next = _turns[_current] > 0 ? _i - _turns[_current] : 0;
_turns[_current] = _i;
_current = next;
}
return $"{_current}";
}
}
}

1
aoc2020/input/day15.in Normal file
View File

@ -0,0 +1 @@
0,14,6,20,1,4