work on day 14 part 2
continuous-integration/drone/push Build is failing Details

not fully working...

correct answer calculated with my input on someone else's code
This commit is contained in:
Ben Harris 2020-12-14 16:43:47 -05:00
parent 9904a454a0
commit cd93985b43
2 changed files with 63 additions and 10 deletions

View File

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

View File

@ -9,17 +9,13 @@ namespace aoc2020
/// </summary>
public sealed class Day14 : Day
{
private readonly Dictionary<ulong, ulong> _writes;
private readonly Dictionary<ulong, ulong> _writes2;
public Day14() : base(14)
{
_writes = new Dictionary<ulong, ulong>();
_writes2 = new Dictionary<ulong, ulong>();
}
public override string Part1()
{
var writes = new Dictionary<ulong, ulong>();
ulong mask = 0, bits = 0;
foreach (var line in Input)
@ -30,7 +26,8 @@ namespace aoc2020
for (var i = 35; i >= 0; --i)
if (str[35 - i] == 'X')
mask |= (ulong) 1 << i;
else if (str[35 - i] == '1') bits |= (ulong) 1 << i;
else if (str[35 - i] == '1')
bits |= (ulong) 1 << i;
}
else
{
@ -40,15 +37,71 @@ namespace aoc2020
.Select(ulong.Parse)
.ToArray();
_writes[spl[0]] = (spl[1] & mask) | bits;
writes[spl[0]] = (spl[1] & mask) | bits;
}
return $"{_writes.Aggregate<KeyValuePair<ulong, ulong>, ulong>(0, (current, w) => current + w.Value)}";
return $"{writes.Aggregate<KeyValuePair<ulong, ulong>, ulong>(0, (current, w) => current + w.Value)}";
}
public override string Part2()
{
return "";
var memory = new Dictionary<long, long>();
var mask = "";
foreach (var line in Input)
{
var spl = line.Split(' ', 3, StringSplitOptions.TrimEntries);
if (spl[0] == "mask")
{
mask = spl[2];
}
else
{
var value = long.Parse(spl[2]);
var addr = long.Parse(spl[0].Split(new[] {'[', ']'},
StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)[1]);
var floats = new List<int>();
for (var i = 0; i < mask.Length; i++)
switch (mask[i])
{
case 'X':
floats.Add(i);
break;
case '1':
addr |= (long) 1 << (35 - i);
break;
}
if (floats.Any())
{
var combos = new List<int>();
combos.AddRange(floats);
foreach (var i in floats)
{
var newCombos = new List<int>();
foreach (var c in combos)
{
newCombos.Add(c | (1 << (35 - i)));
newCombos.Add(c & ~(1 << (35 - i)));
}
combos = newCombos;
}
foreach (var c in combos)
memory[c] = value;
}
else
{
memory[addr] = value;
}
}
}
return $"{memory.Sum(w => w.Value)}";
}
}
}