-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day07Part2.cs
29 lines (23 loc) · 1.11 KB
/
Day07Part2.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
using Microsoft.Extensions.Logging;
namespace AdventOfCode.Day07;
[Solution("Day07", "Part2")]
public class Day07Part2 : Day07
{
private readonly ILogger<Day07Part2> _logger;
public Day07Part2(ILogger<Day07Part2> logger) => _logger = logger;
protected override void RunDay7(Directory rootDirectory)
{
const ulong MaxSpace = 70000000ul;
const ulong MinSpace = 30000000ul;
var usedSpace = rootDirectory.GetTotalSize();
var freeSpace = MaxSpace - usedSpace;
var targetSpace = MinSpace - freeSpace;
_logger.LogDebug("Filesystem is using {used} / {total} bytes, leaving {free} bytes free.", usedSpace, MaxSpace, freeSpace);
_logger.LogDebug("We need to free at least {target} bytes to reach the minimum of {min} bytes free.", targetSpace, MinSpace);
var sizeToRemove = rootDirectory.AllDirectories
.Select(d => d.GetTotalSize())
.Where(size => size >= targetSpace)
.Min();
_logger.LogInformation("We need to delete a directory with [{size}] bytes to free up enough space.", sizeToRemove);
}
}