Skip to content

Commit 9d5030a

Browse files
committed
Avoid using enums
1 parent a94450f commit 9d5030a

File tree

3 files changed

+74
-67
lines changed

3 files changed

+74
-67
lines changed

typescript/euler/bin/p0011.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ const grid = [
2626
[ 1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
2727
];
2828

29-
enum Dir {
30-
Right,
31-
Down,
32-
UpperRight,
33-
DownRight,
34-
}
29+
const DIRS = {
30+
Right: 0,
31+
Down: 1,
32+
UpperRight: 2,
33+
DownRight: 3,
34+
} as const;
35+
36+
type Dir = typeof DIRS[keyof typeof DIRS];
3537

3638
const QTY = 4;
3739
const ROW_SIZE = grid.length;
@@ -41,16 +43,16 @@ const product = (row: number, col: number, dir: Dir) => {
4143
let acc = 1;
4244
for (const i of range(0, QTY)) {
4345
switch (dir) {
44-
case Dir.Right:
46+
case DIRS.Right:
4547
acc *= grid[row][col + i];
4648
break;
47-
case Dir.Down:
49+
case DIRS.Down:
4850
acc *= grid[row + i][col];
4951
break;
50-
case Dir.UpperRight:
52+
case DIRS.UpperRight:
5153
acc *= grid[row - i][col + i];
5254
break;
53-
case Dir.DownRight:
55+
case DIRS.DownRight:
5456
acc *= grid[row + i][col + i];
5557
break;
5658
}
@@ -64,15 +66,15 @@ export const compute = (): string => {
6466
for (const row of range(0, ROW_SIZE)) {
6567
for (const col of range(0, COL_SIZE)) {
6668
if (row <= ROW_SIZE - QTY) {
67-
result = Math.max(result, product(row, col, Dir.Down));
69+
result = Math.max(result, product(row, col, DIRS.Down));
6870
}
6971
if (col <= COL_SIZE - QTY) {
70-
result = Math.max(result, product(row, col, Dir.Right));
72+
result = Math.max(result, product(row, col, DIRS.Right));
7173
if (row >= QTY - 1) {
72-
result = Math.max(result, product(row, col, Dir.UpperRight));
74+
result = Math.max(result, product(row, col, DIRS.UpperRight));
7375
}
7476
if (row <= ROW_SIZE - QTY) {
75-
result = Math.max(result, product(row, col, Dir.DownRight));
77+
result = Math.max(result, product(row, col, DIRS.DownRight));
7678
}
7779
}
7880
}

typescript/euler/bin/p0054.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ const getHandRank = (hand: string[]): number[] => {
5858

5959
const getHand = (suitLst: string[], rankLst: number[]): number[] => {
6060
// deno-fmt-ignore
61-
enum Hand {
62-
HC = 0, OP, TP, TK, S, F, FH, FK, SF, RF
63-
}
61+
const HANDS = {
62+
HC: 0, OP: 1, TP: 2, TK: 3, S: 4, F: 5, FH: 6, FK: 7, SF: 8, RF: 9
63+
} as const;
6464

6565
const cmpDetail = (a: [string, number], b: [string, number]): number => {
6666
const tmp = b[1] - a[1];
@@ -85,34 +85,34 @@ const getHandRank = (hand: string[]): number[] => {
8585
if (isStraight(rankLst)) {
8686
switch (rankLst[0]) {
8787
case 14:
88-
return [Hand.RF, ...getDetail(handInfo)];
88+
return [HANDS.RF, ...getDetail(handInfo)];
8989
default:
90-
return [Hand.SF, ...getDetail(handInfo)];
90+
return [HANDS.SF, ...getDetail(handInfo)];
9191
}
9292
} else {
93-
return [Hand.F, ...getDetail(handInfo)];
93+
return [HANDS.F, ...getDetail(handInfo)];
9494
}
9595
} else {
9696
switch (handInfo.length) {
9797
case 5:
9898
if (isStraight(rankLst)) {
99-
return [Hand.S, ...getDetail(handInfo)];
99+
return [HANDS.S, ...getDetail(handInfo)];
100100
} else {
101-
return [Hand.HC, ...getDetail(handInfo)];
101+
return [HANDS.HC, ...getDetail(handInfo)];
102102
}
103103
case 4:
104-
return [Hand.OP, ...getDetail(handInfo)];
104+
return [HANDS.OP, ...getDetail(handInfo)];
105105
case 3:
106106
if (handInfo[0][1] === 3) {
107-
return [Hand.TK, ...getDetail(handInfo)];
107+
return [HANDS.TK, ...getDetail(handInfo)];
108108
} else {
109-
return [Hand.TP, ...getDetail(handInfo)];
109+
return [HANDS.TP, ...getDetail(handInfo)];
110110
}
111111
case 2:
112112
if (handInfo[0][1] === 4) {
113-
return [Hand.FK, ...getDetail(handInfo)];
113+
return [HANDS.FK, ...getDetail(handInfo)];
114114
} else {
115-
return [Hand.FH, ...getDetail(handInfo)];
115+
return [HANDS.FH, ...getDetail(handInfo)];
116116
}
117117
default:
118118
throw new Error("not reached");

typescript/euler/bin/p0084.ts

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,29 @@
77
import { unzip, zip } from "@std/collections";
88
import { Counter, range } from "../lib/util.ts";
99

10+
// Don't use `as const` this object here because I need to treat these
11+
// values as number. Normally, we would define a type with readonly for
12+
// all properties. However, there were many properties and I was lazy and
13+
// cut corners.
14+
//
1015
// deno-fmt-ignore
11-
enum Square {
12-
GO = 0, A1 = 1, CC1 = 2, A2 = 3, T1 = 4,
13-
R1 = 5, B1 = 6, CH1 = 7, B2 = 8, B3 = 9,
14-
JAIL = 10, C1 = 11, U1 = 12, C2 = 13, C3 = 14,
15-
R2 = 15, D1 = 16, CC2 = 17, D2 = 18, D3 = 19,
16-
FP = 20, E1 = 21, CH2 = 22, E2 = 23, E3 = 24,
17-
R3 = 25, F1 = 26, F2 = 27, U2 = 28, F3 = 29,
18-
G2J = 30, G1 = 31, G2 = 32, CC3 = 33, G3 = 34,
19-
R4 = 35, CH3 = 36, H1 = 37, T2 = 38, H2 = 39,
20-
}
16+
const SQUARE = {
17+
GO: 0, A1: 1, CC1: 2, A2: 3, T1: 4,
18+
R1: 5, B1: 6, CH1: 7, B2: 8, B3: 9,
19+
JAIL: 10, C1: 11, U1: 12, C2: 13, C3: 14,
20+
R2: 15, D1: 16, CC2: 17, D2: 18, D3: 19,
21+
FP: 20, E1: 21, CH2: 22, E2: 23, E3: 24,
22+
R3: 25, F1: 26, F2: 27, U2: 28, F3: 29,
23+
G2J: 30, G1: 31, G2: 32, CC3: 33, G3: 34,
24+
R4: 35, CH3: 36, H1: 37, T2: 38, H2: 39,
25+
};
2126

2227
const communityChest = (sq: number): number => {
2328
switch (Math.trunc(Math.random() * 16)) {
2429
case 0:
25-
return Square.GO;
30+
return SQUARE.GO;
2631
case 1:
27-
return Square.JAIL;
32+
return SQUARE.JAIL;
2833
default:
2934
return sq;
3035
}
@@ -33,43 +38,43 @@ const communityChest = (sq: number): number => {
3338
const chanceCard = (sq: number): number => {
3439
const nextR = (sq: number): number => {
3540
switch (sq) {
36-
case Square.CH1:
37-
return Square.R2;
38-
case Square.CH2:
39-
return Square.R3;
40-
case Square.CH3:
41-
return Square.R1;
41+
case SQUARE.CH1:
42+
return SQUARE.R2;
43+
case SQUARE.CH2:
44+
return SQUARE.R3;
45+
case SQUARE.CH3:
46+
return SQUARE.R1;
4247
default:
4348
throw new RangeError(`invalid square: ${sq}`);
4449
}
4550
};
4651

4752
const nextU = (sq: number): number => {
4853
switch (sq) {
49-
case Square.CH1:
50-
return Square.U1;
51-
case Square.CH2:
52-
return Square.U2;
53-
case Square.CH3:
54-
return Square.U1;
54+
case SQUARE.CH1:
55+
return SQUARE.U1;
56+
case SQUARE.CH2:
57+
return SQUARE.U2;
58+
case SQUARE.CH3:
59+
return SQUARE.U1;
5560
default:
5661
throw new RangeError(`invalid square: ${sq}`);
5762
}
5863
};
5964

6065
switch (Math.trunc(Math.random() * 16)) {
6166
case 0:
62-
return Square.GO;
67+
return SQUARE.GO;
6368
case 1:
64-
return Square.JAIL;
69+
return SQUARE.JAIL;
6570
case 2:
66-
return Square.C1;
71+
return SQUARE.C1;
6772
case 3:
68-
return Square.E3;
73+
return SQUARE.E3;
6974
case 4:
70-
return Square.H2;
75+
return SQUARE.H2;
7176
case 5:
72-
return Square.R1;
77+
return SQUARE.R1;
7378
case 6:
7479
case 7:
7580
return nextR(sq);
@@ -86,30 +91,30 @@ const chanceCard = (sq: number): number => {
8691

8792
const monteCarlo = (dice: () => number, loopCnt: number): string => {
8893
const counter: number[] = new Array(40).fill(0);
89-
let sq = Square.GO;
94+
let sq = SQUARE.GO;
9095
let double = 0;
9196

9297
for (const _ of range(0, loopCnt)) {
9398
const d1 = dice();
9499
const d2 = dice();
95100
double = (d1 !== d2) ? 0 : double + 1;
96101
if (double >= 3) {
97-
sq = Square.JAIL;
102+
sq = SQUARE.JAIL;
98103
double = 0;
99104
} else {
100105
sq = (sq + d1 + d2) % 40;
101106
switch (sq) {
102-
case Square.G2J:
103-
sq = Square.JAIL;
107+
case SQUARE.G2J:
108+
sq = SQUARE.JAIL;
104109
break;
105-
case Square.CC1:
106-
case Square.CC2:
107-
case Square.CC3:
110+
case SQUARE.CC1:
111+
case SQUARE.CC2:
112+
case SQUARE.CC3:
108113
sq = communityChest(sq);
109114
break;
110-
case Square.CH1:
111-
case Square.CH2:
112-
case Square.CH3:
115+
case SQUARE.CH1:
116+
case SQUARE.CH2:
117+
case SQUARE.CH3:
113118
sq = chanceCard(sq);
114119
break;
115120
}

0 commit comments

Comments
 (0)