-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay4.cs
63 lines (51 loc) · 1.79 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
namespace advent_of_code_2022;
public class Day4
{
public static void Part1(string[] args)
{
string[] lines = File.ReadAllLines("day4-input.txt");
int result = 0;
foreach (var line in lines)
{
var pairs = line.Split(',');
var firstParts = pairs[0].Split('-');
int firstLeft = int.Parse(firstParts[0]);
int firstRight = int.Parse(firstParts[1]);
var secondParts = pairs[1].Split('-');
int secondLeft = int.Parse(secondParts[0]);
int secondRight = int.Parse(secondParts[1]);
if (firstLeft <= secondLeft && firstRight >= secondRight)
{
Console.WriteLine(line);
result++;
continue;
}
if (secondLeft <= firstLeft && secondRight >= firstRight)
{
result++;
Console.WriteLine(line);
}
}
Console.WriteLine(result);
}
public static void Solve(string[] args)
{
string[] lines = File.ReadAllLines("day4-input.txt");
int notOverlap = 0;
foreach (var line in lines)
{
var pairs = line.Split(',');
var firstParts = pairs[0].Split('-');
int firstLeft = int.Parse(firstParts[0]);
int firstRight = int.Parse(firstParts[1]);
var secondParts = pairs[1].Split('-');
int secondLeft = int.Parse(secondParts[0]);
int secondRight = int.Parse(secondParts[1]);
if (firstRight < secondLeft || secondRight < firstLeft)
{
notOverlap++;
}
}
Console.WriteLine(lines.Length - notOverlap);
}
}