generated from mazharenko/aoc-agent-template-multipleyears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay14.cs
87 lines (78 loc) · 2.29 KB
/
Day14.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using aoc.Common;
using mazharenko.AoCAgent.Generator;
using MoreLinq;
using ParsingExtensions;
namespace aoc.Year2024;
internal partial class Day14
{
public (V<int> p, V<int> v)[] Parse(string input)
{
return Template.Matching<(int py, int px, int vy, int vx)>(
$"p={Numerics.IntegerInt32},{Numerics.IntegerInt32} v={Numerics.IntegerInt32},{Numerics.IntegerInt32}"
)
.Select(t => (V.Create(t.px, t.py), V.Create(t.vx, t.vy)))
.Lines().Parse(input);
}
[BypassNoExamples]
internal partial class Part1
{
public int Solve((V<int> p, V<int> v)[] input)
{
const int x = 103;
const int y = 101;
const int steps = 100;
var newPositions = input.Select(guard =>
{
var newPosition = guard.p + guard.v * steps;
return V.Create(newPosition.X.EuclideanRemainder(x), newPosition.Y.EuclideanRemainder(y));
}).ToList();
var q1 = newPositions.Where(v => v is { X: < x / 2, Y: < y / 2 });
var q2 = newPositions.Where(v => v is { X: >= x / 2 + 1, Y: < y / 2 });
var q3 = newPositions.Where(v => v is { X: < x / 2, Y: >= y / 2 + 1 });
var q4 = newPositions.Where(v => v is { X: >= x / 2 + 1, Y: >= y / 2 + 1 });
return q1.Count() * q2.Count() * q3.Count() * q4.Count();
}
}
[BypassNoExamples]
internal partial class Part2
{
public int Solve((V<int> p, V<int> v)[] input)
{
const int x = 103;
const int y = 101;
var likelyPatterns =
(1..10000).AsEnumerable()
.Scan((steps: 0, guards: input), (t, i) =>
{
return (i, t.guards.Select(guard =>
{
var newPosition = guard.p + guard.v;
return (
V.Create(newPosition.X.EuclideanRemainder(x),
newPosition.Y.EuclideanRemainder(y)),
guard.v
);
}
).ToArray());
})
.Where(t =>
{
// assume the Christmas tree has many triangles
// #..
// ##.
// ###
var unique = t.guards.Select(g => g.p).ToHashSet();
var triangles = unique.Where(guard =>
unique.IsSupersetOf([
guard + Directions.SE, guard + Directions.S, guard + Directions.SW,
guard + Directions.W, guard + Directions.NW
]) && !unique.Overlaps([
guard + Directions.N, guard + Directions.NE, guard + Directions.E
])
);
return triangles.Count() >= 10;
});
return likelyPatterns.First().steps;
}
}
}