-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.d
110 lines (95 loc) · 2.35 KB
/
day13.d
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
module day13;
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.uni;
void main() {
string[] input = File("input/day13.txt")
.byLine()
.map!(to!string)
.map!strip
// .filter!(line => line.length > 0)
.array();
Dot[] dots;
Fold[] folds;
bool atDots = true;
foreach (line; input) {
if (line.length == 0) {
atDots = false;
continue;
}
if (atDots) {
int[] pts = line.split(",").map!((c) => c.to!int).array;
dots ~= [Dot(pts[0], pts[1])];
} else /* lines */ {
string[] pts = line.split(" ")[2].split("=");
folds ~= [Fold(pts[0], pts[1].to!int)];
}
}
bool first = true;
foreach(fold; folds) {
if (fold.orientation == "x") {
for (int i = 0; i < dots.length; i++) {
Dot dot = dots[i];
if (dot.x > fold.position) {
dots[i].x = dot.x - (dot.x - fold.position) * 2;
}
}
} else if (fold.orientation == "y") {
for(int i = 0; i < dots.length; i++) {
Dot dot = dots[i];
if (dot.y > fold.position) {
dots[i].y = dot.y - (dot.y - fold.position) * 2;
}
}
} else {
throw new Exception("Invalid fold orientation");
}
if (first) {
first = false;
// pt 1
writeln(dots.unique.length);
}
}
// pt 2
for (int y = 0; y < 7; y++) {
for (int x = 0; x < 50; x++) {
if (dots.contains(x, y)) {
write("#");
} else {
write(".");
}
}
write("\n");
}
}
Dot[] unique(Dot[] dots) {
Dot[string] uniqueDots;
foreach (dot; dots) {
uniqueDots[dot.toString] = dot;
}
return uniqueDots.values;
}
bool contains(Dot[] dots, int x, int y) {
foreach(dot; dots) {
if (dot.x == x && dot.y == y) {
return true;
}
}
return false;
}
struct Dot {
int x;
int y;
string toString() const {
return "[" ~ x.to!string ~ "," ~ y.to!string ~ "]";
}
}
struct Fold {
string orientation;
int position;
}