-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask10_2.cs
72 lines (58 loc) · 1.68 KB
/
Task10_2.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
using FluentAssertions;
using NUnit.Framework;
namespace AoC_2024;
[TestFixture]
public class Task10_2
{
[Test]
[TestCase(
@"89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732",
81)]
[TestCase(@"Task10.txt", 1252)]
public void Task(string input, int expected)
{
input = File.Exists(input) ? File.ReadAllText(input) : input;
map = input.SplitLines().Select(x => x.Select(c => int.Parse(c.ToString())).ToArray()).ToArray();
var nines = new List<Point>();
for (var i = 0; i < map.Length; i++)
for (var j = 0; j < map[i].Length; j++)
{
if (map[i][j] == 0) nines.Add(new Point(i, j));
}
var scores = new Dictionary<Point, List<Point>>();
foreach (var trailHead in nines)
{
Dfs(trailHead, trailHead, scores);
}
scores.Values.Sum(x=>x.Count).Should().Be(expected);
}
private void Dfs(Point trailHead, Point current, Dictionary<Point, List<Point>> scores)
{
var val = map[current.Row][current.Col];
if (val == 9)
{
if (!scores.ContainsKey(trailHead)) scores.Add(trailHead, new List<Point>());
scores[trailHead].Add(current);
return;
}
foreach (var neighbour in Extensions.GetVerticalHorizontalNeighbours(map, current))
{
if (neighbour.Item != val + 1) continue;
Dfs(trailHead, neighbour.Index, scores);
}
}
[ThreadStatic] private static int[][] map;
private class Block
{
public int Id { get; set; }
public int Cnt { get; set; }
public bool IsEmpty { get; set; }
}
}