generated from mazharenko/aoc-agent-template-multipleyears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay15.cs
188 lines (168 loc) · 5.29 KB
/
Day15.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using aoc.Common;
using mazharenko.AoCAgent.Generator;
using MoreLinq;
namespace aoc.Year2024;
internal abstract partial class Day15
{
public virtual (V<int> robot, V<int>[] stones, V<int>[] walls, V<int>[] moves) Parse(string input)
{
return Character.In('#', '.', 'O', '@').Map()
.Block()
.ThenBlock(
Character.In('<', 'v', '>', '^')
.Many().Lines()
.Select(ar => ar.SelectMany(x => x)
.Select(c => c switch
{
'>' => Directions.Right,
'<' => Directions.Left,
'^' => Directions.Up,
'v' => Directions.Down
}).ToArray())
)
.Select(t =>
{
var (map, moves) = t;
var allPoints = map.AsEnumerable().ToArray();
var robot = allPoints.Single(x => x.element == '@').point;
var walls = allPoints.Where(x => x.element == '#').Select(x => x.point).ToArray();
var stones = allPoints.Where(x => x.element == 'O').Select(x => x.point).ToArray();
return (robot, stones, walls, moves);
}).Parse(input);
}
protected abstract IEnumerable<V<int>> StonePoints(V<int> stonePos);
private int Solve((V<int> robot, V<int>[] stones, V<int>[] walls, V<int>[] moves) input)
{
var robotPos = input.robot;
var walls = input.walls.ToHashSet();
var occupiedByStones = OccupiedByStones(input.stones.ToHashSet());
foreach (var move in input.moves)
{
var direction = move;
var involvedStones = InvolvedStones(direction, robotPos + direction, occupiedByStones).ToList();
var allStonePoints = involvedStones.SelectMany(StonePoints).ToList();
var canMove = !allStonePoints.Select(x => x + direction).Any(walls.Contains)
&& !walls.Contains(robotPos + direction);
if (canMove)
{
foreach (var stonePoint in allStonePoints)
{
occupiedByStones.Remove(stonePoint);
}
foreach (var stone in involvedStones)
{
foreach (var stonePoint in StonePoints(stone))
{
occupiedByStones[stonePoint + direction] = stone + direction;
}
}
robotPos += direction;
}
}
return occupiedByStones.Values.Distinct().Sum(s => s.X * 100 + s.Y);
}
private Dictionary<V<int>, V<int>> OccupiedByStones(HashSet<V<int>> stones)
{
return stones
.SelectMany(s => StonePoints(s).Select(p => (p, s)))
.ToDictionary(x => x.Item1, x => x.Item2);
}
protected abstract IEnumerable<V<int>> InvolvedStones(V<int> dir, V<int> from,
IDictionary<V<int>, V<int>> occupiedByStones);
internal partial class Part1
{
private readonly Example example = new(
"""
########
#..O.O.#
##@.O..#
#...O..#
#.#.O..#
#...O..#
#......#
########
<^^>>>vv<v>>v<<
""", 2028);
protected override IEnumerable<V<int>> StonePoints(V<int> stonePos)
{
return [stonePos];
}
// source generator needs this. maybe want some improvement
public new int Solve((V<int> robot, V<int>[] stones, V<int>[] walls, V<int>[] moves) input)
{
return base.Solve(input);
}
protected override IEnumerable<V<int>> InvolvedStones(V<int> dir, V<int> from,
IDictionary<V<int>, V<int>> occupiedByStones)
{
return MoreEnumerable.GenerateByIndex(i => dir * i + from)
.TakeWhile(occupiedByStones.ContainsKey);
}
}
[BypassNoExamples]
internal partial class Part2
{
private readonly Example example = new(
"""
##########
#..O..O.O#
#......O.#
#.OO..O.O#
#O#..O...#
#O..O..O.#
#.OO.O.OO#
#....O...#
##########
<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^
""", 9021);
protected override IEnumerable<V<int>> StonePoints(V<int> stonePos)
{
return [stonePos, stonePos + Directions.Right];
}
public new int Solve((V<int> robot, V<int>[] stones, V<int>[] walls, V<int>[] moves) input)
{
V<int> Double(V<int> p) => V.Create(p.X, p.Y * 2);
var doubleInput =
(
robot: Double(input.robot),
stones: input.stones.Select(Double).ToArray(),
walls: input.walls.SelectMany(wall => new[] { Double(wall), Double(wall) + Directions.Right }).ToArray(),
input.moves
);
return base.Solve(doubleInput);
}
protected override IEnumerable<V<int>> InvolvedStones(V<int> dir, V<int> from,
IDictionary<V<int>, V<int>> occupiedByStones)
{
if (dir == Directions.Up || dir == Directions.Down)
{
if (occupiedByStones.TryGetValue(from, out var stone))
{
return
new[] { stone + dir, stone + dir + Directions.Right }
.SelectMany(ss =>
InvolvedStones(dir, ss, occupiedByStones)
.Append(stone)).Distinct();
}
}
else
{
return MoreEnumerable.GenerateByIndex(i => dir * i + from)
.TakeWhile(occupiedByStones.ContainsKey)
.Select(x => occupiedByStones[x])
.Distinct() ;
}
return [];
}
}
}