Skip to content

Commit

Permalink
day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszzborek committed Dec 11, 2023
1 parent c2d20cd commit 929ff66
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 4 deletions.
79 changes: 79 additions & 0 deletions AdventOfCode.Tests/DayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,4 +604,83 @@ public async Task Day10_02_Test4()
var result = await day.Solve_2();
result.ShouldBe("10");
}

[Fact]
public async Task Day11_01_Test1()
{
var strList = string.Join("\r\n", new List<string>
{
"...#......",
".......#..",
"#.........",
"..........",
"......#...",
".#........",
".........#",
"..........",
".......#..",
"#...#....."
});

var day = new Day11();
day.IsTest = true;
day.TestInput = strList;
var result = await day.Solve_1();
result.ShouldBe("374");
}

[Theory]
[InlineData(1, 6, 5,11,9)]
[InlineData(4, 0, 9,10,15)]
[InlineData(0, 2, 12,7,17)]
[InlineData(0, 11, 5,11,5)]
[InlineData(2, 15, 26,14,25)]
public void Day11_01_Test2(int x1, int y1, int x2, int y2, int expected)
{
var day = new Day11();
day.Steps(new Day11.Point(x1, y1), new Day11.Point(x2, y2)).ShouldBe(expected);
}

[Fact]
public async Task Day11_01_RealData()
{
var day = new Day11();
var result = await day.Solve_1();
result.ShouldBe("9521550");
}

[Theory]
[InlineData(10, 1030)]
[InlineData(100, 8410)]
public async Task Day11_02_Test1(int scale, int expected)
{
var strList = string.Join("\r\n", new List<string>
{
"...#......",
".......#..",
"#.........",
"..........",
"......#...",
".#........",
".........#",
"..........",
".......#..",
"#...#....."
});

var day = new Day11();
day.IsTest = true;
day.TestInput = strList;
day.Scale = scale;
var result = await day.Solve_1();
result.ShouldBe(expected.ToString());
}

[Fact]
public async Task Day11_02_RealData()
{
var day = new Day11();
var result = await day.Solve_2();
result.ShouldBe("298932923702");
}
}
3 changes: 3 additions & 0 deletions AdventOfCode/AdventOfCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<None Update="Inputs\10.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Inputs\11.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
18 changes: 16 additions & 2 deletions AdventOfCode/Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,24 @@ public Benchmarks()
// return await _day.Solve_1_OnlyLogic();
// }

// [Benchmark]
// public async Task<string> Day10_Part1_WithLoading()
// {
// var day = new Day10();
// return await day.Solve_1();
// }

[Benchmark]
public async Task<string> Day10_Part1_WithLoading()
public async Task<string> Day11_Part1_WithLoading()
{
var day = new Day10();
var day = new Day11();
return await day.Solve_1();
}

[Benchmark]
public async Task<string> Day11_Part2_WithLoading()
{
var day = new Day11();
return await day.Solve_2();
}
}
166 changes: 166 additions & 0 deletions AdventOfCode/Day11.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Channels;
using System.Threading.Tasks;
using AdventOfCode.Core;

namespace AdventOfCode;

public class Day11 : BetterBaseDay
{
public long Scale { get; set; } = 2;
public override ValueTask<string> Solve_1()
{
Scale -= 1;
var result = Calculate();
return new ValueTask<string>((result).ToString());
}

public override ValueTask<string> Solve_2()
{
Scale = 1000000;
Scale -= 1;
var result = Calculate();
return new ValueTask<string>((result).ToString());
}

private long Calculate()
{
var lines = InputData.Split("\r\n");
var galaxies = new List<Point>();
long maxX = 0;
long maxY = 0;

for (var i = 0; i < lines.Length; i++)
{
for (var j = 0; j < lines[i].Length; j++)
{
if (lines[i][j] != '#')
{
continue;
}

galaxies.Add(new Point(j, i));

maxX = Math.Max(maxX, j);
maxY = Math.Max(maxY, i);
}
}

//Print(galaxies);

ExpandGalaxy(galaxies, maxX, maxY);

//Print(galaxies);
//var pairs = new HashSet<(Point, Point)>();
long result = 0;
foreach (var g1 in galaxies)
{
//foreach (var g2 in galaxies.Where(g2 => !pairs.Contains((g1, g2)) && !pairs.Contains((g2, g1))).Where(g2 => !g1.Equals(g2)))
foreach (var g2 in galaxies)
{
//pairs.Add((g1, g2));
result += Steps(g1, g2);
}
}

return result/2;
}
private void ExpandGalaxy(List<Point> galaxies, long maxX, long maxY)
{

for (long i = 0; i < maxY; i++)
{
if (galaxies.Exists(x => x.Y == i))
{
continue;
}

for (var j = 0; j < galaxies.Count; j++)
{
if(galaxies[j].Y > i)
galaxies[j].Y += Scale;
}
// foreach (var g in galaxies.Where(x => x.Y > i))
// {
// g.Y += Scale;
// }
i += Scale;
maxY += Scale;
}

for (long i = 0; i < maxX; i++)
{
if (galaxies.Exists(x => x.X == i))
{
continue;
}

for (var j = 0; j < galaxies.Count; j++)
{
if(galaxies[j].X > i)
galaxies[j].X += Scale;
}

// foreach (var g in galaxies.Where(x => x.X > i))
// {
// g.X += Scale;
// }
i += Scale;
maxX += Scale;
}
}

public long Steps(Point a, Point b)
{
return Math.Abs(b.X - a.X) + Math.Abs(b.Y - a.Y);
}

private void Print(List<Point> galaxies)
{
var maxX = galaxies.Max(g => g.X);
var maxY = galaxies.Max(g => g.Y);

Console.WriteLine("");

for (var i = 0; i <= maxY; i++)
{
for (var j = 0; j <= maxX; j++)
{
if (galaxies.Contains(new Point(j, i)))
{
Console.Write('#');
}
else
{
Console.Write('.');
}
}
Console.WriteLine();
}

Console.WriteLine("");
Console.WriteLine("==================================");
}

public class Point

Check warning on line 147 in AdventOfCode/Day11.cs

View workflow job for this annotation

GitHub Actions / build-and-run

'Day11.Point' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 147 in AdventOfCode/Day11.cs

View workflow job for this annotation

GitHub Actions / build-and-run

'Day11.Point' overrides Object.Equals(object o) but does not override Object.GetHashCode()
{
public long X { get; set; }
public long Y { get; set; }

public Point(long x, long y)
{
X = x;
Y = y;
}

public override bool Equals(object obj)
{
if (obj is Point point)
return X == point.X && Y == point.Y;

return false;
}
}
}
Loading

0 comments on commit 929ff66

Please sign in to comment.