-
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 365f4f6
Showing
4 changed files
with
141 additions
and
2 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,70 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using AdventOfCode.Utilities; | ||
|
||
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) | ||
{ | ||
var results = new long[1 << 20]; | ||
var seen = new int[1 << 20]; | ||
|
||
int[] prices = new int[2001]; | ||
int[] diffs = new int[2001]; | ||
|
||
foreach ((int id, long seed) in input.Select(long.Parse).Enumerate(start: 1)) | ||
{ | ||
prices[0] = (int)seed % 10; | ||
|
||
foreach ((int i, long n) in NumberSequence(seed).Enumerate(start: 1)) | ||
{ | ||
prices[i] = (int)n % 10; | ||
diffs[i] = prices[i] - prices[i - 1]; | ||
} | ||
|
||
for (int i = 4; i < diffs.Length; i++) | ||
{ | ||
// store the 4 elements of the sequence as a 20bit number, 5 bits per value | ||
int sequence = ((diffs[i - 3] + 9) << 15) | ||
| ((diffs[i - 2] + 9) << 10) | ||
| ((diffs[i - 1] + 9) << 5) | ||
| (diffs[i] + 9); | ||
|
||
if (seen[sequence] != id) // make sure we only take the first instance of the sequence | ||
{ | ||
results[sequence] += prices[i]; | ||
seen[sequence] = id; | ||
} | ||
} | ||
} | ||
|
||
return results.Max(); | ||
} | ||
|
||
private static IEnumerable<long> NumberSequence(long 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; | ||
} | ||
} | ||
} | ||
} |
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
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); | ||
} | ||
} | ||
} |