-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from tiberiushunter/day9-2023
Day 9 2023 Solution
- Loading branch information
Showing
3 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using AdventOfCode.Domain.Interfaces; | ||
using AdventOfCode.Solutions.Helpers; | ||
|
||
namespace AdventOfCode.Solutions._2023; | ||
|
||
public class Day9 : IDay | ||
{ | ||
public string Title => "Mirage Maintenance"; | ||
|
||
public string PartA(string input) | ||
{ | ||
var instructions = InputHelper.ToStringArray(input); | ||
|
||
long sumOfExtrapolatedValues = 0L; | ||
|
||
foreach (var line in instructions) | ||
{ | ||
var parsedLine = line.Split(' ').Select(int.Parse).ToList(); | ||
|
||
var dataset = GenerateDataset(parsedLine); | ||
|
||
for(int i = 1; i < dataset.Count; i++) | ||
{ | ||
dataset | ||
.ElementAt(i) | ||
.Add(dataset.ElementAt(i).Last() + dataset.ElementAt(i - 1).Last()); | ||
} | ||
|
||
sumOfExtrapolatedValues += dataset.Last().Last(); | ||
} | ||
|
||
return sumOfExtrapolatedValues.ToString(); | ||
} | ||
|
||
public string PartB(string input) | ||
{ | ||
var instructions = InputHelper.ToStringArray(input); | ||
|
||
long sumOfExtrapolatedValues = 0L; | ||
|
||
foreach (var line in instructions) | ||
{ | ||
var parsedLine = line.Split(' ').Select(int.Parse).ToList(); | ||
|
||
var dataset = GenerateDataset(parsedLine); | ||
|
||
for (int i = 1; i < dataset.Count; i++) | ||
{ | ||
dataset | ||
.ElementAt(i) | ||
.Insert(0, dataset.ElementAt(i).First() - dataset.ElementAt(i - 1).First()); | ||
} | ||
|
||
sumOfExtrapolatedValues += dataset.Last().First(); | ||
} | ||
|
||
return sumOfExtrapolatedValues.ToString(); | ||
} | ||
|
||
private static List<List<int>> GenerateDataset(List<int> firstLine) | ||
{ | ||
var dataset = new List<List<int>> | ||
{ | ||
firstLine | ||
}; | ||
|
||
while (dataset.Last().Any(x => x != 0)) | ||
{ | ||
dataset.Add(GenerateNextLine(dataset.Last())); | ||
} | ||
|
||
dataset.Reverse(); | ||
dataset.First().Add(0); | ||
|
||
return dataset; | ||
} | ||
|
||
private static List<int> GenerateNextLine(List<int> line) | ||
{ | ||
var nextLine = new List<int>(); | ||
|
||
for (int i = 0; i < line.Count - 1; i++) | ||
{ | ||
nextLine.Add(line[i + 1] - line[i]); | ||
} | ||
|
||
return nextLine; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace AdventOfCode.Solutions.Tests._2023; | ||
|
||
public class Day9Tests : DayTests | ||
{ | ||
[Test] | ||
public async Task PartA() | ||
{ | ||
// Arrange | ||
string expected = "1974913025"; | ||
|
||
// Act | ||
var solution = await _solverService.SolveDay(2023, 9); | ||
|
||
// Assert | ||
solution.PartA.Solution.Should().Be(expected); | ||
} | ||
|
||
[Test] | ||
public async Task PartB() | ||
{ | ||
// Arrange | ||
string expected = "884"; | ||
|
||
// Act | ||
var solution = await _solverService.SolveDay(2023, 9); | ||
|
||
// Assert | ||
solution.PartB.Solution.Should().Be(expected); | ||
} | ||
} |