-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
117 additions
and
25 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,8 @@ | ||
## --- Day 6: Guard Gallivant --- | ||
The Historians use their fancy [device](4) again, this time to whisk you all away to the North Pole prototype suit manufacturing lab... in the year [1518](/2018/day/5)! It turns out that having direct access to history is very convenient for a group of historians. | ||
|
||
You still have to be careful of time paradoxes, and so it will be important to avoid anyone from 1518 while The Historians search for the Chief. Unfortunately, a single <em>guard</em> is patrolling this part of the lab. | ||
|
||
Maybe you can work out where the guard will go ahead of time so that The Historians can search safely? | ||
|
||
Read the [full puzzle](https://adventofcode.com/2024/day/6). |
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,70 @@ | ||
namespace AdventOfCode.Y2024.Day06; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Numerics; | ||
|
||
using Map = System.Collections.Generic.Dictionary<System.Numerics.Complex, char>; | ||
|
||
[ProblemName("Guard Gallivant")] | ||
class Solution : Solver { | ||
|
||
Complex Up = Complex.ImaginaryOne; | ||
Complex TurnRight = -Complex.ImaginaryOne; | ||
|
||
public object PartOne(string input) { | ||
var (map, start) = Parse(input); | ||
return Walk(map, start).positions.Count(); | ||
} | ||
|
||
public object PartTwo(string input) { | ||
var (map, start) = Parse(input); | ||
var positions = Walk(map, start).positions; | ||
var loops = 0; | ||
// simply try a blocker in each locations visited by the guard and count the loops | ||
foreach (var block in positions.Where(pos => map[pos] == '.')) { | ||
map[block] = '#'; | ||
if (Walk(map, start).isLoop) { | ||
loops++; | ||
} | ||
map[block] = '.'; | ||
} | ||
return loops; | ||
} | ||
|
||
// returns the positions visited when starting from 'pos', isLoop is set if the | ||
// guard enters a cycle. | ||
(IEnumerable<Complex> positions, bool isLoop) Walk(Map map, Complex pos) { | ||
var seen = new HashSet<(Complex pos, Complex dir)>(); | ||
var dir = Up; | ||
while (map.ContainsKey(pos) && !seen.Contains((pos, dir))) { | ||
seen.Add((pos, dir)); | ||
if (map.GetValueOrDefault(pos + dir) == '#') { | ||
dir *= TurnRight; | ||
} else { | ||
pos += dir; | ||
} | ||
} | ||
return ( | ||
positions: seen.Select(s => s.pos).Distinct(), | ||
isLoop: seen.Contains((pos, dir)) | ||
); | ||
} | ||
|
||
// store the grid in a dictionary, to make bounds checks and navigation simple | ||
// start represents the starting postion of the guard | ||
(Map map, Complex start) Parse(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>(-Up * y + x, lines[y][x]) | ||
).ToDictionary(); | ||
|
||
var start = map.First(x => x.Value == '^').Key; | ||
|
||
return (map, start); | ||
} | ||
} |
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 @@ | ||
4789 | ||
1304 |
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.