-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
125 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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._ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1384 | ||
1008542 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.