-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
154 lines (129 loc) · 3.1 KB
/
index.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
147
148
149
150
151
152
153
154
import run from "aocrunner";
const parseInput = (rawInput: string) => {
class TrailMap {
map: number[][]
trailheads: number[][]
constructor(rawInput: string) {
this.map = rawInput.split("\n").map(row => row.trim().split("").map(col => parseInt(col)))
this.trailheads = this.map.reduce((acc, row, y) => {
const trailheads = row
.map((char, x) => {
if (char === 0) {
return [x, y]
} else {
return []
}
})
.filter(x => x.length > 0)
return [...acc, ...trailheads]
}, [] as number[][])
}
getMapBoundary() {
const maxY = this.map.length - 1
const maxX = this.map[0].length - 1
return [maxX, maxY]
}
getSurrounding(position: number[]) {
const [x, y] = position
const t = {
val: this.map[y - 1]?.[x],
x,
y: y - 1
}
const r = {
val: this.map[y]?.[x + 1],
x: x + 1,
y
}
const b = {
val: this.map[y + 1]?.[x],
x,
y: y + 1
}
const l = {
val: this.map[y]?.[x - 1],
x: x - 1,
y
}
return [t, r, b, l].filter(({ val }) => val !== undefined)
}
walkTrail(start: { y: number, x: number }): Set<string> {
const endPositions = new Set<string>();
const visited = new Set<string>();
const queue: { y: number, x: number, currentStep: number }[] = [
{ ...start, currentStep: 0 }
];
while (queue.length > 0) {
const { y, x, currentStep } = queue.shift()!;
const posKey = `${y},${x}`;
if (visited.has(posKey)) {
continue
} else {
visited.add(posKey);
}
if (this.map[y][x] === 9) {
endPositions.add(posKey);
}
const availableSteps = this.getSurrounding([x, y]);
for (const step of availableSteps) {
const nextStep = this.map[step.y][step.x];
if (nextStep === currentStep + 1) {
queue.push({
y: step.y,
x: step.x,
currentStep: nextStep
});
}
}
}
return endPositions;
}
}
return new TrailMap(rawInput)
};
const part1 = (rawInput: string) => {
const rawInput2 = `89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732`
const map = parseInput(rawInput);
const scores = map.trailheads.map(trailhead => {
const endPositions = map.walkTrail({ x: trailhead[0], y: trailhead[1] });
return endPositions.size;
});
console.log({ scores })
const total = scores.reduce((total, curr) => {
return total += curr
}, 0)
return total;
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput);
return;
};
run({
part1: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part1,
},
part2: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});