-
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.
- Loading branch information
1 parent
fb907dd
commit 6aecdaf
Showing
3 changed files
with
156 additions
and
0 deletions.
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,87 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace AdventOfCode | ||
{ | ||
/// <summary> | ||
/// Solver for Day 22 | ||
/// </summary> | ||
public class Day22 | ||
{ | ||
public long Part1(string[] input) | ||
{ | ||
return input.Select(long.Parse) | ||
.Sum(seed => NumberSequence(seed).Last()); | ||
} | ||
|
||
public long Part2(string[] input) | ||
{ | ||
long[] seeds = input.Select(long.Parse).ToArray(); | ||
|
||
(int Price, int Diff)[][] prices = seeds.Select(s => PriceSequence(s).ToArray()).ToArray(); | ||
|
||
var results = new Dictionary<(int, int, int, int), long>(); | ||
int[] sequence = new int[4]; | ||
|
||
for (int i = 0; i < seeds.Length; i++) | ||
{ | ||
var seen = new HashSet<(int, int, int, int)>(); | ||
(int Price, int Diff)[] diffs = prices[i]; | ||
|
||
for (int j = 3; j < diffs.Length; j++) | ||
{ | ||
sequence[0] = diffs[j - 3].Diff; | ||
sequence[1] = diffs[j - 2].Diff; | ||
sequence[2] = diffs[j - 1].Diff; | ||
sequence[3] = diffs[j].Diff; | ||
|
||
var key = (sequence[0], sequence[1], sequence[2], sequence[3]); | ||
|
||
if (!seen.Contains(key)) // make sure we only take the first instance of the sequence | ||
{ | ||
results[key] = results.GetValueOrDefault(key) + diffs[j].Price; | ||
seen.Add(key); | ||
} | ||
} | ||
} | ||
|
||
//var foo = results[(-2, 1, -1, 3)]; | ||
|
||
return results.Values.Max(); | ||
} | ||
|
||
private static IEnumerable<long> NumberSequence(long seed) | ||
{ | ||
yield return seed; | ||
|
||
for (int i = 0; i < 2000; i++) | ||
{ | ||
seed ^= (seed * 64); | ||
seed %= 16777216; | ||
seed ^= (seed / 32); | ||
seed %= 16777216; | ||
seed ^= (seed * 2048); | ||
seed %= 16777216; | ||
|
||
yield return seed; | ||
} | ||
} | ||
|
||
private static IEnumerable<(int Price, int Diff)> PriceSequence(long seed) | ||
{ | ||
// why did I not just do this with zip...? | ||
|
||
using IEnumerator<long> cursor = NumberSequence(seed).GetEnumerator(); | ||
|
||
cursor.MoveNext(); | ||
int previous = (int)(cursor.Current % 10); | ||
|
||
while (cursor.MoveNext()) | ||
{ | ||
int price = (int)(cursor.Current % 10); | ||
yield return (price, price - previous); | ||
previous = price; | ||
} | ||
} | ||
} | ||
} |
Binary file not shown.
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,69 @@ | ||
using System.IO; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace AdventOfCode.Tests | ||
{ | ||
public class Day22Tests | ||
{ | ||
private readonly ITestOutputHelper output; | ||
private readonly Day22 solver; | ||
|
||
public Day22Tests(ITestOutputHelper output) | ||
{ | ||
this.output = output; | ||
this.solver = new Day22(); | ||
} | ||
|
||
private static string[] GetRealInput() | ||
{ | ||
string[] input = File.ReadAllLines("inputs/day22.txt"); | ||
return input; | ||
} | ||
|
||
private static string[] GetSampleInput() | ||
{ | ||
return new string[] | ||
{ | ||
"1", | ||
"10", | ||
"100", | ||
"2024", | ||
|
||
//"123" | ||
}; | ||
} | ||
|
||
[Fact] | ||
public void Part1_SampleInput_ProducesCorrectResponse() | ||
{ | ||
var expected = 37327623; | ||
|
||
var result = solver.Part1(GetSampleInput()); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void Part1_RealInput_ProducesCorrectResponse() | ||
{ | ||
var expected = 14392541715; | ||
|
||
var result = solver.Part1(GetRealInput()); | ||
output.WriteLine($"Day 22 - Part 1 - {result}"); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void Part2_RealInput_ProducesCorrectResponse() | ||
{ | ||
var expected = 1628; | ||
|
||
var result = solver.Part2(GetRealInput()); | ||
output.WriteLine($"Day 22 - Part 2 - {result}"); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
} | ||
} |