-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryV2.cs
79 lines (71 loc) · 1.55 KB
/
MemoryV2.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode.Year2020.Day14;
public class MemoryV2 : IMemory
{
private Dictionary<long, long> memory = new Dictionary<long, long>();
private BitArray bitMask = new BitArray(36, false);
private BitArray floatingMask = new BitArray(36, false);
public void SetMask(string str)
{
bitMask.SetAll(false);
floatingMask.SetAll(false);
for (int i = 0; i < str.Length; i++)
{
switch (str[i])
{
case 'X':
floatingMask.Set(i, true);
break;
case '1':
bitMask.Set(i, true);
break;
case '0':
break;
default:
throw new ArgumentException();
}
}
}
public long GetMemorySum()
{
long sum = 0;
foreach (long value in memory.Values)
sum += value;
return sum;
}
public void Write(long address, long value)
{
foreach (long adr in MaskAdress(address).GetAddresses())
{
memory[adr] = value;
}
}
MemoryV2Address MaskAdress(long address)
{
address |= BitArrayToLong(bitMask);
bool?[] array = LongToBooleanArray(address);
for (int i = 0; i < floatingMask.Length; i++)
{
if (floatingMask[i]) array[i] = null;
}
return new MemoryV2Address(array);
}
private bool?[] LongToBooleanArray(long value)
{
return Convert.ToString(value, 2).PadLeft(36, '0').Select<char, bool?>(s => s.Equals('1')).ToArray();
}
private long BitArrayToLong(BitArray array)
{
long number = 0;
for (int i = 0; i < array.Length; i++)
{
number <<= 1;
if (array[i])
number++;
}
return number;
}
}