ben
/
aoc
1
0
Fork 0

2015 day 6
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ben Harris 2022-11-12 16:33:11 -05:00
parent d5ff74a20f
commit 23b74f07d5
3 changed files with 100 additions and 5 deletions

View File

@ -11,6 +11,7 @@ public class Test2015
[DataRow(typeof(Day03), "2081", "2341")]
// [DataRow(typeof(Day04), "346386", "9958218")]
[DataRow(typeof(Day05), "258", "53")]
// [DataRow(typeof(Day06), "543903", "14687245")]
public void TestAllDays(Type dayType, string part1, string part2)
{
Common.CheckDay(dayType, part1, part2);
@ -22,6 +23,7 @@ public class Test2015
[DataRow(typeof(Day03), "2", "11")]
// [DataRow(typeof(Day04), "609043", "6742839")]
[DataRow(typeof(Day05), "1", "1")]
[DataRow(typeof(Day06), "1000000", "1000000")]
public void CheckTestInputs(Type dayType, string part1, string part2)
{
Day.UseTestInput = true;

View File

@ -3,13 +3,105 @@
/// <summary>
/// Day 6: <see href="https://adventofcode.com/2015/day/6"/>
/// </summary>
public sealed class Day06 : Day
public sealed partial class Day06 : Day
{
public Day06() : base(2015, 6, "Puzzle Name")
[GeneratedRegex(@"(\d+),(\d+) through (\d+),(\d+)")]
private static partial Regex Coords();
private readonly Dictionary<(int x, int y), bool> _lightGrid = new();
private readonly Dictionary<(int x, int y), int> _brightnessGrid = new();
public Day06() : base(2015, 6, "Probably a Fire Hazard")
{
}
public override object Part1() => "";
public override object Part1()
{
foreach (var line in Input)
{
var d = Coords().Match(line).Groups.Values.Skip(1).Select(v => int.Parse(v.Value)).ToList();
int x1 = d[0], y1 = d[1], x2 = d[2], y2 = d[3];
int xStart = Math.Min(x1, x2), xEnd = Math.Max(x1, x2), yStart = Math.Min(y1, y2), yEnd = Math.Max(y1, y2);
public override object Part2() => "";
}
for (var x = xStart; x <= xEnd; x++)
{
for (var y = yStart; y <= yEnd; y++)
{
if (line.StartsWith("toggle"))
{
if (_lightGrid.ContainsKey((x, y)))
{
_lightGrid[(x, y)] = !_lightGrid[(x, y)];
}
else
{
_lightGrid[(x, y)] = true;
}
}
else if (line.StartsWith("turn on"))
{
_lightGrid[(x, y)] = true;
}
else if (line.StartsWith("turn off"))
{
_lightGrid[(x, y)] = false;
}
}
}
}
return _lightGrid.Count(v => v.Value);
}
public override object Part2()
{
foreach (var line in Input)
{
var d = Coords().Match(line).Groups.Values.Skip(1).Select(v => int.Parse(v.Value)).ToList();
int x1 = d[0], y1 = d[1], x2 = d[2], y2 = d[3];
int xStart = Math.Min(x1, x2), xEnd = Math.Max(x1, x2), yStart = Math.Min(y1, y2), yEnd = Math.Max(y1, y2);
for (var x = xStart; x <= xEnd; x++)
{
for (var y = yStart; y <= yEnd; y++)
{
if (line.StartsWith("toggle"))
{
if (_brightnessGrid.ContainsKey((x, y)))
{
_brightnessGrid[(x, y)] += 2;
}
else
{
_brightnessGrid[(x, y)] = 2;
}
}
else if (line.StartsWith("turn on"))
{
if (_brightnessGrid.ContainsKey((x, y)))
{
_brightnessGrid[(x, y)]++;
}
else
{
_brightnessGrid[(x, y)] = 1;
}
}
else if (line.StartsWith("turn off"))
{
if (_brightnessGrid.ContainsKey((x, y)) && _brightnessGrid[(x, y)] > 0)
{
_brightnessGrid[(x, y)]--;
}
else
{
_brightnessGrid[(x, y)] = 0;
}
}
}
}
}
return _brightnessGrid.Sum(v => v.Value);
}
}

View File

@ -0,0 +1 @@
turn on 0,0 through 999,999