-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD06.cs
123 lines (103 loc) · 3.15 KB
/
D06.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.Linq;
namespace AOC2024;
/// <summary>
/// Day 6: Guard Gallivant
/// </summary>
public class D06
{
private readonly AOCHttpClient _client = new AOCHttpClient(6);
private readonly Dictionary<char, (int, int)> _directions = new Dictionary<char, (int, int)>
{
{ '^', (0, -1) },
{ '>', (1, 0) },
{ 'v', (0, 1) },
{ '<', (-1, 0) },
};
private readonly char[] _symbols = { '^', '>', 'v', '<' };
public void Part1()
{
string input = _client.RetrieveFile();
// input = @"....#.....
// .........#
// ..........
// ..#.......
// .......#..
// ..........
// .#..^.....
// ........#.
// #.........
// ......#...";
char[,] array = input.ConvertToCharArray();
(int X, int Y) startPosition = array.FindFirst('^');
(HashSet<(int, int)> Set, bool _) seen = GetSeenPositions(array, startPosition);
Console.WriteLine(seen.Set.Count);
}
private (HashSet<(int, int)> Set, bool IsCycle) GetSeenPositions(char[,] array, (int X, int Y) currentPosition)
{
HashSet<(int, int)> set = new HashSet<(int, int)>()
{
currentPosition
};
int currentSymbolIndex = Array.IndexOf(_symbols, array[currentPosition.X, currentPosition.Y]);
int steps = 0;
while (true)
{
char currentSymbol = _symbols[currentSymbolIndex];
(int X, int Y) dir = _directions[currentSymbol];
(int X, int Y) newPosition = (currentPosition.X + dir.X, currentPosition.Y + dir.Y);
if (!array.IsWithinBounds(newPosition))
{
return (set, false);
}
if (array[newPosition.X, newPosition.Y] == '#')
{
currentSymbolIndex = (currentSymbolIndex + 1) % 4;
continue;
}
currentPosition = newPosition;
if (set.Contains(currentPosition))
{
steps++;
}
set.Add(currentPosition);
// The Guard is tired, let the man rest :')
if (steps >= 1000)
{
return (set, true);
}
}
}
public void Part2()
{
string input = _client.RetrieveFile();
// input = @"....#.....
// .........#
// ..........
// ..#.......
// .......#..
// ..........
// .#..^.....
// ........#.
// #.........
// ......#...";
char[,] array = input.ConvertToCharArray();
(int X, int Y) startPosition = array.FindFirst('^');
(HashSet<(int, int)> Set, bool _) initial = GetSeenPositions(array, startPosition);
initial.Set.Remove(startPosition);
long sum = 0;
foreach ((int X, int Y) s in initial.Set)
{
char[,] copy = new char[array.GetLength(0), array.GetLength(1)];
Array.Copy(array, copy, array.Length);
copy[s.X, s.Y] = '#';
(HashSet<(int, int)> Set, bool IsCycle) seen = GetSeenPositions(copy, startPosition);
if (seen.IsCycle)
{
sum++;
}
}
Console.WriteLine(sum);
}
}