Skip to content

Commit acea1dc

Browse files
committed
wow I would have been a full 10 ranks higher if I had not made that mistake on part 2. anyway, can't complain because I leaderboarded on both parts even if it took me a while to debug a bunch of stuff
1 parent 69b1cf5 commit acea1dc

File tree

8 files changed

+234
-3
lines changed

8 files changed

+234
-3
lines changed

2021/solutions/day15/day15.3.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const practice = `
2929
// input = practice;
3030
input = input.trim();
3131

32+
// @ts-expect-error
3233
import * as as from "./astar.js";
3334

3435
const graph = new as.Graph(input.split("\n").map(line => line.split("").map(Number)));
@@ -37,4 +38,4 @@ const start = graph.grid[0][0];
3738
const end = graph.grid[graph.grid.length - 1][graph.grid[0].length - 1];
3839
const result = as.astar.search(graph, start, end);
3940

40-
console.log(result.reduce((t, w) => t + w.weight, 0)); // expect=619
41+
console.log(result.reduce((t: any, w: any) => t + w.weight, 0)); // expect=619

2021/solutions/day16/day16.1.ts

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
A0016C880162017C3686B18A3D4780
19+
`;
20+
// input = practice;
21+
input = input.trim();
22+
23+
const hex2bin = (hex: string) => {
24+
return [...hex].map(c => parseInt(c, 16).toString(2).padStart(4, "0")).join("");
25+
};
26+
27+
let data = [...hex2bin(input).dwth(log)];
28+
29+
function parse(): T {
30+
const version = data.splice(0, 3).join("").use(v => parseInt(v, 2));
31+
const typeid = data.splice(0, 3).join("").use(v => parseInt(v, 2));
32+
console.log(version, typeid);
33+
34+
if(typeid === 4) {
35+
let bins = "";
36+
while(true) {
37+
const bit = data.shift();
38+
bins += data.splice(0, 4).join("");
39+
if(bit !== "1") break;
40+
}
41+
// return [parseInt(bins, 2)];
42+
return [version];
43+
}
44+
45+
const lentid = data.splice(0, 1).join("").use(v => parseInt(v, 2));
46+
47+
const res: T = [];
48+
if(lentid === 0) {
49+
const length = data.splice(0, 15).join("").use(v => parseInt(v, 2));
50+
51+
const resv = data.length - length;
52+
while(data.length > resv) {
53+
res.push(parse());
54+
}
55+
return [version, ...res];
56+
}else{
57+
const iterc = data.splice(0, 11).join("").use(v => parseInt(v, 2));
58+
59+
for(let i = 0; i < iterc; i++) {
60+
res.push(parse());
61+
}
62+
}
63+
return [version, ...res];
64+
}
65+
66+
parse().dwth(log).flat(Infinity).reduce((t, a) => t + a, 0).dwth(log);
67+
68+
// added after completion
69+
// i just didn't specify a return type for parse while solving
70+
// also tbh i should have done flat in the code instead of using .flat() because
71+
// it took me too long to figure out you had to .flat(Infinity) (I thought flat did
72+
// that by defualt)
73+
type T = (number | T)[];

2021/solutions/day16/day16.2.ts

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
880086C3E88112
19+
`;
20+
// input = practice;
21+
input = input.trim();
22+
23+
const hex2bin = (hex: string) => {
24+
return [...hex].map(c => parseInt(c, 16).toString(2).padStart(4, "0")).join("");
25+
};
26+
27+
let data = [...hex2bin(input).dwth(log)];
28+
29+
function parse() {
30+
const version = data.splice(0, 3).join("").use(v => parseInt(v, 2));
31+
const typeid = data.splice(0, 3).join("").use(v => parseInt(v, 2));
32+
33+
if(typeid === 4) {
34+
let bins = "";
35+
while(true) {
36+
const bit = data.shift();
37+
bins += data.splice(0, 4).join("");
38+
if(bit !== "1") break;
39+
}
40+
return parseInt(bins, 2);
41+
// return [version];
42+
}
43+
44+
const lentid = data.splice(0, 1).join("").use(v => parseInt(v, 2));
45+
46+
const res: number[] = [];
47+
if(lentid === 0) {
48+
const length = data.splice(0, 15).join("").use(v => parseInt(v, 2));
49+
50+
const resv = data.length - length;
51+
while(data.length > resv) {
52+
res.push(parse());
53+
}
54+
}else{
55+
const iterc = data.splice(0, 11).join("").use(v => parseInt(v, 2));
56+
57+
for(let i = 0; i < iterc; i++) {
58+
res.push(parse());
59+
}
60+
}
61+
console.log(res, typeid);
62+
63+
if(typeid === 0) {
64+
return res.reduce((t, a) => t + a, 0);
65+
}else if(typeid === 1) {
66+
return res.reduce((t, a) => t * a, 1);
67+
}else if(typeid === 2) {
68+
return res.reduce((t, a) => Math.min(t, a), Infinity);
69+
}else if(typeid === 3) {
70+
return res.reduce((t, a) => Math.max(t, a), -Infinity);
71+
}else if(typeid === 5) {
72+
return res[0] > res[1] ? 1 : 0;
73+
}else if(typeid === 6) {
74+
return res[0] < res[1] ? 1 : 0;
75+
}else if(typeid === 7) {
76+
return res[0] === res[1] ? 1 : 0;
77+
}else throw new Error("bad")
78+
}
79+
80+
parse().dwth(log);

2021/solutions/day16/day16.3.ts

+25
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/day16/day16.4.ts

+25
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/day16/day16.5.ts

+25
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/day16/day16.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
E20D41802B2984BD00540010F82D09E35880350D61A41D3004E5611E585F40159ED7AD7C90CF6BD6BE49C802DEB00525272CC1927752698693DA7C70029C0081002140096028C5400F6023C9C00D601ED88070070030005C2201448400E400F40400C400A50801E20004C1000809D14700B67676EE661137ADC64FF2BBAD745B3F2D69026335E92A0053533D78932A9DFE23AC7858C028920A973785338832CFA200F47C81D2BBBC7F9A9E1802FE00ACBA44F4D1E775DDC19C8054D93B7E72DBE7006AA200C41A8510980010D8731720CB80132918319804738AB3A8D3E773C4A4015A498E680292B1852E753E2B29D97F0DE6008CB3D4D031802D2853400D24DEAE0137AB8210051D24EB600844B95C56781B3004F002B99D8F635379EDE273AF26972D4A5610BA51004C12D1E25D802F32313239377B37100105343327E8031802B801AA00021D07231C2F10076184668693AC6600BCD83E8025231D752E5ADE311008A4EA092754596C6789727F069F99A4645008247D2579388DCF53558AE4B76B257200AAB80107947E94789FE76E36402868803F0D62743F00043A1646288800084C3F8971308032996A2BD8023292DF8BE467BB3790047F2572EF004A699E6164C013A007C62848DE91CC6DB459B6B40087E530AB31EE633BD23180393CBF36333038E011CBCE73C6FB098F4956112C98864EA1C2801D2D0F319802D60088002190620E479100622E4358952D84510074C0188CF0923410021F1CE1146E3006E3FC578EE600A4B6C4B002449C97E92449C97E92459796EB4FF874400A9A16100A26CEA6D0E5E5EC8841C9B8FE37109C99818023A00A4FD8BA531586BB8B1DC9AE080293B6972B7FA444285CC00AE492BC910C1697B5BDD8425409700562F471201186C0120004322B42489A200D4138A71AA796D00374978FE07B2314E99BFB6E909678A0

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66

77
### 2021
88

9-
- Total Score: 53
10-
- Times On Leaderboard: 3 / 26 (~12%)
9+
- Total Score: 87
10+
- Times On Leaderboard: 5 / 28 (~18%)
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+
| 16 | 00:20:19 | 88 | 13 | 00:26:17 | 80 | 21 |
1718
| 15 | 00:21:39 | 1276 | | 00:28:25 | 515 | |
1819
| 14 | 00:08:38 | 569 | | 00:23:32 | 564 | |
1920
| 13 | 00:14:31 | 946 | | 00:16:11 | 504 | |

0 commit comments

Comments
 (0)