day 10 part 2
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
adds the socket and device to the list and reverts the 1s and 3s counts back to 0. algorithm design from - https://math.stackexchange.com/q/1118670 - https://oeis.org/A249631
This commit is contained in:
parent
89c32f6074
commit
608de1ff5f
|
@ -17,7 +17,7 @@ namespace aoc2020.test
|
||||||
[DataRow(typeof(Day07), "169", "82372")]
|
[DataRow(typeof(Day07), "169", "82372")]
|
||||||
[DataRow(typeof(Day08), "1654", "833")]
|
[DataRow(typeof(Day08), "1654", "833")]
|
||||||
[DataRow(typeof(Day09), "138879426", "23761694")]
|
[DataRow(typeof(Day09), "138879426", "23761694")]
|
||||||
[DataRow(typeof(Day10), "1980", "")]
|
[DataRow(typeof(Day10), "1980", "4628074479616")]
|
||||||
public void CheckAllDays(Type dayType, string part1, string part2)
|
public void CheckAllDays(Type dayType, string part1, string part2)
|
||||||
{
|
{
|
||||||
// create day instance
|
// create day instance
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO.Enumeration;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace aoc2020
|
namespace aoc2020
|
||||||
|
@ -10,34 +9,51 @@ namespace aoc2020
|
||||||
public sealed class Day10 : Day
|
public sealed class Day10 : Day
|
||||||
{
|
{
|
||||||
private readonly int[] _adapters;
|
private readonly int[] _adapters;
|
||||||
|
private readonly long[] _memo;
|
||||||
|
|
||||||
public Day10() : base(10)
|
public Day10() : base(10)
|
||||||
{
|
{
|
||||||
_adapters = Input.Select(int.Parse).OrderBy(i => i).ToArray();
|
var parsed = Input.Select(int.Parse).ToArray();
|
||||||
|
// add socket and device to the list
|
||||||
|
_adapters = parsed.Concat(new[] {0, parsed.Max() + 3}).OrderBy(i => i).ToArray();
|
||||||
|
_memo = new long[_adapters.Length];
|
||||||
|
}
|
||||||
|
|
||||||
|
private long Connections(int i)
|
||||||
|
{
|
||||||
|
if (i == _adapters.Length - 1) _memo[i] = 1;
|
||||||
|
if (_memo[i] > 0) return _memo[i];
|
||||||
|
|
||||||
|
for (var j = i + 1; j <= i + 3 && j < _adapters.Length; j++)
|
||||||
|
if (_adapters[j] - _adapters[i] <= 3)
|
||||||
|
_memo[i] += Connections(j);
|
||||||
|
|
||||||
|
return _memo[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part1()
|
public override string Part1()
|
||||||
{
|
{
|
||||||
var ones = 1;
|
var ones = 0;
|
||||||
var threes = 1;
|
var threes = 0;
|
||||||
|
|
||||||
for (var i = 0; i < _adapters.Length - 1; i++)
|
for (var i = 0; i < _adapters.Length - 1; i++)
|
||||||
{
|
switch (_adapters[i + 1] - _adapters[i])
|
||||||
var difference = _adapters[i + 1] - _adapters[i];
|
|
||||||
switch (difference)
|
|
||||||
{
|
{
|
||||||
case 1: ones++; break;
|
case 1:
|
||||||
case 3: threes++; break;
|
ones++;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
threes++;
|
||||||
|
break;
|
||||||
default: throw new Exception("something went wrong");
|
default: throw new Exception("something went wrong");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return $"{ones * threes}";
|
return $"{ones * threes}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part2()
|
public override string Part2()
|
||||||
{
|
{
|
||||||
return "";
|
return $"{Connections(0)}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue