-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle19.cs
214 lines (172 loc) · 8.28 KB
/
Puzzle19.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.Linq;
namespace AOC2022
{
class Blueprint
{
public Blueprint(int multiplier, string line)
{
this.multiplier = multiplier;
string[] descriptions = line.Split(":")[1].Split(".");
string[] description_zero_words = descriptions[0].Split(" ");
string[] description_one_words = descriptions[1].Split(" ");
string[] description_two_words = descriptions[2].Split(" ");
string[] description_three_words = descriptions[3].Split(" ");
ore_robot_ore_cost = int.Parse(description_zero_words[5]);
clay_robot_ore_cost = int.Parse(description_one_words[5]);
obsidian_robot_ore_cost = int.Parse(description_two_words[5]);
obsidian_robot_clay_cost = int.Parse(description_two_words[8]);
geode_robot_ore_cost = int.Parse(description_three_words[5]);
geode_robot_obsidian_cost = int.Parse(description_three_words[8]);
max_ore_cost = Math.Max(Math.Max(ore_robot_ore_cost, clay_robot_ore_cost), Math.Max(obsidian_robot_ore_cost, geode_robot_ore_cost));
}
public readonly int multiplier = 0;
public readonly int ore_robot_ore_cost = 0;
public readonly int clay_robot_ore_cost = 0;
public readonly int obsidian_robot_ore_cost = 0;
public readonly int obsidian_robot_clay_cost = 0;
public readonly int geode_robot_ore_cost = 0;
public readonly int geode_robot_obsidian_cost = 0;
public readonly int max_ore_cost;
}
class Puzzle19State
{
public int ore = 0;
public int clay = 0;
public int obsidian = 0;
public int geodes = 0;
public int ore_robots = 1;
public int clay_robots = 0;
public int obsidian_robots = 0;
public int time = 1;
public int skips = 0;
public Puzzle19State Clone()
{
Puzzle19State new_state = new Puzzle19State
{
ore = ore,
clay = clay,
obsidian = obsidian,
geodes = geodes,
ore_robots = ore_robots,
clay_robots = clay_robots,
obsidian_robots = obsidian_robots,
time = time,
skips = skips
};
return new_state;
}
public string MemoizationString
{
get
{
return string.Format("{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}-{8}", ore, clay, obsidian, geodes, ore_robots, clay_robots, obsidian_robots, time, skips);
}
}
}
partial class Program
{
static void Puzzle19()
{
string[] lines = System.IO.File.ReadAllLines("puzzles/input19.txt");
Blueprint[] all_blueprints = new Blueprint[lines.Length];
for (int i = 0; i < lines.Length; i++)
{
all_blueprints[i] = new Blueprint(i + 1, lines[i]);
}
List<Tuple<Blueprint, int>> GetBestGeodes(int time_limit, IEnumerable<Blueprint> blueprints)
{
var scores = new List<Tuple<Blueprint, int>>();
foreach (Blueprint blueprint in blueprints)
{
Puzzle19State best_state = null;
Queue<Puzzle19State> state_queue = new Queue<Puzzle19State>();
state_queue.Enqueue(new Puzzle19State());
Dictionary<string, Puzzle19State> all_states = new Dictionary<string, Puzzle19State>();
void TryEnqueue(Puzzle19State state)
{
string memoization_string = state.MemoizationString;
if (all_states.ContainsKey(memoization_string))
{
return;
}
all_states.Add(memoization_string, state);
state_queue.Enqueue(state);
}
while (state_queue.TryDequeue(out Puzzle19State state))
{
bool skip_obsidian = (state.skips & (1 << 2)) != 0;
bool skip_clay = (state.skips & (1 << 1)) != 0;
bool skip_ore = (state.skips & (1 << 0)) != 0;
bool can_craft_geode_robot = state.ore >= blueprint.geode_robot_ore_cost && state.obsidian >= blueprint.geode_robot_obsidian_cost;
bool can_craft_obsidian_robot = !skip_obsidian && state.obsidian_robots < blueprint.geode_robot_obsidian_cost && state.ore >= blueprint.obsidian_robot_ore_cost && state.clay >= blueprint.obsidian_robot_clay_cost;
bool can_craft_clay_robot = !skip_clay && state.clay_robots < blueprint.obsidian_robot_clay_cost && state.ore >= blueprint.clay_robot_ore_cost;
bool can_craft_ore_robot = !skip_ore && state.ore_robots < blueprint.max_ore_cost && state.ore >= blueprint.ore_robot_ore_cost;
state.ore += state.ore_robots;
state.clay += state.clay_robots;
state.obsidian += state.obsidian_robots;
if (state.time == time_limit)
{
if (best_state == null || state.geodes > best_state.geodes)
{
best_state = state;
}
continue;
}
state.time++;
if (can_craft_geode_robot)
{
Puzzle19State new_state = state.Clone();
new_state.ore -= blueprint.geode_robot_ore_cost;
new_state.obsidian -= blueprint.geode_robot_obsidian_cost;
new_state.geodes += ((time_limit - state.time) + 1);
new_state.skips = 0;
TryEnqueue(new_state);
}
else
{
int skips = 0;
if (can_craft_obsidian_robot)
{
skips += 1 << 2;
Puzzle19State new_state = state.Clone();
new_state.ore -= blueprint.obsidian_robot_ore_cost;
new_state.clay -= blueprint.obsidian_robot_clay_cost;
new_state.obsidian_robots++;
new_state.skips = 0;
TryEnqueue(new_state);
}
if (can_craft_clay_robot)
{
skips += 1 << 1;
Puzzle19State new_state = state.Clone();
new_state.ore -= blueprint.clay_robot_ore_cost;
new_state.clay_robots++;
new_state.skips = 0;
TryEnqueue(new_state);
}
if (can_craft_ore_robot)
{
skips += 1 << 0;
Puzzle19State new_state = state.Clone();
new_state.ore -= blueprint.ore_robot_ore_cost;
new_state.ore_robots++;
new_state.skips = 0;
TryEnqueue(new_state);
}
state.skips |= skips;
TryEnqueue(state);
}
}
scores.Add(new Tuple<Blueprint, int>(blueprint, best_state.geodes));
}
return scores;
}
int score = GetBestGeodes(24, all_blueprints).Sum(x => x.Item2 * x.Item1.multiplier);
int score2 = GetBestGeodes(32, all_blueprints.Take(3)).Aggregate(1, (x, y) => x * y.Item2);
Console.WriteLine("{0}", score);
Console.WriteLine("{0}", score2);
}
}
}