Skip to content

Commit

Permalink
2023-09 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
tetsuo13 committed Jan 15, 2024
1 parent ea39f5b commit a6bfa39
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ AdventOfCode --year 2023 --day 1
| [Scratchcards](https://adventofcode.com/2023/day/4) | [Solution](./src/AdventOfCode/Calendar/2023/Day04/Solution.cs) | 2023-04 | Completed |
| [Wait For It](https://adventofcode.com/2023/day/6) | [Solution](./src/AdventOfCode/Calendar/2023/Day06/Solution.cs) | 2023-06 | Completed |
| [Haunted Wasteland](https://adventofcode.com/2023/day/8) | [Solution](./src/AdventOfCode/Calendar/2023/Day08/Solution.cs) | 2023-08 | Completed |
| [Mirage Maintenance](https://adventofcode.com/2023/day/9) | [Solution](./src/AdventOfCode/Calendar/2023/Day09/Solution.cs) | 2023-09 | Part 1 |
| [Mirage Maintenance](https://adventofcode.com/2023/day/9) | [Solution](./src/AdventOfCode/Calendar/2023/Day09/Solution.cs) | 2023-09 | Completed |
21 changes: 14 additions & 7 deletions src/AdventOfCode/Calendar/2023/Day09/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,34 @@ public override async Task<object> Run(RunMode runMode)

return runMode switch
{
RunMode.PartOne => SumExtrapolatedValues(histories),
RunMode.PartTwo => 0,
RunMode.PartOne => SumExtrapolatedValues(histories, false),
RunMode.PartTwo => SumExtrapolatedValues(histories, true),
_ => throw new ArgumentOutOfRangeException(nameof(runMode))
};
}

private static int SumExtrapolatedValues(IEnumerable<IEnumerable<int>> histories)
private static int SumExtrapolatedValues(IEnumerable<IEnumerable<int>> histories, bool backwards)
{
return histories
.Select(sequence => sequence.Last() + GetNextInSequence(sequence))
.Select(sequence =>
{
return (backwards ? sequence.First() : sequence.Last()) +
GetNextInSequence(sequence, backwards);
})
.Sum();
}

private static int GetNextInSequence(IEnumerable<int> sequence)
private static int GetNextInSequence(IEnumerable<int> sequence, bool backwards)
{
if (sequence.All(x => x == 0))
{
return 0;
}

var nextSequence = sequence.Skip(1).Zip(sequence, (curr, prev) => curr - prev);
return nextSequence.Last() + GetNextInSequence(nextSequence);
var nextSequence = sequence.Skip(1)
.Zip(sequence, (curr, prev) => backwards ? prev - curr : curr - prev);

return (backwards ? nextSequence.First() : nextSequence.Last()) +
GetNextInSequence(nextSequence, backwards);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class SolutionTests
{
[Theory]
[InlineData(RunMode.PartOne, 114)]
[InlineData(RunMode.PartTwo, 2)]
public async Task Example(RunMode runMode, int expected)
{
string[] input =
Expand Down

0 comments on commit a6bfa39

Please sign in to comment.