-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tadej Koderman
committed
Dec 4, 2024
1 parent
23d76bb
commit c001785
Showing
5 changed files
with
255 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,80 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace AoCCore; | ||
|
||
internal static class Read | ||
{ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace AoCCore; | ||
|
||
internal static class Read | ||
{ | ||
public static string Line() | ||
{ | ||
return Console.ReadLine() ?? string.Empty; | ||
} | ||
|
||
public static T Value<T>() | ||
where T : IParsable<T> | ||
{ | ||
return T.Parse(Line(), null); | ||
} | ||
|
||
public static bool Try<T>([NotNullWhen(true)] out T? result) | ||
where T : IParsable<T> | ||
{ | ||
return T.TryParse(Line(), null, out result); | ||
} | ||
|
||
public static IEnumerable<string> StringBatch() | ||
{ | ||
string line; | ||
while ((line = Line()).Length > 0) | ||
{ | ||
yield return line; | ||
} | ||
} | ||
|
||
public static IEnumerable<string[]> StringBatches() | ||
{ | ||
while (true) | ||
{ | ||
var batch = StringBatch().ToArray(); | ||
|
||
if (batch.Length == 0) yield break; | ||
|
||
yield return batch; | ||
} | ||
} | ||
|
||
public static IEnumerable<T> HBatch<T>(char separator = ' ') where T : IParsable<T> => Line().Split(separator, StringSplitOptions.RemoveEmptyEntries).Select(x => T.Parse(x, null)); | ||
public static IEnumerable<T[]> HBatches<T>(char separator = ' ') where T : IParsable<T> => StringBatch().Select(row => row.Split(separator, StringSplitOptions.RemoveEmptyEntries).Select(x => T.Parse(x, null)).ToArray()); | ||
|
||
public static IEnumerable<T> Batch<T>() where T : IParsable<T> => StringBatch().Select(x => T.Parse(x, null)); | ||
|
||
public static IEnumerable<T[]> Batches<T>() where T : IParsable<T> => StringBatches().Select(x => x.Select(v => T.Parse(v, null)).ToArray()); | ||
|
||
public static int Int() => Value<int>(); | ||
|
||
public static bool TryInt(out int value) => Try(out value); | ||
|
||
/// <summary> | ||
/// 1000 /n 2000 /n 3000 /n /n | ||
/// </summary> | ||
public static IEnumerable<int> IntBatch() => Batch<int>(); | ||
|
||
/// <summary> | ||
/// 1000 /n 2000 /n 3000 /n /n 7000 /n 8000 /n /n /n | ||
/// </summary> | ||
public static IEnumerable<int[]> IntBatches() => Batches<int>(); | ||
|
||
public static long Long() => Value<long>(); | ||
|
||
public static bool TryLong(out long value) => Try(out value); | ||
|
||
/// <summary> | ||
/// 1000 /n 2000 /n 3000 /n /n | ||
/// </summary> | ||
public static IEnumerable<long> LongBatch() => Batch<long>(); | ||
|
||
/// <summary> | ||
/// 1000 /n 2000 /n 3000 /n /n 7000 /n 8000 /n /n /n | ||
/// </summary> | ||
public static IEnumerable<long[]> LongBatches() => Batches<long>(); | ||
} | ||
|
||
public static T Value<T>() | ||
where T : IParsable<T> | ||
{ | ||
return T.Parse(Line(), null); | ||
} | ||
|
||
public static bool Try<T>([NotNullWhen(true)] out T? result) | ||
where T : IParsable<T> | ||
{ | ||
return T.TryParse(Line(), null, out result); | ||
} | ||
|
||
public static IEnumerable<string> StringBatch() | ||
{ | ||
string line; | ||
while ((line = Line()).Length > 0) | ||
{ | ||
yield return line; | ||
} | ||
} | ||
|
||
public static IEnumerable<string[]> StringBatches() | ||
{ | ||
while (true) | ||
{ | ||
var batch = StringBatch().ToArray(); | ||
|
||
if (batch.Length == 0) yield break; | ||
|
||
yield return batch; | ||
} | ||
} | ||
|
||
public static IEnumerable<T> HBatch<T>(char separator = ' ') where T : IParsable<T> => Line().Split(separator, StringSplitOptions.RemoveEmptyEntries).Select(x => T.Parse(x, null)); | ||
|
||
public static IEnumerable<T[]> HBatches<T>(char separator = ' ') where T : IParsable<T> => StringBatch().Select(row => row.Split(separator, StringSplitOptions.RemoveEmptyEntries).Select(x => T.Parse(x, null)).ToArray()); | ||
|
||
public static IEnumerable<T> Batch<T>() where T : IParsable<T> => StringBatch().Select(x => T.Parse(x, null)); | ||
|
||
public static IEnumerable<T[]> Batches<T>() where T : IParsable<T> => StringBatches().Select(x => x.Select(v => T.Parse(v, null)).ToArray()); | ||
|
||
public static int Int() => Value<int>(); | ||
|
||
public static bool TryInt(out int value) => Try(out value); | ||
|
||
/// <summary> | ||
/// 1000 /n 2000 /n 3000 /n /n | ||
/// </summary> | ||
public static IEnumerable<int> IntBatch() => Batch<int>(); | ||
|
||
/// <summary> | ||
/// 1000 /n 2000 /n 3000 /n /n 7000 /n 8000 /n /n /n | ||
/// </summary> | ||
public static IEnumerable<int[]> IntBatches() => Batches<int>(); | ||
|
||
public static long Long() => Value<long>(); | ||
|
||
public static bool TryLong(out long value) => Try(out value); | ||
|
||
/// <summary> | ||
/// 1000 /n 2000 /n 3000 /n /n | ||
/// </summary> | ||
public static IEnumerable<long> LongBatch() => Batch<long>(); | ||
|
||
/// <summary> | ||
/// 1000 /n 2000 /n 3000 /n /n 7000 /n 8000 /n /n /n | ||
/// </summary> | ||
public static IEnumerable<long[]> LongBatches() => Batches<long>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
namespace AoCCore; | ||
|
||
public static class Y2024 | ||
{ | ||
/* | ||
3 4 | ||
4 3 | ||
2 5 | ||
1 3 | ||
3 9 | ||
3 3 | ||
*/ | ||
public static void Day01A() | ||
{ | ||
var ints = Read.HBatches<int>().ToArray(); | ||
|
||
var left = ints.Select(x => x[0]).Order().ToArray(); | ||
var right = ints.Select(x => x[1]).Order().ToArray(); | ||
|
||
left.Zip(right).Select(x => Math.Abs(x.First - x.Second)).Sum().P("sum"); | ||
} | ||
|
||
public static void Day01B() | ||
{ | ||
var ints = Read.HBatches<int>().ToArray(); | ||
|
||
var right = ints.Select(x => x[1]).GroupBy(x => x, (key, values) => (key, values.Count())).ToDictionary(x => x.key, x => x.Item2); | ||
|
||
var lefts = new Dictionary<int, int>(); | ||
|
||
ints.Select(x => right.TryGetValue(x[0], out var score) ? (long)x[0] * score : 0).Sum().P("sum"); | ||
} | ||
|
||
/* | ||
7 6 4 2 1 | ||
1 2 7 8 9 | ||
9 7 6 2 1 | ||
1 3 2 4 5 | ||
8 6 4 4 1 | ||
1 3 6 7 9 | ||
*/ | ||
public static void Day02A() | ||
{ | ||
var ints = Read.HBatches<int>().ToArray(); | ||
|
||
ints.Sum(IsSafe).P("Safe"); | ||
|
||
static int IsSafe(int[] report) | ||
{ | ||
bool isIncreasing = report[1] > report[0]; | ||
|
||
for (int i = 1; i < report.Length; i++) | ||
{ | ||
if (report[i] == report[i - 1]) | ||
{ | ||
return 0; | ||
} | ||
|
||
if (isIncreasing ^ report[i] > report[i - 1]) | ||
{ | ||
return 0; | ||
} | ||
|
||
if (Math.Abs(report[i] - report[i - 1]) > 3) | ||
{ | ||
return 0; | ||
} | ||
} | ||
|
||
return 1; | ||
} | ||
} | ||
|
||
public static void Day02B() | ||
{ | ||
var ints = Read.HBatches<int>().ToArray(); | ||
|
||
int safes = 0; | ||
|
||
foreach (var report in ints) | ||
{ | ||
if (IsSafe(report)) | ||
{ | ||
safes++; | ||
continue; | ||
} | ||
|
||
for (var i = 0; i < report.Length; i++) | ||
{ | ||
if (IsSafe(report.Take(i).Concat(report.Skip(i + 1)).ToArray())) | ||
{ | ||
safes++; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
safes.P("Safe"); | ||
|
||
static bool IsSafe(int[] report) | ||
{ | ||
bool isIncreasing = report[1] > report[0]; | ||
|
||
for (int i = 1; i < report.Length; i++) | ||
{ | ||
if (report[i] == report[i - 1]) | ||
{ | ||
return false; | ||
} | ||
|
||
if (isIncreasing ^ report[i] > report[i - 1]) | ||
{ | ||
return false; | ||
} | ||
|
||
if (Math.Abs(report[i] - report[i - 1]) > 3) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
/* | ||
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) | ||
*/ | ||
public static void Day03A() | ||
{ | ||
new Regex(@"mul\((\d+),(\d+)\)") | ||
.Matches(Read.StringBatch().Join()) | ||
.Sum(m => m.Groups[1].Value.Long() * m.Groups[2].Value.Int()) | ||
.P("Sum"); | ||
|
||
// 179834255 | ||
} | ||
|
||
/* | ||
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) | ||
*/ | ||
public static void Day03B() | ||
{ | ||
new Regex(@"mul\((\d+),(\d+)\)|(do\(\))|(don't\(\))") | ||
.Matches(string.Join(",", Read.StringBatch())) | ||
.Aggregate( | ||
(Sum: 0L, Enabled: true), | ||
(a, m) => m.Groups[3].Success | ||
? (a.Sum, true) | ||
: m.Groups[4].Success | ||
? (a.Sum, false) | ||
: a.Enabled | ||
? (a.Sum + m.Groups[1].Value.Long() * m.Groups[2].Value.Int(), true) | ||
: (a.Sum, false)) | ||
.Sum | ||
.P(); | ||
|
||
// 80570939 | ||
} | ||
|
||
public static void Current() // Day05A() | ||
{ | ||
} | ||
} |