diff --git a/2024/Day22/README.md b/2024/Day22/README.md new file mode 100644 index 00000000..6d05ba3d --- /dev/null +++ b/2024/Day22/README.md @@ -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._ diff --git a/2024/Day22/Solution.cs b/2024/Day22/Solution.cs new file mode 100644 index 00000000..52934a0a --- /dev/null +++ b/2024/Day22/Solution.cs @@ -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 Quads(int[] nums) { + for(var i = 4;i<=nums.Length;i++) { + yield return nums[(i-4) .. i]; + } + + } + + + int[] Diff(IEnumerable x) { + return x.Zip(x.Skip(1)).Select(p => p.Second - p.First).ToArray(); + } + IEnumerable 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 GetNums(string input) => + input.Split("\n").Select(int.Parse); +} \ No newline at end of file diff --git a/2024/Day22/input.in b/2024/Day22/input.in new file mode 100644 index 00000000..c00da3da Binary files /dev/null and b/2024/Day22/input.in differ diff --git a/2024/Day22/input.refout b/2024/Day22/input.refout new file mode 100644 index 00000000..89564865 --- /dev/null +++ b/2024/Day22/input.refout @@ -0,0 +1,2 @@ +18525593556 +2089 \ No newline at end of file diff --git a/2024/SplashScreen.cs b/2024/SplashScreen.cs index fb6eda91..125d9916 100644 --- a/2024/SplashScreen.cs +++ b/2024/SplashScreen.cs @@ -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, "~ ~ ~"); @@ -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, "~ "); @@ -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, "| | "); @@ -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, "... "); @@ -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, "''. "); @@ -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, "| "); @@ -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, "| | "); @@ -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, "──┬┴┴┴┬─"); @@ -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├─"); @@ -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, "*"); @@ -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(); diff --git a/2024/calendar.svg b/2024/calendar.svg index c5fe6742..994bc444 100644 --- a/2024/calendar.svg +++ b/2024/calendar.svg @@ -1,4 +1,4 @@ - +