Skip to content

Commit 361e9c5

Browse files
committed
"H occurs 191 times" "least common element (H, 161)" excuse me you can't just have a typo on the challenge statement
anyway - opened the problem. "oh this needs python, it's literally zip(). but for some reason it took a while to convince myself to use .op(), the function i added to this environment that is literally python zip(). - asked copilot to write a mostCommon() function for me even though it's the easiest thing to write and it probably took me longer trying to prompt copilot than it would have for me to have written it myself and have sorted the output properly. I guess I have a video so I can time it and see - did spend a little bit of time questioning myself because of the typo in the challenge statement but that was just part 2 and not that long - anyway basically just overall slow and checked my work at every level - also took 51 lines of code while others got it in like 30. 51 lines takes longer to write - idk i can check my video and make a pie chart or something - it really bothers me that when i go to type .use() in my code vscode will "correct" me to something wrong
1 parent 209094e commit 361e9c5

File tree

7 files changed

+379
-1
lines changed

7 files changed

+379
-1
lines changed

2021/solutions/day14/day14.1.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
NNCB
19+
20+
CH -> B
21+
HH -> N
22+
CB -> H
23+
NH -> C
24+
HB -> C
25+
HC -> B
26+
HN -> C
27+
NN -> C
28+
BH -> H
29+
NC -> B
30+
NB -> B
31+
BN -> B
32+
BB -> N
33+
BC -> B
34+
CC -> N
35+
CN -> C
36+
37+
`;
38+
// input = practice;
39+
input = input.trim();
40+
41+
const [template, insertionrules] = input.split("\n\n");
42+
43+
const ruleso = insertionrules.split("\n").map(r => r.split(" -> "));
44+
const rules = new Map<string, string>();
45+
46+
ruleso.forEach(([lhs, rhs]) => rules.set(lhs, rhs));
47+
48+
let templ = template;
49+
50+
const paircounts = new Map<string, number>();
51+
52+
[...templ].op([...templ].slice(1), (a, b) => {
53+
if(!b) return;
54+
paircounts.set(a + b, (paircounts.get(a + b) ?? 0) + 1);
55+
}).join("")
56+
57+
console.log(templ);
58+
for(let i = 0; i < 10; i++) {
59+
// zip(a, a[1..])
60+
61+
templ = [...templ].op([...templ].slice(1), (a, b) => {
62+
const match = rules.get(a + b);
63+
if(match) return a + match;
64+
return a;
65+
}).join("");
66+
67+
// console.log(i + 1, templ);
68+
}
69+
70+
const arr = [...templ];
71+
72+
// most common element in arer
73+
74+
function mostCommon(arr: string[]) {
75+
const counts = arr.reduce((acc, cur) => {
76+
acc[cur] = (acc[cur] || 0) + 1;
77+
return acc;
78+
}, {} as {[key: string]: number});
79+
80+
const max = Math.max(...Object.values(counts));
81+
const min = Math.min(...Object.values(counts));
82+
const mostCommon = Object.keys(counts).filter(k => counts[k] === max || counts[k] === min);
83+
return [counts[mostCommon[0]], counts[mostCommon[1]]].sort((a, b) => a - b);
84+
}
85+
86+
87+
console.log(mostCommon(arr).use(([a, b]) => b - a));

2021/solutions/day14/day14.2.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
NNCB
19+
20+
CH -> B
21+
HH -> N
22+
CB -> H
23+
NH -> C
24+
HB -> C
25+
HC -> B
26+
HN -> C
27+
NN -> C
28+
BH -> H
29+
NC -> B
30+
NB -> B
31+
BN -> B
32+
BB -> N
33+
BC -> B
34+
CC -> N
35+
CN -> C
36+
37+
`;
38+
// input = practice;
39+
input = input.trim();
40+
41+
const [template, insertionrules] = input.split("\n\n");
42+
43+
const ruleso = insertionrules.split("\n").map(r => r.split(" -> "));
44+
const rules = new Map<string, string>();
45+
46+
ruleso.forEach(([lhs, rhs]) => rules.set(lhs, rhs));
47+
48+
let templ = template;
49+
50+
let paircounts = new Map<string, number>();
51+
52+
[...templ].op([...templ].slice(1), (a, b) => {
53+
if(!b) {
54+
paircounts.set(a + "!", (paircounts.get(a + "!") ?? 0) + 1);
55+
return;
56+
}
57+
paircounts.set(a + b, (paircounts.get(a + b) ?? 0) + 1);
58+
}).join("")
59+
60+
console.log(templ, paircounts);
61+
62+
/*
63+
Map(6) {
64+
'NC' => 1,
65+
'CN' => 2, // ?
66+
'NB' => 1,
67+
'BC' => 2,
68+
'CH' => 1,
69+
'HB' => 2
70+
}
71+
NNCB
72+
1 NCNBCHB
73+
74+
NC
75+
CN
76+
NB
77+
BC
78+
CH
79+
HB
80+
*/
81+
82+
for(let i = 0; i < 40; i++) {
83+
84+
let npc = new Map<string, number>();
85+
for(const [key, value] of paircounts.entries()) {
86+
const match = rules.get(key);
87+
if(match) {
88+
const k = key[0] + match;
89+
npc.set(k, (npc.get(k) ?? 0) + value);
90+
const l = match + key[1];
91+
npc.set(l, (npc.get(l) ?? 0) + value);
92+
}else{
93+
const k = key;
94+
npc.set(k, (npc.get(k) ?? 0) + value);
95+
}
96+
}
97+
paircounts = npc;
98+
99+
}
100+
101+
console.log(paircounts);
102+
103+
let occurances = new Map<string, number>();
104+
for(const [key, value] of paircounts.entries()) {
105+
occurances.set(key[0], (occurances.get(key[0]) ?? 0) + value);
106+
}
107+
console.log(occurances);
108+
console.log([...occurances.entries()].map(([k, v]) => v).sort((a, b) => a - b).use((
109+
a
110+
) => {
111+
return a.at(-1) - a.at(0);
112+
}));
113+

2021/solutions/day14/day14.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/day14/day14.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/day14/day14.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/day14/day14.txt

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
PSVVKKCNBPNBBHNSFKBO
2+
3+
CF -> H
4+
PP -> H
5+
SP -> V
6+
NO -> C
7+
SF -> F
8+
FS -> H
9+
OF -> P
10+
PN -> B
11+
SH -> V
12+
BO -> K
13+
ON -> V
14+
VP -> S
15+
HN -> B
16+
PS -> P
17+
FV -> H
18+
NC -> N
19+
FN -> S
20+
PF -> F
21+
BF -> F
22+
NB -> O
23+
HS -> C
24+
SC -> V
25+
PC -> K
26+
KF -> K
27+
HC -> C
28+
OK -> H
29+
KS -> P
30+
VF -> C
31+
NV -> S
32+
KK -> F
33+
HV -> H
34+
SV -> V
35+
KC -> N
36+
HF -> P
37+
SN -> F
38+
VS -> P
39+
VN -> F
40+
VH -> C
41+
OB -> K
42+
VV -> O
43+
VC -> O
44+
KP -> V
45+
OP -> C
46+
HO -> S
47+
NP -> K
48+
HB -> C
49+
CS -> S
50+
OO -> S
51+
CV -> K
52+
BS -> F
53+
BH -> P
54+
HP -> P
55+
PK -> B
56+
BB -> H
57+
PV -> N
58+
VO -> P
59+
SS -> B
60+
CC -> F
61+
BC -> V
62+
FF -> S
63+
HK -> V
64+
OH -> N
65+
BV -> C
66+
CP -> F
67+
KN -> K
68+
NN -> S
69+
FB -> F
70+
PH -> O
71+
FH -> N
72+
FK -> P
73+
CK -> V
74+
CN -> S
75+
BP -> K
76+
CH -> F
77+
FP -> K
78+
HH -> N
79+
NF -> C
80+
VB -> B
81+
FO -> N
82+
PB -> C
83+
KH -> K
84+
PO -> K
85+
OV -> F
86+
NH -> H
87+
KV -> B
88+
OS -> K
89+
OC -> K
90+
FC -> H
91+
SO -> H
92+
KO -> P
93+
NS -> F
94+
CB -> C
95+
CO -> F
96+
KB -> V
97+
BK -> K
98+
NK -> O
99+
SK -> C
100+
SB -> B
101+
VK -> O
102+
BN -> H

0 commit comments

Comments
 (0)