-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day4.cs
85 lines (80 loc) · 1.76 KB
/
Day4.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
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventofCode2022
{
internal static class DayFour
{
private class ElfTask
{
public int start;
public int end;
}
internal static long Part1(string input)
{
string[] lines = input.Split('\n');
int sum = 0;
foreach (string lin in lines)
{
string[] parts = lin.Split(',');
string[] p1 = parts[0].Split('-');
string[] p2 = parts[1].Split('-');
ElfTask a = new ElfTask()
{
start = int.Parse(p1[0]),
end = int.Parse(p1[1])
};
ElfTask b = new ElfTask()
{
start = int.Parse(p2[0]),
end = int.Parse(p2[1])
};
if(CanCombine(a, b))
{
sum++;
}
}
return sum;
}
private static bool CanCombine(ElfTask taskA, ElfTask taskB)
{
if (taskA.start <= taskB.start && taskA.end >= taskB.end) return true;
if (taskA.start >= taskB.start && taskA.end <= taskB.end) return true;
return false;
}
internal static long Part2(string input)
{
string[] lines = input.Split('\n');
int sum = 0;
foreach (string lin in lines)
{
string[] parts = lin.Split(',');
string[] p1 = parts[0].Split('-');
string[] p2 = parts[1].Split('-');
ElfTask a = new ElfTask()
{
start = int.Parse(p1[0]),
end = int.Parse(p1[1])
};
ElfTask b = new ElfTask()
{
start = int.Parse(p2[0]),
end = int.Parse(p2[1])
};
if (DoOverlap(a, b))
{
sum++;
}
}
return sum;
}
private static bool DoOverlap(ElfTask taskA, ElfTask taskB)
{
if (taskA.end >= taskB.start && taskA.start <= taskB.end) return true;
if (taskB.end >= taskA.start && taskB.start <= taskA.end) return true;
return false;
}
}
}