diff --git a/AdventOfCode2026/Day2.cs b/AdventOfCode2026/Day2.cs new file mode 100644 index 0000000..babc9eb --- /dev/null +++ b/AdventOfCode2026/Day2.cs @@ -0,0 +1,102 @@ +namespace AdventOfCode2026; + +public class Day2 : Solution +{ + public override string SolvePart1() + { + return InputReader + .ReadLines(2) + .SelectMany(ToRanges) + .Select(ToLimit) + .SelectMany(GetIds) + .Where(id => IsTwiceSequence(id.ToString())) + .Sum() + .ToString(); + } + + public override string SolvePart2() + { + return InputReader + .ReadLines(2) + .SelectMany(ToRanges) + .Select(ToLimit) + .SelectMany(GetIds) + .Where(id => IsTwiceOrMoreSequence(id.ToString())) + .Sum() + .ToString(); + } + + private static IEnumerable GetIds((string from, string to) limit) => GetIds(limit.from, limit.to); + + private static IEnumerable GetIds(string from, string to) + { + var current = long.Parse(from); + var end = long.Parse(to); + while (current <= end) + { + yield return current; + current++; + } + } + + private static bool IsTwiceOrMoreSequence(string input) + { + if (input.Length < 2) + { + return false; + } + + for (var subLength = 1; subLength <= input.Length / 2; subLength++) + { + if (input.Length % subLength != 0) + { + continue; + } + + var testBlock = input[..subLength]; + var theoreticalString = string.Concat(Enumerable.Repeat(testBlock, input.Length / subLength)); + if (theoreticalString == input) + { + return true; + } + } + + return false; + } + + private static bool IsTwiceSequence(string input) + { + if (input.Length < 2) + { + return false; + } + + if (input.Length % 2 != 0) + { + return false; + } + + var firstHalfIndex = 0; + var secondHalfIndex = input.Length / 2; + while (secondHalfIndex < input.Length) + { + if (input[firstHalfIndex] != input[secondHalfIndex]) + { + return false; + } + + firstHalfIndex++; + secondHalfIndex++; + } + + return true; + } + + private static (string from, string to) ToLimit(string range) + { + var parts = range.Split('-'); + return (parts[0], parts[1]); + } + + private static IEnumerable ToRanges(string line) => line.Split(','); +} \ No newline at end of file diff --git a/AdventOfCode2026/Inputs/Day2_0.txt b/AdventOfCode2026/Inputs/Day2_0.txt new file mode 100644 index 0000000..bd04584 --- /dev/null +++ b/AdventOfCode2026/Inputs/Day2_0.txt @@ -0,0 +1 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 \ No newline at end of file diff --git a/AdventOfCode2026/Inputs/Day2_1.txt b/AdventOfCode2026/Inputs/Day2_1.txt new file mode 100644 index 0000000..8d56767 --- /dev/null +++ b/AdventOfCode2026/Inputs/Day2_1.txt @@ -0,0 +1 @@ +269351-363914,180-254,79-106,771-1061,4780775-4976839,7568-10237,33329-46781,127083410-127183480,19624-26384,9393862801-9393974421,2144-3002,922397-1093053,39-55,2173488366-2173540399,879765-909760,85099621-85259580,2-16,796214-878478,163241-234234,93853262-94049189,416472-519164,77197-98043,17-27,88534636-88694588,57-76,193139610-193243344,53458904-53583295,4674629752-4674660925,4423378-4482184,570401-735018,280-392,4545446473-4545461510,462-664,5092-7032,26156828-26366132,10296-12941,61640-74898,7171671518-7171766360,3433355031-3433496616 \ No newline at end of file