-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay09.cs
More file actions
141 lines (118 loc) Β· 3.46 KB
/
Day09.cs
File metadata and controls
141 lines (118 loc) Β· 3.46 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
namespace aoc.y2024;
[RunnableDay(year: 2024, day: 9)]
class Day09 : Day
{
public override string PartOne()
{
var map = ReadInput().AsEnumerable().Select(c => c - '0').ToList();
var disk = BuildDisk(map);
for (int i = 0, j = disk.Length - 1; i < disk.Length && i < j; i++)
{
if (disk[i] == null)
{
while (disk[j] == null) j--;
disk[i] = disk[j];
disk[j] = null;
j--;
}
}
return disk.Checksum().ToString();
}
public override string PartTwo()
{
var map = ReadInput().AsEnumerable().Select(c => c - '0').ToList();
var disk = BuildDisk(map);
for (int i = disk.Files.Count - 1; i >= 0; i--)
{
var lookupSize = disk.Files[i].Size;
var currentEmptySize = 0;
var currentEmptyIdx = -1;
for (int j = 0; j < disk.Files[i].DiskPosition; j++)
{
if (disk[j] == null)
{
if (currentEmptyIdx == -1) currentEmptyIdx = j;
currentEmptySize++;
}
else
{
currentEmptyIdx = -1;
currentEmptySize = 0;
}
if (currentEmptySize == lookupSize && disk.Files[i].DiskPosition > currentEmptyIdx)
{
disk.MoveFile(disk.Files[i], currentEmptyIdx);
break;
}
}
}
return disk.Checksum().ToString();
}
private static Disk BuildDisk(List<int> diskMap)
{
var files = new List<Disk.File>();
var diskData = new int?[diskMap.Sum()];
for (int i = 0, diskIdx = 0; i < diskMap.Count; i++)
{
if (i % 2 == 0)
{
files.Add(new Disk.File(i / 2, diskIdx, diskMap[i]));
}
for (int j = 0; j < diskMap[i]; j++, diskIdx++)
{
if (i % 2 == 0)
{
diskData[diskIdx] = i / 2;
}
else
{
diskData[diskIdx] = null;
}
}
}
return new Disk(diskData, files);
}
}
class Disk(int?[] Data, List<Disk.File> Files)
{
public int?[] Data { get; } = Data;
public List<File> Files { get; } = Files;
public int Length
{
get => Data.Length;
}
public int? this[int i]
{
get => Data[i];
set => Data[i] = value;
}
public long Checksum()
{
var total = 0L;
for (int i = 0; i < Data.Length; i++)
{
total += i * (Data[i] ?? 0);
}
return total;
}
public void MoveFile(File file, int idx)
{
if (Data[idx..(idx + file.Size)].All(x => x == null))
{
for (int i = idx; i < idx + file.Size; i++) Data[i] = file.ID;
for (int i = file.DiskPosition; i < file.DiskPosition + file.Size; i++) Data[i] = null;
file.MoveTo(idx);
}
else throw new Exception("Invalid move");
}
public class File(int ID, int DiskPosition, int Size)
{
public int ID { get; } = ID;
public int DiskPosition { get; private set; } = DiskPosition;
public int Size { get; } = Size;
public void MoveTo(int idx)
{
this.DiskPosition = idx;
}
}
};