@@ -43,51 +43,51 @@ type CircleOfSuckEdge = {
43
43
}
44
44
45
45
const findCircleOfSuck = async ( teams : Team [ ] , games : Game [ ] ) : Promise < CircleOfSuckEdge [ ] | null > => {
46
- // maps winner team -> loser team
47
- const gameGraph = _ . fromPairs ( _ . map ( teams , ( { school } ) => [ school , [ ] as string [ ] ] ) )
48
-
49
- // contains all future games, as [home_team, away_team] pairs
50
- const futureGames = [ ] as [ string , string ] [ ]
51
-
52
- _ . each ( games , ( { conference_game, away_team, home_team, away_points, home_points } ) => {
53
- if ( ! conference_game ) {
54
- return
55
- }
56
-
57
- if ( away_points === null || home_points === null ) {
58
- futureGames . push ( [ home_team , away_team ] )
59
- } else if ( away_points > home_points ) {
60
- gameGraph [ away_team ] . push ( home_team )
61
- } else {
62
- gameGraph [ home_team ] . push ( away_team )
63
- }
64
- } )
46
+ // maps winner team -> loser team
47
+ const gameGraph = _ . fromPairs ( _ . map ( teams , ( { school } ) => [ school , [ ] as string [ ] ] ) )
65
48
66
- const teamToIndex = _ . fromPairs ( _ . map ( teams , ( { school } , i ) => [ school , i ] ) )
49
+ // contains all future games, as [home_team, away_team] pairs
50
+ const futureGames = [ ] as [ string , string ] [ ]
67
51
68
- const graph = new WeightedDiGraph ( teams . length )
69
- _ . each ( gameGraph , ( losers , winner ) => {
70
- _ . each ( losers , ( loser ) => {
71
- graph . addEdge ( teamToIndex [ winner ] , teamToIndex [ loser ] , 0 )
72
- } )
73
- } )
74
- _ . each ( futureGames , ( [ home , away ] ) => {
75
- graph . addEdge ( teamToIndex [ home ] , teamToIndex [ away ] , 1 )
76
- graph . addEdge ( teamToIndex [ away ] , teamToIndex [ home ] , 1 )
77
- } )
52
+ _ . each ( games , ( { conference_game, away_team, home_team, away_points, home_points } ) => {
53
+ if ( ! conference_game ) {
54
+ return
55
+ }
78
56
79
- const hamiltonian = getHamiltonian ( graph )
80
- if ( ! hamiltonian ) {
81
- return null
57
+ if ( away_points === null || home_points === null ) {
58
+ futureGames . push ( [ home_team , away_team ] )
59
+ } else if ( away_points > home_points ) {
60
+ gameGraph [ away_team ] . push ( home_team )
61
+ } else {
62
+ gameGraph [ home_team ] . push ( away_team )
82
63
}
64
+ } )
83
65
84
- return _ . map ( hamiltonian . map ( ( v ) => teams [ v ] ) , ( team1 , i , arr ) => {
85
- const team2 = arr [ i === arr . length - 1 ? 0 : i + 1 ]
66
+ const teamToIndex = _ . fromPairs ( _ . map ( teams , ( { school } , i ) => [ school , i ] ) )
86
67
87
- return {
88
- from : team1 ,
89
- to : team2 ,
90
- isPlayed : _ . includes ( gameGraph [ team1 . school ] , team2 . school ) ,
91
- }
68
+ const graph = new WeightedDiGraph ( teams . length )
69
+ _ . each ( gameGraph , ( losers , winner ) => {
70
+ _ . each ( losers , ( loser ) => {
71
+ graph . addEdge ( teamToIndex [ winner ] , teamToIndex [ loser ] , 0 )
92
72
} )
73
+ } )
74
+ _ . each ( futureGames , ( [ home , away ] ) => {
75
+ graph . addEdge ( teamToIndex [ home ] , teamToIndex [ away ] , 1 )
76
+ graph . addEdge ( teamToIndex [ away ] , teamToIndex [ home ] , 1 )
77
+ } )
78
+
79
+ const hamiltonian = getHamiltonian ( graph )
80
+ if ( ! hamiltonian ) {
81
+ return null
93
82
}
83
+
84
+ return _ . map ( hamiltonian . map ( ( v ) => teams [ v ] ) , ( team1 , i , arr ) => {
85
+ const team2 = arr [ i === arr . length - 1 ? 0 : i + 1 ]
86
+
87
+ return {
88
+ from : team1 ,
89
+ to : team2 ,
90
+ isPlayed : _ . includes ( gameGraph [ team1 . school ] , team2 . school ) ,
91
+ }
92
+ } )
93
+ }
0 commit comments