|
| 1 | +import {readFileSync} from 'fs'; |
| 2 | +import { |
| 3 | + add_vote, |
| 4 | + find_lowest_name, |
| 5 | + remove_name, |
| 6 | + has_names_remaining, |
| 7 | + count_appearance, |
| 8 | + normalize_by_appearances, |
| 9 | + calculate_best_worst, |
| 10 | + normalize_scores, |
| 11 | +} from './name-scores'; |
| 12 | + |
| 13 | +const fileToArray = (): string[][] => { |
| 14 | + const content = readFileSync(`/Users/ajhorst/dev/fantasy-name-rankings/resources/responses.csv`, 'utf8'); |
| 15 | + |
| 16 | + const result: string[][] = []; |
| 17 | + |
| 18 | + const rows = content.split('\n'); |
| 19 | + for (let i = 0; i < rows.length; i++) { |
| 20 | + const row = rows[i]; |
| 21 | + if (result[i] === undefined) { |
| 22 | + result[i] = []; |
| 23 | + } |
| 24 | + const cells = row.substring(1, row.length - 1).split('","'); |
| 25 | + // starting at 1 because 0 is response timestamps and 1 is emails |
| 26 | + for (let j = 2; j < cells.length; j++) { |
| 27 | + const cell = cells[j]; |
| 28 | + result[i].push(cell); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return result; |
| 33 | +}; |
| 34 | + |
| 35 | +const parse_name = (line: string): string => { |
| 36 | + const bracket_start = line.indexOf('['); |
| 37 | + const bracket_end = line.indexOf(']'); |
| 38 | + return line.substring(bracket_start + 1, bracket_end); |
| 39 | +}; |
| 40 | + |
| 41 | +const make_vote_group = (arr: string[][], row: number, i: number): string[] => { |
| 42 | + const vote_1 = arr[row][i]; |
| 43 | + const vote_2 = arr[row][i + 1]; |
| 44 | + const vote_3 = arr[row][i + 2]; |
| 45 | + const vote_4 = arr[row][i + 3]; |
| 46 | + |
| 47 | + return [vote_1, vote_2, vote_3, vote_4]; |
| 48 | +}; |
| 49 | + |
| 50 | +const arrayToVotes = (arr: string[][]) => { |
| 51 | + const rowCount = arr.length; |
| 52 | + const colCount = arr[0].length; |
| 53 | + |
| 54 | + for (let i = 0; i < colCount; i += 4) { |
| 55 | + const name_1 = parse_name(arr[0][i]); |
| 56 | + const name_2 = parse_name(arr[0][i + 1]); |
| 57 | + const name_3 = parse_name(arr[0][i + 2]); |
| 58 | + const name_4 = parse_name(arr[0][i + 3]); |
| 59 | + |
| 60 | + const name_group = [name_1, name_2, name_3, name_4]; |
| 61 | + count_appearance(name_group); |
| 62 | + for (let j = 1; j < rowCount; j++) { |
| 63 | + const vote_group = make_vote_group(arr, j, i); |
| 64 | + add_vote(name_group, vote_group); |
| 65 | + } |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +const names_scores: [string, number][] = []; |
| 70 | + |
| 71 | +const calculateList = () => { |
| 72 | + normalize_by_appearances(); |
| 73 | + while (has_names_remaining()) { |
| 74 | + const [lowest_name, lowest_score] = find_lowest_name(); |
| 75 | + names_scores.push([lowest_name, lowest_score]); |
| 76 | + remove_name(lowest_name); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +const arr = fileToArray(); |
| 81 | + |
| 82 | +arrayToVotes(arr); |
| 83 | + |
| 84 | +calculateList(); |
| 85 | +calculate_best_worst(); |
| 86 | + |
| 87 | +const final_scores = normalize_scores(names_scores); |
| 88 | +console.log('final list is:'); |
| 89 | + |
| 90 | +for (let i = 0; i < final_scores.length; i++) { |
| 91 | + const [name, score] = final_scores[i]; |
| 92 | + console.log(`${name} with a score of ${score}`); |
| 93 | +} |
0 commit comments