-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.ts
146 lines (119 loc) · 3.03 KB
/
day18.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
135
136
137
138
139
140
141
142
143
144
145
146
const test_input = `R 6 (#70c710)
D 5 (#0dc571)
L 2 (#5713f0)
D 2 (#d2c081)
R 2 (#59c680)
D 2 (#411b91)
L 5 (#8ceee2)
U 2 (#caa173)
L 1 (#1b58a2)
U 2 (#caa171)
R 2 (#7807d2)
U 3 (#a77fa3)
L 2 (#015232)
U 2 (#7a21e3)`.split('\n');
const puzzle_input = `<get from aoc website>`.split('\n');
function log(u: unknown) {
console.log(JSON.stringify(u, undefined, 2));
}
type Coords = [row: number, col: number];
function printGrid(coords: Coords[]) {
const max = coords.reduce((a, c) => Math.max(a, c[0], c[1]), 0) + 1;
const output: string[][] = new Array(max);
for (let i = 0; i < output.length; i++) {
output[i] = new Array(max).fill('.');
}
for (let i = 1; i < coords.length; i++) {
const [x1, y1] = coords[i - 1];
const [x2, y2] = coords[i];
let start = Math.min(x1, x2);
let end = Math.max(x1, x2);
while (start <= end) {
output[start][y1] = '#';
start++;
}
start = Math.min(y1, y2);
end = Math.max(y1, y2);
while (start <= end) {
output[x1][start] = '#';
start++;
}
}
log({ o: output.map((r) => r.join('')) });
}
function part1(input: string[]) {
let circ = 0;
const coords = input.reduce<Coords[]>(
(acc, line) => {
const [d, c] = line.split(' ');
const cnt = Number(c);
circ += Math.abs(cnt);
const [vert, hor] = acc[acc.length - 1];
switch (d) {
case 'R': // right ++
acc.push([vert, hor + cnt]);
break;
case 'L': // left --
acc.push([vert, hor - cnt]);
break;
case 'U': // up --
acc.push([vert - cnt, hor]);
break;
case 'D': // down ++
acc.push([vert + cnt, hor]);
break;
}
return acc;
},
[[0, 0]],
);
let surface = 0;
for (let i = 1; i < coords.length; i++) {
const [x1, y1] = coords[i - 1];
const [x2, y2] = coords[i];
surface += (x1 * y2 - x2 * y1) / 2;
}
log({ circ, surface });
log(Math.abs(surface) + circ / 2 + 1);
}
function part2(input: string[]) {
let circ = 0;
const coords = input.reduce<Coords[]>(
(acc, line) => {
const code = line.split(' ')[2];
const c = code.substring(2, 7);
const d = code[7];
const cnt = Number.parseInt(c, 16);
circ += Math.abs(cnt);
const [vert, hor] = acc[acc.length - 1];
switch (d) {
case '0': // right ++
acc.push([vert, hor + cnt]);
break;
case '2': // left --
acc.push([vert, hor - cnt]);
break;
case '3': // up --
acc.push([vert - cnt, hor]);
break;
case '1': // down ++
acc.push([vert + cnt, hor]);
break;
}
return acc;
},
[[0, 0]],
);
let surface = 0;
for (let i = 1; i < coords.length; i++) {
const [x1, y1] = coords[i - 1];
const [x2, y2] = coords[i];
surface += (x1 * y2 - x2 * y1) / 2;
}
log({ circ, surface });
log(Math.abs(surface) + circ / 2 + 1);
}
// part1(test_input);
// part1(puzzle_input);
part2(test_input);
part2(puzzle_input);