2022 day 7 part 1 works for test input??
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
334f4aa9d9
commit
01a62decce
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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() => "";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue