Skip to content

Commit

Permalink
day 22 part 2 in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 22, 2024
1 parent 2804a9c commit 4d27595
Showing 1 changed file with 55 additions and 10 deletions.
65 changes: 55 additions & 10 deletions AdventOfCode/Day22.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

using System.Collections.Generic;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace AdventOfCode;

public class Day22 : BaseDay
Expand All @@ -14,26 +17,68 @@ public override ValueTask<string> Solve_1()

public override ValueTask<string> Solve_2()
{
return new("");
long[] numbers = File.ReadAllLines(InputFilePath).Select(long.Parse).ToArray();


Dictionary<(int, int, int, int), int> bananas = new Dictionary<(int, int, int, int), int>();
foreach (long n in numbers)
{
(int, int)[] changes = GetPricesWithChanges(n, 2000);

for (int i = 0; i < changes.Length - 3; ++i)
{
if (!bananas.TryAdd((changes[i].Item2, changes[i+1].Item2, changes[i+2].Item2, changes[i+3].Item2), changes[i+3].Item1))
{
bananas[(changes[i].Item2, changes[i + 1].Item2, changes[i + 2].Item2, changes[i + 3].Item2)] += changes[i + 3].Item1;
}
}
}

int max = bananas.Max(b => b.Value);

return new(max.ToString());
}

private long GetIteration(long number, int iterations)
{
for (int i = 0; i < iterations; ++i)
{
// 1
number = (number << 6) ^ number;
number = number % 16777216;
number = GetNext(number);
}

return number;
}

// 2
number = (number / 32) ^ number;
number = number % 16777216;
private (int, int)[] GetPricesWithChanges(long number, int iterations)
{
(int, int)[] priceChanges = new (int, int)[iterations];
int prevDigit = (int)(number % 10);

// 3
number = (number << 11) ^ number;
number = number % 16777216;
for (int i = 0; i < iterations; ++i)
{
number = GetNext(number);
int currDigit = (int)(number % 10);
priceChanges[i] = (currDigit, currDigit - prevDigit);
prevDigit = currDigit;
}

return priceChanges;
}

private long GetNext(long number)
{
// 1
number = (number << 6) ^ number;
number = number % 16777216;

// 2
number = (number / 32) ^ number;
number = number % 16777216;

// 3
number = (number << 11) ^ number;
number = number % 16777216;

return number;
}
}

0 comments on commit 4d27595

Please sign in to comment.