-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay02.cs
More file actions
89 lines (75 loc) · 2.25 KB
/
Day02.cs
File metadata and controls
89 lines (75 loc) · 2.25 KB
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
86
87
88
89
using System.Diagnostics;
using System;
namespace _2025._02;
public sealed class Day02 : Base
{
public Day02(bool example) : base(example)
{
Day = "02";
}
private IEnumerable<(ulong, ulong)> Parse(string[] input)
{
return input.Select(x =>
{
var split = x.Split("-");
return (ulong.Parse(split[0]), ulong.Parse(split[1]));
});
}
public override object PartOne()
{
string[] input = ReadInput()[0].Split(",");
ulong invalidIds = 0;
foreach((ulong lower, ulong upper) in Parse(input))
{
for(ulong it = lower; it <= upper; it++)
{
string id = it.ToString();
if (id.Length % 2 != 0)
{
continue;
}
if (id[0..(id.Length/2)] == id[(id.Length/2)..])
{
invalidIds += it;
}
}
}
return invalidIds.ToString();
}
public override object PartTwo()
{
string[] input = ReadInput()[0].Split(",");
ulong invalidIds = 0;
foreach((ulong lower, ulong upper) in Parse(input))
{
for(ulong it = lower; it <= upper; it++)
{
string id = it.ToString();
for(int idx = 1; idx <= id.Length/2; idx++)
{
string first = id[0..idx];
bool repeats = true;
if(id.Length % idx != 0)
{
continue;
}
for(int check = idx; check < id.Length; check += idx)
{
if(id[check..(check+idx)] != first)
{
repeats = false;
break;
}
}
if (repeats)
{
invalidIds += it;
break;
}
}
}
}
return invalidIds.ToString();
}
}