Skip to content

Commit

Permalink
2024/20
Browse files Browse the repository at this point in the history
  • Loading branch information
encse committed Dec 20, 2024
1 parent e429f36 commit 267f859
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 42 deletions.
6 changes: 6 additions & 0 deletions 2024/Day20/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## --- Day 20: Race Condition ---
The Historians are quite pixelated again. This time, a massive, black building looms over you - you're [right outside](/2017/day/24) the CPU!

While The Historians get to work, a nearby program sees that you're idle and challenges you to a <em>race</em>. Apparently, you've arrived just in time for the frequently-held <em>race condition</em> festival!

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

[ProblemName("Race Condition")]
class Solution : Solver {

public object PartOne(string input) => Solve(input, 2);
public object PartTwo(string input) => Solve(input, 20);

int Solve(string input, int cheat) {
var path = GetPath(input);

// this nested loop is 6x times faster then the same thing with LINQ ¯\_(ツ)_/¯
var res = 0;
for (var i = 0; i < path.Length; i++) {
for (var j = i + 1; j < path.Length; j++) {
var dist = Manhattan(path[i], path[j]);

// the index of an element in the path equals to its distance
// from the finish line

var saving = j - (i + dist);
if (dist <= cheat && saving >= 100) {
res++;
}
}
}
return res;
}

int Manhattan(Complex a, Complex b) =>
(int)(Math.Abs(a.Imaginary - b.Imaginary) + Math.Abs(a.Real - b.Real));

// follow the path from start to finish, supposed that there is a single track in the input
Complex[] GetPath(string input) {
var lines = input.Split("\n");
var map = (
from y in Enumerable.Range(0, lines.Length)
from x in Enumerable.Range(0, lines[0].Length)
select new KeyValuePair<Complex, char>(x + y * Complex.ImaginaryOne, lines[y][x])
).ToDictionary();

var start = map.Keys.Single(k => map[k] == 'S');
var goal = map.Keys.Single(k => map[k] == 'E');

var (prev, cur, dir) = ((Complex?)null, start, Complex.ImaginaryOne);

var res = new List<Complex> { start };
while (cur != goal) {
if (map[cur + dir] == '#' || cur + dir == prev) {
dir *= Complex.ImaginaryOne;
} else {
(prev, cur) = (cur, cur + dir);
res.Add(cur);
}
}
return res.ToArray();
}
}
Binary file added 2024/Day20/input.in
Binary file not shown.
2 changes: 2 additions & 0 deletions 2024/Day20/input.refout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1384
1008542
70 changes: 41 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 █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ /^2024$/\n \n ");
Write(0xcc00, false, " ");
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ /* 2024 */\n \n ");
Write(0xcc00, false, " ");
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(0x4d8b03, false, "@");
Write(0x7fbd39, false, "#");
Write(0x5eabb4, false, "..");
Write(0x488813, false, "@");
Write(0x7fbd39, false, "#");
Write(0xe3b585, false, "'. ~ ");
Write(0xcc00, false, "\" ' ");
Write(0xe3b585, false, "~ ");
Expand All @@ -68,10 +68,9 @@ public void Show() {
Write(0xcccccc, false, "| 4 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "|");
Write(0x7fbd39, false, "_");
Write(0x1461f, false, "_");
Write(0x5eabb4, false, ".~.");
Write(0x4d8b03, false, "_");
Write(0x488813, false, "#");
Write(0x488813, false, "_@");
Write(0xe3b585, false, "'.. ~ ~ ");
Write(0xffff66, true, "*");
Write(0xcccccc, false, "| | ");
Expand All @@ -84,10 +83,10 @@ public void Show() {
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "| ");
Write(0xffffff, false, "||| ");
Write(0x488813, false, "#");
Write(0x4d8b03, false, "@");
Write(0x427322, false, "#");
Write(0x4d8b03, false, "#");
Write(0x7fbd39, false, "@");
Write(0x488813, false, "@");
Write(0x427322, false, "@");
Write(0xe3b585, false, "'''...");
Write(0xcccccc, false, "| |");
Write(0xa25151, false, "... ");
Expand All @@ -98,11 +97,11 @@ public void Show() {
Write(0xcccccc, false, "| 6 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "|");
Write(0x7fbd39, false, "@");
Write(0x427322, false, "@");
Write(0xffffff, false, "~~~");
Write(0x488813, false, "@# ");
Write(0x4d8b03, false, "# ");
Write(0x488813, false, "# @ ");
Write(0x1461f, false, "@");
Write(0x488813, false, "#@#");
Write(0x4d8b03, false, "# ");
Write(0xcccccc, false, "| |");
Write(0xa5a8af, false, "/\\ ");
Write(0xa25151, false, "''. ");
Expand Down Expand Up @@ -169,18 +168,16 @@ public void Show() {
Write(0xffff66, true, ":");
Write(0x333333, false, "::");
Write(0xcccccc, false, "| | ");
Write(0xffffff, false, ". ");
Write(0xa2db, false, ". ");
Write(0xffffff, 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, "| |");
Write(0x91cc, false, "' ");
Write(0xcccccc, false, "| | ");
Write(0xffffff, false, ".' ");
Write(0xcccccc, false, "| 13 ");
Write(0xffff66, false, "**\n ");
Expand Down Expand Up @@ -215,7 +212,8 @@ public void Show() {
Write(0x5555bb, false, "~ ");
Write(0xcc00, false, "'.");
Write(0xcccccc, false, "| |");
Write(0x666666, false, " : ");
Write(0x333333, false, ".");
Write(0x666666, false, ": ");
Write(0x333333, false, "::");
Write(0x553322, true, ":");
Write(0x333333, false, "::");
Expand All @@ -230,15 +228,17 @@ public void Show() {
Write(0x5555bb, false, "~ ");
Write(0xcc00, false, ":");
Write(0xcccccc, false, "| |");
Write(0x666666, false, " '. ");
Write(0x666666, false, " '. ");
Write(0x333333, false, ". .");
Write(0xcccccc, false, "| |");
Write(0x666666, false, "┬o┤ten├─");
Write(0xcccccc, false, "| 17 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "| ");
Write(0xcc00, false, "'..' .'");
Write(0xcccccc, false, "| |");
Write(0x666666, false, " '");
Write(0xcccccc, false, "| | ");
Write(0x333333, false, ".");
Write(0x666666, false, "'");
Write(0x456efe, true, "o ");
Write(0xcccccc, false, "| |");
Write(0x666666, false, "┘");
Expand All @@ -254,12 +254,24 @@ public void Show() {
Write(0xcccccc, false, "| |");
Write(0x666666, false, "─┘├┬┬┬┴─");
Write(0xcccccc, false, "| 19 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "|");
Write(0xcc00, false, "'''");
Write(0xcccccc, false, ")) | | ");
Write(0xbee4e7, true, "o ");
Write(0xffff66, true, "*");
Write(0x666666, false, " : ");
Write(0xcccccc, false, "'. .'");
Write(0x666666, false, "──┘");
Write(0x9900, false, ">>");
Write(0xff9900, true, "o");
Write(0x9900, false, "<<");
Write(0xcccccc, false, "| 20 ");
Write(0xffff66, false, "**\n ");
Write(0x333333, false, " | | | '. .' | ");
Write(0x666666, false, "20\n 21\n ");
Write(0x666666, false, " 22\n 23\n ");
Write(0x666666, false, " 24\n ");
Write(0x666666, false, " 25\n \n");
Write(0x333333, false, ".------' '------. | '' | ");
Write(0x666666, false, "21\n 22\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 267f859

Please sign in to comment.