Repository navigation
Expand file tree
/
Copy pathDay10.cs
More file actions
75 lines (61 loc) · 1.74 KB
/
Copy pathDay10.cs
File metadata and controls
75 lines (61 loc) · 1.74 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
public static class Day10
{
public static int GetNumber(string path)
{
var items = File.ReadAllLines(path).Select(x => int.Parse(x)).OrderBy(x => x).ToList();
items.Insert(0, Math.Max(0, items.First() - 3));
items.Add(items.Last() + 3);
Console.WriteLine(string.Join(Environment.NewLine, items));
var a = 0;
var b = 0;
for (int i = 0; i < items.Count - 1; i++)
{
var diff = items[i + 1] - items[i];
if (diff == 1)
{
a++;
}
else if (diff == 3)
{
b++;
}
}
Console.WriteLine($"a: {a}, b: {b}");
return a * b;
}
public static long GetArrangements(string path)
{
var items = File.ReadAllLines(path).Select(x => int.Parse(x)).OrderBy(x => x).ToList();
items.Insert(0, Math.Max(0, items.First() - 3));
items.Add(items.Last() + 3);
return GetArrangements(items, 0);
}
private static long GetArrangements(List<int> items, int i)
{
long res = 0;
for (; i < items.Count - 2; i++)
{
var item = items[i];
items.RemoveAt(i);
if (items[i + 1] - items[i] <= 3)
{
res++;
res += GetArrangements(items, i + 1);
}
items.Insert(i, item);
}
return res;
}
private static bool IsValid(List<int> items, Range range)
{
for (int i = range.Start.Value; i < range.End.Value; i++)
{
var diff = items[i + 1] - items[i];
if (diff > 3)
{
return false;
}
}
return true;
}
}