Skip to content

Commit aa5e820

Browse files
committed
2024 Day 7
1 parent 4e32615 commit aa5e820

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

src/2024/07/Program.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
11
using AdventOfCode.Common;
22

3-
var lines = Resources.GetInputFileLines();
3+
var equations = Resources.GetInputFileLines()
4+
.Select(line => line.SplitBy(": "))
5+
.Select(parts => new Equation(ulong.Parse(parts[0]), [.. parts[1].Split(' ').Select(ulong.Parse)]))
6+
.ToArray();
47

5-
Console.WriteLine($"Part 1: {""}");
6-
Console.WriteLine($"Part 2: {""}");
8+
var validEquations = equations
9+
.Where(equation => equation.IsValid(useConcatenation: false))
10+
.Select(equation => equation.Target)
11+
.ToList();
12+
13+
var total = validEquations
14+
.Sum();
15+
16+
var totalWithConcatenation = equations
17+
.Where(e => !validEquations.Contains(e.Target))
18+
.Where(e => e.IsValid(useConcatenation: true))
19+
.Select(equation => equation.Target)
20+
.Sum();
21+
22+
Console.WriteLine($"Part 1: {total}");
23+
Console.WriteLine($"Part 2: {total + totalWithConcatenation}");
24+
25+
file record Equation(ulong Target, ulong[] Operands)
26+
{
27+
public bool IsValid(bool useConcatenation)
28+
=> IsValid(useConcatenation, 0, 0);
29+
30+
private bool IsValid(bool useConcatenation, ulong acc, int index)
31+
{
32+
if (index == Operands.Length) return acc == Target;
33+
if (acc > Target) return false;
34+
35+
return IsValid(useConcatenation, Math.Max(acc, 1) * Operands[index], index + 1)
36+
|| IsValid(useConcatenation, acc + Operands[index], index + 1)
37+
|| (useConcatenation && IsValid(true, Concatenate(acc, Operands[index]), index + 1));
38+
}
39+
40+
// 1000 with as many zeros as a base-10 log (and account for '0')
41+
private static ulong Concatenate(ulong first, ulong second)
42+
=> first * (ulong)Math.Max(10, Math.Pow(10, Math.Ceiling(Math.Log10(second)))) + second;
43+
}

src/2024/08/Program.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
int GetAntinodeCount(bool allInLine)
1515
{
1616
return antennasByFrequency
17-
.SelectMany(group => group
18-
.AllCombinations(includeIdentities: false)
19-
.Where(pair => pair.Item1.Col < pair.Item2.Col) // We don't need both combinations (x,y) and (y,x)
20-
.SelectMany(pair => allInLine
21-
? GetAntinodes2(pair.Item1, pair.Item2)
22-
: GetAntinodes1(pair.Item1, pair.Item2)))
23-
.Distinct()
24-
.Count();
17+
.SelectMany(group => group
18+
.AllCombinations(includeIdentities: false)
19+
.Where(pair => pair.Item1.Col < pair.Item2.Col) // We don't need both combinations (x,y) and (y,x)
20+
.SelectMany(pair => allInLine
21+
? GetAntinodes2(pair.Item1, pair.Item2)
22+
: GetAntinodes1(pair.Item1, pair.Item2)))
23+
.Distinct()
24+
.Count();
2525
}
2626

2727
IEnumerable<Coor> GetAntinodes1(Coor a1, Coor a2)

src/Common/Resources.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,8 @@ public static long GetLeastCommonMultiple(long a, long b)
162162
{
163163
return a * b / GetGreatestCommonDivisor(a, b);
164164
}
165+
166+
public static ulong Sum(this IEnumerable<ulong> numbers)
167+
=> numbers.Aggregate(0UL, (i, acc) => i + acc);
168+
165169
}

0 commit comments

Comments
 (0)