-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
64 lines (54 loc) · 1.61 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
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode2016.Day03
{
internal class Solution
{
private readonly int[,] _data;
public Solution(IEnumerable<string> input)
{
var lines = input.ToArray();
_data = new int[lines.Length, 3];
for (var i = 0; i < lines.Length; i++)
{
var xs = lines[i].Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();
for (var j = 0; j < 3; j++)
{
_data[i, j] = int.Parse(xs[j]);
}
}
}
public int PartOne()
{
var valid = 0;
for (var i = 0; i < _data.GetLength(0); i++)
{
if (IsValidTriangle(_data[i, 0], _data[i, 1], _data[i, 2]))
{
valid++;
}
}
return valid;
}
public int PartTwo()
{
var valid = 0;
for (var i = 0; i < _data.GetLength(0); i += 3)
{
for (var j = 0; j < 3; j++)
{
if (IsValidTriangle(_data[i, j], _data[i + 1, j], _data[i + 2, j]))
{
valid++;
}
}
}
return valid;
}
private static bool IsValidTriangle(int a, int b, int c) =>
a + b > c &&
a + c > b &&
b + c > a;
}
}