-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay06.cs
More file actions
150 lines (126 loc) Β· 4.15 KB
/
Day06.cs
File metadata and controls
150 lines (126 loc) Β· 4.15 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System.Diagnostics;
namespace aoc.y2024;
[RunnableDay(year: 2024, day: 6)]
class Day06 : Day
{
public override string PartOne()
{
var map = ReadInputLines().Select(line => line.ToCharArray()).ToArray();
var startingPosition = StartingPosition(map);
var patrolResult = Patrol(map, startingPosition, Direction.Up);
var result = patrolResult.Path.Keys.Select(p => p.Item1).ToHashSet().Count;
return result.ToString();
}
public override string PartTwo()
{
var map = ReadInputLines().Select(line => line.ToCharArray()).ToArray();
var startingPosition = StartingPosition(map);
var patrolResult = Patrol(map, startingPosition, Direction.Up);
var possibleObstacles = patrolResult.Path.Keys.Select(p => p.Item1).ToHashSet();
var loops = 0;
foreach (var possibleObstacle in possibleObstacles)
{
var newMap = map.Select(row => row.ToArray()).ToArray();
newMap[possibleObstacle.Item2][possibleObstacle.Item1] = '#';
var result = Patrol(newMap, startingPosition, Direction.Up);
if (result.IsLoop)
{
loops++;
}
}
return loops.ToString();
}
private static (int, int) StartingPosition(char[][] map)
{
var startPosition = (-1, -1);
for (int y = 0; y < map.Length && startPosition == (-1, -1); y++)
{
for (int x = 0; x < map[0].Length && startPosition == (-1, -1); x++)
{
if (map[y][x] == '^')
{
startPosition = (x, y);
break;
}
}
}
return startPosition;
}
private static PatrolResult Patrol(char[][] map, (int, int) startPosition, Direction startDirection)
{
var trackingPositions = new Dictionary<((int, int), Direction), bool>();
var facing = startDirection;
var currentPosition = startPosition;
var isLoop = false;
while (true)
{
trackingPositions[(currentPosition, facing)] = true;
var directionModifier = facing.Modifier();
var nextPosition = (
currentPosition.Item1 + directionModifier.Item1,
currentPosition.Item2 + directionModifier.Item2
);
if (
nextPosition.Item1 < 0 || nextPosition.Item1 >= map[0].Length
|| nextPosition.Item2 < 0 || nextPosition.Item2 >= map.Length
)
{
break;
}
if (map[nextPosition.Item2][nextPosition.Item1] == '#')
{
facing = facing.TurnRight();
continue;
}
if (trackingPositions.ContainsKey((nextPosition, facing)))
{
isLoop = true;
break;
}
currentPosition = nextPosition;
}
return new PatrolResult(trackingPositions, isLoop);
}
}
readonly record struct PatrolResult(Dictionary<((int, int), Direction), bool> Path, bool IsLoop);
// TODO: Move these to another file
enum Direction
{
Up,
Right,
Down,
Left
}
static class Extensions
{
public static (int, int) Modifier(this Direction direction)
{
return direction switch
{
Direction.Up => (0, -1),
Direction.Right => (1, 0),
Direction.Down => (0, 1),
Direction.Left => (-1, 0),
_ => throw new UnreachableException(),
};
}
public static Direction TurnRight(this Direction direction)
{
return direction switch
{
Direction.Up => Direction.Right,
Direction.Right => Direction.Down,
Direction.Down => Direction.Left,
Direction.Left => Direction.Up,
_ => throw new UnreachableException(),
};
}
public static (int, int) Move(this Direction direction, (int, int) position)
{
var modifier = direction.Modifier();
return (
position.Item1 + modifier.Item1,
position.Item2 + modifier.Item2
);
}
}