-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc-15.0.csx
88 lines (72 loc) · 2.66 KB
/
aoc-15.0.csx
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
83
84
85
86
87
88
using System.Linq;
var input = File.ReadAllLines(@".\input.15.0.txt");
var template = input.First();
System.IO.File.WriteAllText(@".\temp.15.0.txt", template);
var pairInsertions = input
.Where(i => i.Contains(" -> "))
.Select(i => i.Split(" -> "))
.ToDictionary(i => i[0], i => i[1]);
var charCounter = new Dictionary<char, int>();
int stepsToPerform = 40;
string readFileName, writeFileName;
var fs1 = new MemoryStream();
fs1.Write(Encoding.UTF8.GetBytes(template));
fs1.Seek(0, SeekOrigin.Begin);
var fs2 = new MemoryStream();
var readBuffer = new byte[2];
for (int step = 0; step < stepsToPerform; step++)
{
readFileName = @$".\temp.15.{step % 2}.txt";
writeFileName = @$".\temp.15.{(step + 1) % 2}.txt";
// using var fs1 = new FileStream(readFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 1024 * 1024 * 25, true);
// using var fs1 = File.Open(readFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// using var fs2 = new StreamWriter(File.Open(writeFileName, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite));
// using var fs2 = new StreamWriter(new FileStream(writeFileName, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite, 1024 * 1024 * 25, true));
WriteLine($"Step {step} Count: {fs1.Length}");
var maxReadLength = 2;
for (int templatePos = 0; templatePos < fs1.Length - 1; templatePos++)
{
fs1.Read(readBuffer, 0, (int)maxReadLength);
fs1.Seek(-1, SeekOrigin.Current);
string pair = new (new[]{(char)readBuffer[0], (char)readBuffer[1]});
var insertPair = Pair(pair, templatePos);
if (insertPair.Length == 0) break;
fs2.Write(Encoding.UTF8.GetBytes(insertPair), 0, insertPair.Length);
}
fs1.Seek(0, SeekOrigin.Begin);
fs2.WriteTo(fs1);
fs1.Seek(0, SeekOrigin.Begin);
fs2.Seek(0, SeekOrigin.Begin);
}
fs1.Seek(0, SeekOrigin.Begin);
while(fs1.Position < fs1.Length)
{
char @char = (char)fs1.ReadByte();
CountChars(@char);
}
char minChar = charCounter.Min(c => c.Key);
int min = charCounter.Min(c => c.Value);
char maxChar = charCounter.Max(c => c.Key);
int max = charCounter.Max(c => c.Value);
WriteLine($"{maxChar} - {minChar}");
WriteLine($"{max} - {min} = {max - min}");
char[] Pair(string pair, int templatePos)
{
if (pair[0] == 0 || pair[1] == 0) return new char[0];
string insert = pairInsertions[pair];
if (templatePos > 0)
return new char[]{ insert[0], pair[1] };
else
return new char[]{ pair[0], insert[0], pair[1] };
}
void CountChars(char @char)
{
if (charCounter.ContainsKey(@char))
{
charCounter[@char] += 1;
}
else
{
charCounter[@char] = 1;
}
}