feat: solve part 1
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
namespace ParabolicReflectorDish.Tests;
|
||||
|
||||
public class DishTests
|
||||
{
|
||||
[Theory]
|
||||
[MemberData(nameof(TestData.ParseTestData), MemberType = typeof(TestData))]
|
||||
public void Parse_GivenInput_ItShouldReturnDishInstance(string[] input, Dish expected)
|
||||
{
|
||||
var result = Dish.Parse(input);
|
||||
result.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(TestData.CalculateTotalLoadTestData), MemberType = typeof(TestData))]
|
||||
public void CalculateTotalLoad_WhenCalled_ItShouldReturnExpectedLoad(Dish dish, long expected)
|
||||
{
|
||||
var result = dish.CalculateTotalLoad();
|
||||
result.Should().Be(expected);
|
||||
}
|
||||
|
||||
public static class TestData
|
||||
{
|
||||
private static readonly Dish TestDish = new(
|
||||
[
|
||||
['O', 'O', 'O', 'O', '.', '#', '.', 'O', '.', '.'],
|
||||
['O', 'O', '.', '.', '#', '.', '.', '.', '.', '#'],
|
||||
['O', 'O', '.', '.', 'O', '#', '#', '.', '.', 'O'],
|
||||
['O', '.', '.', '#', '.', 'O', 'O', '.', '.', '.'],
|
||||
['.', '.', '.', '.', '.', '.', '.', '.', '#', '.'],
|
||||
['.', '.', '#', '.', '.', '.', '.', '#', '.', '#'],
|
||||
['.', '.', 'O', '.', '.', '#', '.', 'O', '.', 'O'],
|
||||
['.', '.', 'O', '.', '.', '.', '.', '.', '.', '.'],
|
||||
['#', '.', '.', '.', '.', '#', '#', '#', '.', '.'],
|
||||
['#', '.', '.', '.', '.', '#', '.', '.', '.', '.'],
|
||||
]
|
||||
);
|
||||
|
||||
public static IEnumerable<object[]> CalculateTotalLoadTestData =>
|
||||
new List<object[]>
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new Dish(
|
||||
[
|
||||
['O', 'O', 'O', 'O', '.', '#', '.', 'O', '.', '.'],
|
||||
['O', 'O', '.', '.', '#', '.', '.', '.', '.', '#'],
|
||||
['O', 'O', '.', '.', 'O', '#', '#', '.', '.', 'O'],
|
||||
['O', '.', '.', '#', '.', 'O', 'O', '.', '.', '.'],
|
||||
['.', '.', '.', '.', '.', '.', '.', '.', '#', '.'],
|
||||
['.', '.', '#', '.', '.', '.', '.', '#', '.', '#'],
|
||||
['.', '.', 'O', '.', '.', '#', '.', 'O', '.', 'O'],
|
||||
['.', '.', 'O', '.', '.', '.', '.', '.', '.', '.'],
|
||||
['#', '.', '.', '.', '.', '#', '#', '#', '.', '.'],
|
||||
['#', '.', '.', '.', '.', '#', '.', '.', '.', '.'],
|
||||
]
|
||||
),
|
||||
136
|
||||
}
|
||||
};
|
||||
|
||||
public static IEnumerable<object[]> ParseTestData =>
|
||||
new List<object[]>
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new string[]
|
||||
{
|
||||
"O....#....",
|
||||
"O.OO#....#",
|
||||
".....##...",
|
||||
"OO.#O....O",
|
||||
".O.....O#.",
|
||||
"O.#..O.#.#",
|
||||
"..O..#O..O",
|
||||
".......O..",
|
||||
"#....###..",
|
||||
"#OO..#....",
|
||||
},
|
||||
new Dish(
|
||||
[
|
||||
['O', '.', '.', '.', '.', '#', '.', '.', '.', '.'],
|
||||
['O', '.', 'O', 'O', '#', '.', '.', '.', '.', '#'],
|
||||
['.', '.', '.', '.', '.', '#', '#', '.', '.', '.'],
|
||||
['O', 'O', '.', '#', 'O', '.', '.', '.', '.', 'O'],
|
||||
['.', 'O', '.', '.', '.', '.', '.', 'O', '#', '.'],
|
||||
['O', '.', '#', '.', '.', 'O', '.', '#', '.', '#'],
|
||||
['.', '.', 'O', '.', '.', '#', 'O', '.', '.', 'O'],
|
||||
['.', '.', '.', '.', '.', '.', '.', 'O', '.', '.'],
|
||||
['#', '.', '.', '.', '.', '#', '#', '#', '.', '.'],
|
||||
['#', 'O', 'O', '.', '.', '#', '.', '.', '.', '.'],
|
||||
]
|
||||
),
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace ParabolicReflectorDish.Tests;
|
||||
|
||||
public class UnitTest1
|
||||
{
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,107 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace ParabolicReflectorDish;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static async Task<int> Main(string[] args)
|
||||
{
|
||||
if (args.Length is 0)
|
||||
{
|
||||
Console.WriteLine("Please provide a path to the input file.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (File.Exists(args[0]) is false)
|
||||
{
|
||||
Console.WriteLine("The provided file does not exist.");
|
||||
return -2;
|
||||
}
|
||||
|
||||
var input = await File.ReadAllLinesAsync(args[0]);
|
||||
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
var result = Dish
|
||||
.Parse(input)
|
||||
.CalculateTotalLoad();
|
||||
|
||||
stopwatch.Stop();
|
||||
|
||||
Console.WriteLine($"The total load is {result}. ({stopwatch.ElapsedMilliseconds}ms)");
|
||||
|
||||
return (int)result;
|
||||
}
|
||||
}
|
||||
|
||||
public class Dish(
|
||||
List<List<char>> rows
|
||||
)
|
||||
{
|
||||
private const char RoundRockSymbol = 'O';
|
||||
private const char SquareRockSymbol = '#';
|
||||
private const char EmptySpaceSymbol = '.';
|
||||
|
||||
public List<List<char>> Rows { get; init; } = rows;
|
||||
|
||||
private List<List<char>> TiltDishToNorth(List<List<char>> rows)
|
||||
{
|
||||
var modifiedRows = rows.ToList();
|
||||
var numOfColumns = modifiedRows[0].Count;
|
||||
var numOfRows = modifiedRows.Count;
|
||||
|
||||
for (var currentColumnIndex = 0; currentColumnIndex < numOfColumns; currentColumnIndex++)
|
||||
{
|
||||
for (var currentRowIndex = 0; currentRowIndex < numOfRows; currentRowIndex++)
|
||||
{
|
||||
var current = modifiedRows[currentRowIndex][currentColumnIndex];
|
||||
|
||||
if (current is EmptySpaceSymbol)
|
||||
{
|
||||
for (var i = currentRowIndex + 1; i < numOfRows; i++)
|
||||
{
|
||||
var next = modifiedRows[i][currentColumnIndex];
|
||||
|
||||
if (next is SquareRockSymbol)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (next is RoundRockSymbol)
|
||||
{
|
||||
modifiedRows[currentRowIndex][currentColumnIndex] = RoundRockSymbol;
|
||||
modifiedRows[i][currentColumnIndex] = EmptySpaceSymbol;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return modifiedRows;
|
||||
}
|
||||
|
||||
public long CalculateTotalLoad()
|
||||
{
|
||||
var rows = TiltDishToNorth(Rows);
|
||||
rows.Reverse();
|
||||
|
||||
return rows
|
||||
.Select((r, index) =>
|
||||
{
|
||||
var numOfRoundRocks = r.Where(c => c is RoundRockSymbol).Count();
|
||||
return (index + 1) * numOfRoundRocks;
|
||||
})
|
||||
.Sum();
|
||||
}
|
||||
|
||||
public static Dish Parse(string[] input)
|
||||
{
|
||||
var inputList = input
|
||||
.Select(s => s.ToList())
|
||||
.ToList();
|
||||
|
||||
return new Dish(inputList);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user