-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
92 lines (75 loc) · 2.02 KB
/
Program.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
83
84
85
86
87
88
89
90
91
92
using AdventOfCode.Common;
int[] diskMap = Resources.GetInputFileLines().First()
.Select(c => c - '0')
.ToArray();
var blocks = new LinkedList<Block>();
bool empty = false;
for (int i = 0; i < diskMap.Length; i++)
{
blocks.AddLast(!empty
? new FileBlock((i + 1) / 2, i, diskMap[i])
: new FreeBlock(i, diskMap[i]));
empty = !empty;
}
LinkedListNode<Block> end = blocks.Last!;
while(end != blocks.First)
{
LinkedListNode<Block> start = blocks.First!;
while (start != end)
{
if (end.Value is not FileBlock fileToMove)
{
end = end.Previous!;
continue;
}
if (start.Value is not FreeBlock free || free.Size < fileToMove.Size)
{
start = start.Next!;
continue;
}
var newStart = start.Next;
blocks.AddBefore(start, new FileBlock(fileToMove.FileId, fileToMove.BlockId, fileToMove.Size));
free.Size -= fileToMove.Size;
if (free.Size == 0)
{
blocks.Remove(start);
}
var newEmptySpace = blocks.AddBefore(end, new FreeBlock(0, fileToMove.Size));
start = blocks.First!;
end = end.Previous!;
blocks.Remove(newEmptySpace.Next!);
//Print();
}
end = end.Previous!;
}
ulong checksum = 0UL;
int id = 0;
foreach (var block in blocks)
{
if (block is not FileBlock f)
{
id += block.Size;
continue;
}
for (int j = 0; j < block.Size; j++)
{
checksum += (ulong)(id * f.FileId);
id++;
}
}
Console.WriteLine(checksum);
void Print()
{
foreach (var block in blocks)
{
Console.Write(new string(block is FileBlock f ? (char)('0' + f.FileId) : '.', block.Size));
}
Console.WriteLine();
}
Console.WriteLine();
abstract file record Block(int BlockId, int Size)
{
public int Size { get; set; } = Size;
}
file record FileBlock(int FileId, int BlockId, int Size) : Block(BlockId, Size);
file record FreeBlock(int BlockId, int Size) : Block(BlockId, Size);