-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day14.cs
82 lines (70 loc) · 2.43 KB
/
Day14.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2020.Solvers;
public class Day14 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
var mem1 = new Dictionary<long, long>();
var mem2 = new Dictionary<long, long>();
long maskXs = 0; // ['X'] -> 1, ['0' or '1'] -> 0
long mask1s = 0; // ['X' or '0'] -> 0, ['1'] -> 1
var reader = new SpanReader(input);
while (!reader.Done)
{
if (reader[1] == 'a') // mask
{
maskXs = 0;
mask1s = 0;
reader.SkipLength("mask = ".Length);
for (int i = 0; i < 36; i++)
{
byte c = reader[i];
switch (c)
{
case (byte)'1':
mask1s |= 1L << (35 - i);
break;
case (byte)'X':
maskXs |= 1L << (35 - i);
break;
}
}
reader.SkipLength(37); // 36 digits + newline
}
else // mem
{
reader.SkipLength("mem[".Length);
long addr = reader.ReadPosLongUntil(']');
reader.SkipLength(" = ".Length);
long val = reader.ReadPosLongUntil('\n');
// Part 1
mem1[addr] = (val & maskXs) | mask1s;
// Part 2
addr |= mask1s; // any 1's in the mask should be set to 1 in the address
addr &= ~maskXs; // any x's need to be set to 0 (since we will be iterating through permutations of bits in X)
// iterate through submasks: https://cp-algorithms.com/algebra/all-submasks.html
long mask = maskXs;
mem2[addr] = val; // handle mask = 0 case
while (mask != 0)
{
mem2[addr | mask] = val;
mask = (mask - 1) & maskXs;
}
}
}
long part1 = 0;
foreach ((_, long v) in mem1)
{
part1 += v;
}
long part2 = 0;
foreach ((_, long v) in mem2)
{
part2 += v;
}
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
}