-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday15.d
More file actions
130 lines (108 loc) · 3.2 KB
/
Copy pathday15.d
File metadata and controls
130 lines (108 loc) · 3.2 KB
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
module day15;
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.range;
import std.regex;
import std.stdio;
import std.string;
void main() {
uint[][] input = File("input/day15.txt")
.byLine()
.map!(to!string)
.map!strip
.filter!(line => line.length > 0)
.map!(line => line.map!(c => c.to!string.to!uint).array())
.array();
auto cavern = new Array2d!(uint)(input.join(), input[0].length);
// cavern.print();
auto bigCavern = new Array2d!(uint)(input[0].length * 5, input.length * 5);
for (int y = 0; y < bigCavern.height; y++) {
for (int x = 0; x < bigCavern.width; x++) {
int mx = x / cavern.width;
int my = y / cavern.height;
int ox = x % cavern.width;
int oy = y % cavern.height;
bigCavern[x, y] = cavern[ox, oy] + mx + my;
bigCavern[x, y] = ((bigCavern[x, y] - 1) % 9) + 1;
}
}
// bigCavern.print();
// pt 1
writeln(lowestRiskLevel(cavern));
// pt 2
writeln(lowestRiskLevel(bigCavern));
}
int lowestRiskLevel(Array2d!(uint) cavern) {
int[Position] risks;
risks[Position(0, 0)] = 0;
auto frontier = BinaryHeap!(Position[], (a, b) => risks[a] > risks[b])([], 0);
frontier.insert(Position(0, 0));
while (!frontier.empty()) {
Position pos = frontier.front();
frontier.removeFront();
if (pos.x == cavern.width - 1 && pos.y == cavern.height - 1) {
return risks[pos];
}
foreach (adj; pos.adjacent(cavern.width - 1, cavern.height - 1)) {
int nextRisk = risks[pos] + cavern[adj.x, adj.y];
if (!(adj in risks) || nextRisk < risks[adj]) {
risks[adj] = nextRisk;
frontier.insert(Position(adj.x, adj.y));
}
}
}
return 0;
}
class Array2d(T) {
private T[] arr;
private uint width;
private uint height;
public this(uint width, uint height) {
this.arr = new T[width * height];
this.width = width;
this.height = height;
}
public this(T[] arr, uint width) {
this.arr = arr;
this.width = width;
this.height = arr.length / width;
}
ref T opIndex(uint x, uint y) {
return arr[x + y * width];
}
int opApply(int delegate(ref T) dg) {
int result = 0;
foreach (T item; arr) {
if (dg(item)) {
result = 1;
break;
}
}
return result;
}
public void print() {
for (int i = 0; i < arr.length; i++) {
auto a = arr[i];
write(a);
if (i % width == width - 1) {
write("\n");
}
}
write("\n");
}
}
struct Position {
uint x;
uint y;
Position[] adjacent(int boundX, int boundY) {
Position[] adjs;
if (this.x > 0) adjs ~= [Position(this.x - 1, this.y)];
if (this.x < boundX) adjs ~= [Position(this.x + 1, this.y)];
if (this.y > 0) adjs ~= [Position(this.x, this.y - 1)];
if (this.y < boundY) adjs ~= [Position(this.x, this.y + 1)];
return adjs;
}
}