-
Notifications
You must be signed in to change notification settings - Fork 1
/
day13_origami.ts
134 lines (115 loc) · 3.02 KB
/
day13_origami.ts
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
import fs from "fs";
import assert from "assert";
import _ from "lodash";
const TEST_INPUT = `6,10
0,14
9,10
0,3
10,4
4,11
6,0
6,12
4,1
0,13
10,12
3,4
3,0
8,4
1,10
2,14
8,10
9,0
fold along y=7
fold along x=5`;
const parseInput = (puzzleInput: string): [Set<string>, string[]] => {
const [_dots, _folds] = puzzleInput.split("\n\n");
const dots: Set<string> = new Set();
_dots
.trim()
.split("\n")
.forEach((value) => dots.add(value));
const folds: string[] = [];
_folds
.trim()
.split("\n")
.forEach((value) => {
const fold_info = value.split("fold along ");
folds.push(fold_info[1]);
});
return [dots, folds];
};
const puzzleInput = fs
.readFileSync("2021/data/day13_input.txt")
.toString()
.trim();
const foldPage = (dots: Set<string>, foldLine: number, foldIndex: 1 | 0) => {
// y means fold up; foldIndex == 1
// x means fold left, foldIndex == 0
const nums = [...dots].map((key) => key.split(",").map(Number)[foldIndex]);
const numMin = Math.min(...nums);
const numMax = Math.max(...nums);
const originHasChanged = numMax - foldLine > foldLine - numMin;
const transformAmount = Math.abs(2 * foldLine - numMax);
let updatedDots = [...dots].map((key) => {
const value = key.split(",").map(Number);
// point has not changed
if (value[foldIndex] < foldLine) return key;
let newCoordinate = 2 * foldLine - value[foldIndex];
if (originHasChanged) {
newCoordinate += transformAmount;
}
if (foldIndex == 1) return `${value[0]},${newCoordinate}`;
return `${newCoordinate},${value[1]}`;
});
return new Set(updatedDots);
};
// part 1
const part1 = (puzzleInput: string) => {
let [dots, folds] = parseInput(puzzleInput);
const fold = folds.slice(0)[0];
const foldLine = parseInt(fold.split("=")[1]);
if (fold.split("=")[0] == "x") {
dots = foldPage(dots, foldLine, 0);
} else {
dots = foldPage(dots, foldLine, 1);
}
return dots.size;
};
console.log(part1(TEST_INPUT));
console.time("part 1");
console.log(part1(puzzleInput));
console.timeEnd("part 1");
// part 2
const drawDots = (dots: Set<string>) => {
const xs = [...dots].map((key) => key.split(",").map(Number)[0]);
const xMax = Math.max(...xs);
const ys = [...dots].map((key) => key.split(",").map(Number)[0]);
const yMax = Math.max(...ys);
const grid = new Array(yMax + 1)
.fill(".")
.map((value) => new Array(xMax + 1).fill("."));
for (const dot of dots) {
const [x, y] = dot.split(",").map(Number);
grid[y][x] = "#";
}
let printString = "";
grid.forEach((row) => {
printString += row.join("") + "\n";
});
console.log(printString);
};
const part2 = (puzzleInput: string) => {
let [dots, folds] = parseInput(puzzleInput);
for (const fold of folds) {
const foldLine = parseInt(fold.split("=")[1]);
if (fold.split("=")[0] == "x") {
dots = foldPage(dots, foldLine, 0);
} else {
dots = foldPage(dots, foldLine, 1);
}
}
return drawDots(dots);
};
console.time("part 2");
part2(puzzleInput);
console.timeEnd("part 2");