-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
76 lines (59 loc) · 2.49 KB
/
Solution.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
namespace AdventOfCode2016.Day07
{
internal class Solution
{
private readonly IReadOnlyCollection<IpV7Address> _addresses;
public Solution(IEnumerable<string> input)
{
_addresses = input.Select(IpV7Address.Parse).ToList();
}
public int PartOne() => _addresses.Count(x => x.IsTlsSupported);
public int PartTwo() => _addresses.Count(x => x.IsSslSupported);
}
internal class IpV7Address
{
public IpV7Address(IReadOnlyCollection<string> hypernetSequences, IReadOnlyCollection<string> supernetSequences)
{
HypernetSequences = hypernetSequences;
SupernetSequences = supernetSequences;
}
public IReadOnlyCollection<string> HypernetSequences { get; }
public IReadOnlyCollection<string> SupernetSequences { get; }
public bool IsTlsSupported => SupernetSequences.Any(HasAbba) && !HypernetSequences.Any(HasAbba);
public bool IsSslSupported
{
get
{
var aba = SupernetSequences.SelectMany(GetAba);
var bab = HypernetSequences.SelectMany(GetAba);
return aba.Any(x => bab.Any(y => AreComplements(x, y)));
}
}
private static bool HasAbba(string sequence)
=> sequence.Windowed(4).Select(x => string.Concat(x)).Any(IsAba);
private static bool IsAba(string sequence)
=> sequence == sequence.Rev() && sequence.Distinct().Count() > 1;
private static bool AreComplements(string left, string right)
{
var leftChars = string.Concat(left.Distinct().OrderBy(x => x));
var rightChars = string.Concat(right.Distinct().OrderBy(x => x));
return left != right && leftChars == rightChars;
}
private static IEnumerable<string> GetAba(string sequence) =>
sequence
.Windowed(3)
.Select(x => string.Concat(x))
.Where(IsAba);
public static IpV7Address Parse(string address)
{
var sequences = address.Split('[', ']');
var supernet = sequences.Where((x, i) => i % 2 == 0).ToList();
var hypernet = sequences.Where((x, i) => i % 2 == 1).ToList();
return new IpV7Address(hypernet, supernet);
}
}
}