Skip to content

Commit

Permalink
2024/22 wip
Browse files Browse the repository at this point in the history
  • Loading branch information
encse committed Dec 22, 2024
1 parent 5244f8d commit a6ef6c2
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 43 deletions.
6 changes: 6 additions & 0 deletions 2024/Day22/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## --- Day 22: Monkey Market ---
As you're all teleported deep into the jungle, a [monkey](/2022/day/11) steals The Historians' device! You'll need get it back while The Historians are looking for the Chief.

The monkey that stole the device seems willing to trade it, but only in exchange for an absurd number of bananas. Your only option is to buy bananas on the Monkey Exchange Market.

_Visit the website for the full story and [full puzzle](https://adventofcode.com/2024/day/22) description._
82 changes: 82 additions & 0 deletions 2024/Day22/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
namespace AdventOfCode.Y2024.Day22;

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.Numerics;

[ProblemName("Monkey Market")]
class Solution : Solver {

public object PartOne(string input) {
return GetNums(input).Select(x => (long)Step(x).Last()).Sum();
}

public object PartTwo(string input) {
var mainDict = new Dictionary<(int a, int b, int c, int d), int>();
foreach(var num in GetNums(input)) {
var banana = GetBanana(num);
foreach(var k in banana.Keys){
mainDict[k] = mainDict.GetValueOrDefault(k) + banana[k];
}
}
return mainDict.Values.Max();
}

Dictionary<(int a, int b, int c, int d), int> GetBanana(int num) {
var res = new Dictionary<(int a, int b, int c, int d), int>();

var prices = Step(num).Select(n => n % 10).ToArray();
for(var i = 5;i < prices.Length; i++) {
var slice = prices[(i-5) .. i];
var diff = Diff(slice);
var key = (diff[0], diff[1], diff[2], diff[3]);
if (!res.ContainsKey(key)) {
res[key] = slice.Last();
}
}
return res;
// var diff = Diff(steps).ToArray();

// var res = 0;
// for(var i = seq.Length-1; i<diff.Length;i++) {
// if (diff[(i-(seq.Length-1))..i].SequenceEqual(seq)) {
// res += steps[i];
// }
// }
return res;
}
IEnumerable<int[]> Quads(int[] nums) {
for(var i = 4;i<=nums.Length;i++) {
yield return nums[(i-4) .. i];
}

}


int[] Diff(IEnumerable<int> x) {
return x.Zip(x.Skip(1)).Select(p => p.Second - p.First).ToArray();
}
IEnumerable<int> Step(int a) {
yield return a;
for(var i = 0;i< 2000;i++) {
var b = (long)a;
b = Mix(b, b * 64);
b = Prune(b);
b = Mix(b, b/32);
b = Prune(b);
b = Mix(b, b*2048);
b = Prune(b);
a = (int)b;
yield return a;
}
}
long Mix(long a, long b) => a ^ b;
long Prune(long a) => a % 16777216;

IEnumerable<int> GetNums(string input) =>
input.Split("\n").Select(int.Parse);
}
Binary file added 2024/Day22/input.in
Binary file not shown.
2 changes: 2 additions & 0 deletions 2024/Day22/input.refout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
18525593556
2089
74 changes: 45 additions & 29 deletions 2024/SplashScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public void Show() {

var color = Console.ForegroundColor;
Write(0xcc00, false, " ▄█▄ ▄▄█ ▄ ▄ ▄▄▄ ▄▄ ▄█▄ ▄▄▄ ▄█ ▄▄ ▄▄▄ ▄▄█ ▄▄▄\n █▄█ █ █ █ █ █▄█ █ █ █ █ █ █▄ ");
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ $year = 2024\n ");
Write(0xcc00, false, "\n ");
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ 0x0000 | 2024\n ");
Write(0xcc00, false, " \n ");
Write(0x888888, false, " .-----. .------------------. \n ");
Write(0xcccccc, false, ".--'");
Write(0xe3b585, false, "~ ~ ~");
Expand Down Expand Up @@ -49,9 +49,9 @@ public void Show() {
Write(0xcccccc, false, "| 3 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "|");
Write(0x1461f, false, "@");
Write(0x488813, false, "@");
Write(0x5eabb4, false, "..");
Write(0x427322, false, "@");
Write(0x4d8b03, false, "@");
Write(0xe3b585, false, "'. ~ ");
Write(0xcc00, false, "\" ' ");
Write(0xe3b585, false, "~ ");
Expand All @@ -70,7 +70,8 @@ public void Show() {
Write(0xcccccc, false, "|");
Write(0x4d8b03, false, "_");
Write(0x5eabb4, false, ".~.");
Write(0x4d8b03, false, "_@");
Write(0x4d8b03, false, "_");
Write(0x427322, false, "@");
Write(0xe3b585, false, "'.. ~ ~ ");
Write(0xffff66, true, "*");
Write(0xcccccc, false, "| | ");
Expand All @@ -83,10 +84,9 @@ public void Show() {
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "| ");
Write(0xffffff, false, "||| ");
Write(0x488813, false, "@");
Write(0x1461f, false, "#");
Write(0x427322, false, "@");
Write(0x4d8b03, false, "@");
Write(0x7fbd39, false, "@");
Write(0x488813, false, "@ ");
Write(0xe3b585, false, "'''...");
Write(0xcccccc, false, "| |");
Write(0xa25151, false, "... ");
Expand All @@ -97,12 +97,14 @@ public void Show() {
Write(0xcccccc, false, "| 6 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "|");
Write(0x427322, false, "@");
Write(0x488813, false, "#");
Write(0xffffff, false, "~~~");
Write(0x427322, false, "#");
Write(0x4d8b03, false, "@@ ");
Write(0x7fbd39, false, "#");
Write(0x427322, false, "# ");
Write(0x488813, false, "#");
Write(0x427322, false, "@");
Write(0x7fbd39, false, "@");
Write(0x4d8b03, false, "@#");
Write(0x1461f, false, "@");
Write(0x427322, false, "@ ");
Write(0xcccccc, false, "| |");
Write(0xa5a8af, false, "/\\ ");
Write(0xa25151, false, "''. ");
Expand Down Expand Up @@ -158,8 +160,7 @@ public void Show() {
Write(0xa5a8af, false, "/\\ ");
Write(0xa25151, false, "..' ");
Write(0xcccccc, false, "| | ");
Write(0xffffff, false, ". ");
Write(0xb5ed, false, ". ");
Write(0xffffff, false, ". ");
Write(0xcccccc, false, "| 11 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "| ");
Expand All @@ -169,15 +170,16 @@ public void Show() {
Write(0x333333, false, "::");
Write(0xffff66, true, ":");
Write(0x333333, false, "::");
Write(0xcccccc, false, "| | ");
Write(0xffffff, false, ". ");
Write(0xa2db, false, ". .");
Write(0xcccccc, false, "| |");
Write(0xa2db, false, ". ");
Write(0xffffff, false, ". ");
Write(0xa2db, false, ".");
Write(0xcccccc, false, "| 12 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "|");
Write(0xffffff, false, "'. - -");
Write(0xcccccc, false, "| |");
Write(0x333333, false, ". ::");
Write(0xcccccc, false, "| | ");
Write(0x333333, false, "::");
Write(0x9900, true, ":");
Write(0x333333, false, "::");
Write(0xcccccc, false, "| | ");
Expand All @@ -204,8 +206,7 @@ public void Show() {
Write(0xcccccc, false, "|");
Write(0xcc00, false, ". ''. ");
Write(0xcccccc, false, "| |");
Write(0x666666, false, ".");
Write(0x333333, false, ". ");
Write(0x666666, false, ". ");
Write(0x9900, true, ":::::");
Write(0xcccccc, false, "| |");
Write(0x666666, false, "──┬┴┴┴┬─");
Expand All @@ -230,9 +231,8 @@ public void Show() {
Write(0xcc00, false, "'.");
Write(0x5555bb, false, "~ ");
Write(0xcc00, false, ":");
Write(0xcccccc, false, "| | ");
Write(0x333333, false, ".");
Write(0x666666, false, "'. ");
Write(0xcccccc, false, "| |");
Write(0x666666, false, " '. ");
Write(0x333333, false, ". ");
Write(0xcccccc, false, "| |");
Write(0x666666, false, "┬o┤ten├─");
Expand All @@ -242,7 +242,8 @@ public void Show() {
Write(0xcc00, false, "'..' .'");
Write(0xcccccc, false, "| |");
Write(0x666666, false, " '");
Write(0x456efe, true, "o ");
Write(0x456efe, true, "o ");
Write(0x333333, false, ". ");
Write(0xcccccc, false, "| |");
Write(0x666666, false, "┘");
Write(0xffff66, true, "*");
Expand Down Expand Up @@ -290,10 +291,25 @@ public void Show() {
Write(0x9900, false, "<");
Write(0xcccccc, false, "| 21 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "| ");
Write(0xff0000, false, ".---_ ");
Write(0x66ff, false, "'------'_ ");
Write(0xaaaaaa, false, ".~' ");
Write(0xcccccc, false, "| | |");
Write(0xff0000, false, "\\|");
Write(0x9900ff, false, "\\ / \\ /");
Write(0x333399, false, "~ ");
Write(0x9900, false, ">");
Write(0xff0000, true, "@");
Write(0x9900, false, "<<");
Write(0xffff66, true, "*");
Write(0x9900, false, "<");
Write(0x66ff, true, "O");
Write(0xcccccc, false, "| 22 ");
Write(0xffff66, false, "**\n ");
Write(0x333333, false, "| | | | ");
Write(0x666666, false, "22\n 23\n ");
Write(0x666666, false, " 24\n 25\n ");
Write(0x666666, false, " \n");
Write(0x666666, false, "23\n 24\n ");
Write(0x666666, false, " 25\n \n");

Console.ForegroundColor = color;
Console.WriteLine();
Expand Down
Loading

0 comments on commit a6ef6c2

Please sign in to comment.