-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.linq
69 lines (58 loc) · 1.95 KB
/
24.linq
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
<Query Kind="Program" />
int _maxStrength = 0;
int _maxStrengthLongest = 0;
int _longest = 0;
void Main()
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(Util.CurrentQueryPath));
string[] steps = File.ReadAllLines("24.txt");
List<Component> components = new List<Component>();
foreach (var s in steps)
{
int[] parts = s.Split('/').Select(p => int.Parse(p)).ToArray();
components.Add(new Component(parts[0], parts[1]));
}
Build(0, new List<Component>(), components);
$"P1: {_maxStrength}".Dump();
$"P2: {_maxStrengthLongest}".Dump();
}
public void Build(int connector, List<Component> bridge, List<Component> availableComponents)
{
var suitableComponents = availableComponents.Where(c => c.A == connector || c.B == connector).ToArray();
if (suitableComponents.Length == 0)
{
int strength = bridge.Sum(sc => sc.A + sc.B);
if (strength > _maxStrength) _maxStrength = strength;
if (bridge.Count > _longest)
{
_longest = bridge.Count;
_maxStrengthLongest = strength;
}
else if (bridge.Count == _longest && strength > _maxStrengthLongest)
{
_maxStrengthLongest = strength;
}
}
else
{
foreach (var component in suitableComponents)
{
var nextBridge = new List<Component>(bridge);
nextBridge.Add(component);
var remainingComponents = availableComponents.Where(c => c != component).ToList();
int nextConnector = component.A;
if (component.A == connector) nextConnector = component.B;
Build(nextConnector, nextBridge, remainingComponents);
}
}
}
public class Component
{
public Component(int a, int b)
{
A = a;
B = b;
}
public int A { get; private set; }
public int B { get; private set; }
}