Skip to content

Commit

Permalink
day(22): Monkey Market 🐒
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrodger committed Dec 22, 2024
1 parent fb907dd commit 365f4f6
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 2 deletions.
70 changes: 70 additions & 0 deletions src/AdventOfCode/Day22.cs
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;
}
}
}
}
4 changes: 2 additions & 2 deletions src/AdventOfCode/Utilities/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> @t
return value;
}

public static IEnumerable<(int i, T item)> Enumerate<T>(this IEnumerable<T> @this)
public static IEnumerable<(int i, T item)> Enumerate<T>(this IEnumerable<T> @this, int start = 0)
{
int i = 0;
int i = start;

foreach (T item in @this)
{
Expand Down
Binary file added src/AdventOfCode/inputs/day22.txt
Binary file not shown.
69 changes: 69 additions & 0 deletions tests/AdventOfCode.Tests/Day22Tests.cs
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);
}
}
}

0 comments on commit 365f4f6

Please sign in to comment.