Skip to content

Commit d4d14c6

Browse files
committed
Solve 2024 day 7
1 parent 5a0a008 commit d4d14c6

File tree

3 files changed

+988
-0
lines changed

3 files changed

+988
-0
lines changed

2024.Tests/Day07Tests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Basje.AdventOfCode.Y2024.D07;
2+
3+
namespace Basje.AdventOfCode.Y2024.Tests;
4+
5+
public static class Day07Tests
6+
{
7+
[Fact]
8+
public static void Part1()
9+
{
10+
var solution = new Day07();
11+
const string input = """
12+
190: 10 19
13+
3267: 81 40 27
14+
83: 17 5
15+
156: 15 6
16+
7290: 6 8 6 15
17+
161011: 16 10 13
18+
192: 17 8 14
19+
21037: 9 7 18 13
20+
292: 11 6 16 20
21+
""";
22+
23+
var answer = solution.SolvePart1(input);
24+
25+
answer.Should().Be(3749);
26+
}
27+
28+
[Fact]
29+
public static void Part2()
30+
{
31+
var solution = new Day07();
32+
const string input = """
33+
190: 10 19
34+
3267: 81 40 27
35+
83: 17 5
36+
156: 15 6
37+
7290: 6 8 6 15
38+
161011: 16 10 13
39+
192: 17 8 14
40+
21037: 9 7 18 13
41+
292: 11 6 16 20
42+
""";
43+
44+
var answer = solution.SolvePart2(input);
45+
46+
answer.Should().Be(11387);
47+
}
48+
}

2024/D07/Day07.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using Basje.AdventOfCode.Y2024.Toolbox.Grid;
2+
3+
namespace Basje.AdventOfCode.Y2024.D07;
4+
5+
public class Day07 : Solution<long[][]>
6+
{
7+
protected override long[][] ParseInput(string input)
8+
{
9+
return input
10+
.PerLine()
11+
.IgnoreEmptyLines()
12+
.Select(line => line.Split(": ".ToArray(), StringSplitOptions.RemoveEmptyEntries))
13+
.Select(line => line.Select(long.Parse).ToArray())
14+
.ToArray();
15+
}
16+
17+
protected override object SolvePart1(long[][] equations)
18+
{
19+
long sum = 0;
20+
21+
foreach (var equation in equations)
22+
{
23+
var test = equation.First();
24+
var values = CalculateTestValues(equation.Skip(1).Take(1).ToList(), equation.Skip(2).ToList());
25+
26+
if (values.Contains(test))
27+
{
28+
sum += test;
29+
}
30+
}
31+
32+
return sum;
33+
}
34+
35+
protected override object SolvePart2(long[][] equations)
36+
{
37+
long sum = 0;
38+
39+
foreach (var equation in equations)
40+
{
41+
var test = equation.First();
42+
var values = CalculateTestValues2(equation.Skip(1).Take(1).ToList(), equation.Skip(2).ToList());
43+
44+
if (values.Contains(test))
45+
{
46+
sum += test;
47+
}
48+
}
49+
50+
return sum;
51+
}
52+
53+
private List<long> CalculateTestValues(List<long> values, List<long> input)
54+
{
55+
if (input.Count == 0) return values;
56+
57+
List<long> newValues = [];
58+
var firstInput = input.First();
59+
60+
foreach (var number in values)
61+
{
62+
newValues.Add(number + firstInput);
63+
newValues.Add(number * firstInput);
64+
}
65+
66+
return CalculateTestValues(newValues, input.Skip(1).ToList());
67+
}
68+
69+
private List<long> CalculateTestValues2(List<long> values, List<long> input)
70+
{
71+
if (input.Count == 0) return values;
72+
73+
List<long> newValues = [];
74+
var firstInput = input.First();
75+
76+
foreach (var number in values)
77+
{
78+
newValues.Add(number + firstInput);
79+
newValues.Add(number * firstInput);
80+
newValues.Add(long.Parse($"{number}{firstInput}"));
81+
}
82+
83+
return CalculateTestValues2(newValues, input.Skip(1).ToList());
84+
}
85+
}
86+
87+
internal static class Day07Extensions
88+
{
89+
90+
}

0 commit comments

Comments
 (0)