Files
2020/Q10.cs

80 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
namespace _2020
{
class Q10
{
static readonly List<long> list = new List<long>();
static List<long> sortedList = null;
public static void Go()
{
var start = DateTime.Now;
MakeList();
Util.Log($"Q10 MakeList took {(DateTime.Now - start).TotalMilliseconds}ms");
var p1start = DateTime.Now;
Part1();
Util.Log($"Q10 part1 took {(DateTime.Now - p1start).TotalMilliseconds}ms");
var p2start = DateTime.Now;
Part2();
Util.Log($"Q10 part2 took {(DateTime.Now - p2start).TotalMilliseconds}ms");
Util.Log($"Q10 took {(DateTime.Now - start).TotalMilliseconds}ms");
}
static void MakeList()
{
list.Add(0);
foreach (var line in File.ReadAllLines("10input.txt"))
{
list.Add(Convert.ToInt64(line));
}
sortedList = new List<long>(list);
sortedList.Sort();
sortedList.Add(sortedList[^1] + 3);
}
static void Part1()
{
var diffs = new int[3];
for (int i = 1; i < sortedList.Count; i++)
{
diffs[sortedList[i] - sortedList[i - 1] - 1]++;
}
Util.Log($"Q10Part1: device joltage={sortedList[^1]}, diffs by 1={diffs[0]}, diffs by 3={diffs[2]}, multiplied={diffs[0] * diffs[2]}");
}
static void Part2()
{
var pathsToIndices = new List<long>
{
1
};
for (int i = 1; i < sortedList.Count; i++)
{
long pathLen = 0;
for (int j = i - 1; j >= 0; j--)
{
if (sortedList[j] + 3 >= sortedList[i])
{
pathLen += pathsToIndices[j];
}
else
{
break;
}
}
pathsToIndices.Add(pathLen);
}
Util.Log($"Q10Part2: combinations={pathsToIndices[^1]}");
}
}
}