Skip to content

Commit

Permalink
Add 2024, days 1, 2, 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadej Koderman committed Dec 4, 2024
1 parent 23d76bb commit c001785
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 83 deletions.
3 changes: 3 additions & 0 deletions AoCCore/Calc.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace AoCCore;

/// <summary>
/// GreatestCommonFactor and LeastCommonMultiple
/// </summary>
internal class Calc
{
public static long GCF(long a, long b)
Expand Down
13 changes: 8 additions & 5 deletions AoCCore/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ public static void Assert(this Expression<Func<bool>> test)
}
}

public static void P<T>(this T error)
{
Console.WriteLine(error);
}

public static void P<T>(this T value, [CallerArgumentExpression(nameof(value))] string? message = null)
{
Console.WriteLine($"{message}: {value}");
Expand All @@ -48,4 +43,12 @@ public static void P<T>(this IEnumerable<T> values, string message)
foreach (T value in values)
value.P();
}

public static T As<T>(this string input) where T : IParsable<T> => T.Parse(input, null);

public static int Int(this string input) => input.As<int>();

public static long Long(this string input) => input.As<long>();

public static string Join(this IEnumerable<string> input, string separator = ",") => string.Join(separator, input);
}
5 changes: 2 additions & 3 deletions AoCCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using AoCCore;
using AoCCore;

while (true)
{
Y2023.Current();
Y2024.Current();

Console.WriteLine();
Console.WriteLine("Paste data again..");
Expand Down
151 changes: 76 additions & 75 deletions AoCCore/Read.cs
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>();
}
166 changes: 166 additions & 0 deletions AoCCore/Y2024.cs
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()
{
}
}

0 comments on commit c001785

Please sign in to comment.