Skip to content

Commit a298c71

Browse files
committed
damn. 103/100. if I had just ignored the typescript error on .as and used it, I would have saved like a second or so which would have been enough to make it on the leaderboard for part 1
wow also I've enabled es2021 now so I can use .at and ts won't complain …or not oh maybe I have to update ts. will do that in the next commit
1 parent b722ab6 commit a298c71

File tree

8 files changed

+257
-3
lines changed

8 files changed

+257
-3
lines changed

2021/solutions/day12/day12.1.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {fhmain} from "../../../src/fheader";
2+
fhmain(__filename);
3+
/*
4+
input: string, lines: string[], dblines: string[][]
5+
copy(text: string) → clipboard
6+
error(message: string) → thrown error
7+
-5..mod(3) → @mod(-5, 3)
8+
*/
9+
10+
// Cardinals:
11+
// [[1,0],[-1,0],[0,1],[0,-1]]
12+
// +Diagonals:
13+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
14+
15+
export {};
16+
17+
const practice = `
18+
dc-end
19+
HN-start
20+
start-kj
21+
dc-start
22+
dc-HN
23+
LN-dc
24+
HN-end
25+
kj-sa
26+
kj-HN
27+
kj-dc
28+
`;
29+
// input = practice;
30+
input = input.trim();
31+
32+
const system: {[key: string]: string[]} = {
33+
34+
};
35+
36+
input.split("\n").forEach(line => {
37+
const [left, right] = line.split("-");
38+
(system[left] ??= []).push(right);
39+
(system[right] ??= []).push(left);
40+
});
41+
42+
const unique_paths = new Set<string>();
43+
44+
function isUppercase(a: string): boolean {
45+
return a.toUpperCase() === a;
46+
}
47+
48+
// visit small caves at most once, and can visit big caves any number of times
49+
function findPath(begin: string[]): string[] {
50+
if(begin[begin.length - 1] === "end") {
51+
return [begin.join(",")];
52+
}
53+
54+
let choices = system[begin[begin.length-1]];
55+
56+
choices = choices.filter(c => {
57+
if(isUppercase(c)) return true;
58+
return !begin.includes(c);
59+
});
60+
61+
return choices.flatMap((c) => findPath([...begin, c]));
62+
}
63+
64+
console.log(findPath(["start"]).length); // expect=4338
65+
66+
// input.
67+
68+
// hi! i'm glad you're excited to code
69+
// but consider fully reading the problem statement first.
70+
// Sincerely, future you.

2021/solutions/day12/day12.2.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import {fhmain} from "../../../src/fheader";
2+
fhmain(__filename);
3+
/*
4+
input: string, lines: string[], dblines: string[][]
5+
copy(text: string) → clipboard
6+
error(message: string) → thrown error
7+
-5..mod(3) → @mod(-5, 3)
8+
*/
9+
10+
// Cardinals:
11+
// [[1,0],[-1,0],[0,1],[0,-1]]
12+
// +Diagonals:
13+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
14+
15+
export {};
16+
17+
const practice = `
18+
dc-end
19+
HN-start
20+
start-kj
21+
dc-start
22+
dc-HN
23+
LN-dc
24+
HN-end
25+
kj-sa
26+
kj-HN
27+
kj-dc
28+
`;
29+
// input = practice;
30+
input = input.trim();
31+
32+
const system: {[key: string]: string[]} = {
33+
34+
};
35+
36+
input.split("\n").forEach(line => {
37+
const [left, right] = line.split("-");
38+
(system[left] ??= []).push(right);
39+
(system[right] ??= []).push(left);
40+
});
41+
42+
const unique_paths = new Set<string>();
43+
44+
function isUppercase(a: string): boolean {
45+
return a.toUpperCase() === a;
46+
}
47+
48+
// visit small caves at most once, and can visit big caves any number of times
49+
function findPath(begin: string[]): string[] {
50+
if(begin[begin.length - 1] === "end") {
51+
return [begin.join(",")];
52+
}
53+
54+
let choices = system[begin[begin.length-1]];
55+
56+
let has = new Set<string>();
57+
let allowv = 2;
58+
begin.some(v => {
59+
if(isUppercase(v)) return false;
60+
if(has.has(v)) {
61+
allowv = 1;
62+
return true;
63+
}
64+
has.add(v);
65+
return false;
66+
});
67+
// console.log(begin, allowv);
68+
69+
choices = choices.filter(c => {
70+
if(isUppercase(c)) return true;
71+
if(c === "start") return false;
72+
// check if begin has any item twice
73+
return begin.filter(v => v === c).length < allowv;
74+
});
75+
76+
return choices.flatMap((c) => findPath([...begin, c]));
77+
}
78+
79+
findPath(["start"]).dwth(log).dwth(v => log(v.length)); // expect=114189
80+
81+
// input.
82+
83+
// hi! i'm glad you're excited to code
84+
// but consider fully reading the problem statement first.
85+
// Sincerely, future you.

2021/solutions/day12/day12.3.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {fhmain} from "../../../src/fheader";
2+
fhmain(__filename);
3+
/*
4+
input: string, lines: string[], dblines: string[][]
5+
copy(text: string) → clipboard
6+
error(message: string) → thrown error
7+
-5..mod(3) → @mod(-5, 3)
8+
*/
9+
10+
// Cardinals:
11+
// [[1,0],[-1,0],[0,1],[0,-1]]
12+
// +Diagonals:
13+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
14+
15+
export {};
16+
17+
const practice = ``;
18+
// input = practice;
19+
input = input.trim();
20+
21+
// input.
22+
23+
// hi! i'm glad you're excited to code
24+
// but consider fully reading the problem statement first.
25+
// Sincerely, future you.

2021/solutions/day12/day12.4.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {fhmain} from "../../../src/fheader";
2+
fhmain(__filename);
3+
/*
4+
input: string, lines: string[], dblines: string[][]
5+
copy(text: string) → clipboard
6+
error(message: string) → thrown error
7+
-5..mod(3) → @mod(-5, 3)
8+
*/
9+
10+
// Cardinals:
11+
// [[1,0],[-1,0],[0,1],[0,-1]]
12+
// +Diagonals:
13+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
14+
15+
export {};
16+
17+
const practice = ``;
18+
// input = practice;
19+
input = input.trim();
20+
21+
// input.
22+
23+
// hi! i'm glad you're excited to code
24+
// but consider fully reading the problem statement first.
25+
// Sincerely, future you.

2021/solutions/day12/day12.5.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {fhmain} from "../../../src/fheader";
2+
fhmain(__filename);
3+
/*
4+
input: string, lines: string[], dblines: string[][]
5+
copy(text: string) → clipboard
6+
error(message: string) → thrown error
7+
-5..mod(3) → @mod(-5, 3)
8+
*/
9+
10+
// Cardinals:
11+
// [[1,0],[-1,0],[0,1],[0,-1]]
12+
// +Diagonals:
13+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
14+
15+
export {};
16+
17+
const practice = ``;
18+
// input = practice;
19+
input = input.trim();
20+
21+
// input.
22+
23+
// hi! i'm glad you're excited to code
24+
// but consider fully reading the problem statement first.
25+
// Sincerely, future you.

2021/solutions/day12/day12.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
nu-start
2+
rt-start
3+
db-qh
4+
PE-end
5+
sl-rt
6+
qh-end
7+
ZH-rt
8+
nu-rt
9+
PE-db
10+
db-sl
11+
nu-ZH
12+
nu-qh
13+
PE-qh
14+
ZH-db
15+
ne-end
16+
ne-ZH
17+
QG-db
18+
qh-sl
19+
ZH-qh
20+
start-ZH
21+
nu-PE
22+
uf-db
23+
ne-sl

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66

77
### 2021
88

9-
- Total Score: 52
10-
- Times On Leaderboard: 2 / 18 (~11%)
9+
- Total Score: 53
10+
- Times On Leaderboard: 3 / 20 (~15%)
1111
- Best Leaderboard Position: 62ⁿᵈ place
1212
- Worst Leaderboard Position: 1688ᵗʰ place
1313
- Best Day was Day 2: 39 points, #178/#62
1414

1515
| Day | Time | Rank | Score | Time | Rank | Score |
1616
| --: | -------: | ---: | ----: | -------: | ---: | ----: |
17+
| 12 | 00:06:39 | 103 | | 00:12:40 | 100 | 1 |
1718
| 11 | 00:15:41 | 755 | | 00:17:44 | 682 | |
1819
| 10 | 00:04:42 | 192 | | 00:10:38 | 287 | |
1920
| 9 | 00:03:33 | 88 | 13 | 00:28:33 | 1688 | |

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"strict": true,
4-
"lib": ["es2019"],
4+
"lib": ["es2021"],
55
"target": "esnext"
66
}
77
}

0 commit comments

Comments
 (0)