ben
/
aoc
1
0
Fork 0

2022 day 7 part 1 works for test input??
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ben Harris 2022-12-07 13:19:44 -05:00
parent 334f4aa9d9
commit 01a62decce
2 changed files with 42 additions and 8 deletions

View File

@ -0,0 +1,16 @@
namespace AOC.Common;
public class DefaultDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TValue : new() where TKey : notnull
{
public new TValue this[TKey key]
{
get
{
if (TryGetValue(key, out var val)) return val;
val = new();
Add(key, val);
return val;
}
set => base[key] = value;
}
}

View File

@ -5,23 +5,41 @@ namespace AOC2022;
/// </summary>
public sealed class Day07 : Day
{
private readonly DefaultDictionary<string, long> _dirs;
public Day07() : base(2022, 7, "No Space Left On Device")
{
}
public override object Part1()
{
var dirs = new Dictionary<string, long>();
_dirs = new();
var path = new Stack<string>();
foreach (var line in Input)
{
if (line.StartsWith('$'))
if (line.StartsWith("$ cd"))
{
// command
var split = line.Split("$ cd ");
if (split[1] == "..")
{
path.Pop();
}
else if (split[1] != "/")
{
path.Push(split[1]);
}
}
else
{
if (!long.TryParse(line.Split(' ')[0], out var filesize))
continue;
var pathList = path.ToList();
foreach (var i in Enumerable.Range(0, path.Count + 1))
_dirs[string.Join("/", pathList.GetRange(0, i))] += filesize;
}
}
}
public override object Part1() =>
_dirs.Values.Where(d => d <= 1_000_000L).Sum();
public override object Part2() => "";
}