-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFizzBuzz.ts
More file actions
82 lines (66 loc) · 2.56 KB
/
FizzBuzz.ts
File metadata and controls
82 lines (66 loc) · 2.56 KB
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
export type Condition = (n:number) => boolean;
export type Operation = (currentBuzzwords:string[], i:number) => string[];
export class Rule{
condition: Condition;
operation: Operation;
description:string;
constructor(newCond: Condition, newOp: Operation, description: string =""){
this.condition = newCond;
this.operation = newOp;
this.description = description;
}
applyRule(data: string[], value: number): string[]{
let data2 = JSON.parse(JSON.stringify(data));
if(this.condition(value)){
data2 = this.operation(data, value)
}
return data2;
}
followedBy(rule: Rule):Rule{
return new Rule((i) => true, (current:string[], i:number) =>{
return rule.applyRule( this.applyRule(current, i), i);
}, this.description + ", then " + rule.description)
}
}
export let FizzRule = new Rule((i:number) => i%3 == 0, (current:string[], i:number) => {
current.push("Fizz")
return current;
}, "FizzRule")
export let BuzzRule = new Rule((i:number) => i%5 == 0, (current:string[], i:number) => {
current.push("Buzz")
return current;
}, "BuzzRule")
export let BangRule = new Rule((i:number) => i%7 == 0, (current:string[], i:number) => {
current.push("Bang")
return current;
}, "BangRule")
export let BongRule = new Rule((i:number) => i%11 == 0, (current:string[], i:number) => {
return ["Bong"];
}, "BongRule")
export let FezzRule = new Rule((i:number) => i%13 == 0, (current:string[], j:number) => {
for(let i=0; i<current.length; i++){
if(current[i][0]=='B'){
current.splice(i, 0, "Fezz")
return current;
}
}
current.push("Fezz")
return current;
}, "FezzRule")
export let ReverseRule = new Rule((i:number) => i%17 == 0, (current:string[], i:number) => {
current.reverse();
return current;
}, "ReverseRule")
export let EmptyRule = new Rule((i:number) => true, (current:string[], i:number) => {
if (current.length==0){
return [i.toString()];
}
return current;
}, "NonEmptyRule")
export let Part1Rule = FizzRule.followedBy(BuzzRule).followedBy(EmptyRule);
export let Part2Rule = FizzRule.followedBy(BuzzRule).followedBy(BangRule).followedBy(BongRule).followedBy(FezzRule).followedBy(ReverseRule).followedBy(EmptyRule);
export function printValue(i:number){
console.log(Part2Rule.applyRule([], i).reduce((previous, current, currentIndex, array) => {
return previous + current;
}))
}