Last active
December 23, 2020 12:47
-
-
Save whatadewitt/9d929de2b297240e28105075cc429ec5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const deck1 = [ | |
14, | |
23, | |
6, | |
16, | |
46, | |
24, | |
13, | |
25, | |
17, | |
4, | |
31, | |
7, | |
1, | |
47, | |
15, | |
9, | |
50, | |
3, | |
30, | |
37, | |
43, | |
10, | |
28, | |
33, | |
32, | |
]; | |
const deck2 = [ | |
29, | |
49, | |
11, | |
42, | |
35, | |
18, | |
39, | |
40, | |
36, | |
19, | |
48, | |
22, | |
2, | |
20, | |
26, | |
8, | |
12, | |
44, | |
45, | |
21, | |
38, | |
41, | |
34, | |
5, | |
27, | |
]; | |
while (deck1.length && deck2.length) { | |
const card1 = deck1.shift(); | |
const card2 = deck2.shift(); | |
if (card1 > card2) { | |
deck1.push(card1, card2); | |
} else { | |
deck2.push(card2, card1); | |
} | |
} | |
const winner = [...deck1, ...deck2]; | |
console.log( | |
winner.reduce((acc, curr, idx) => acc + curr * (winner.length - idx), 0) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let deck1 = [ | |
14, | |
23, | |
6, | |
16, | |
46, | |
24, | |
13, | |
25, | |
17, | |
4, | |
31, | |
7, | |
1, | |
47, | |
15, | |
9, | |
50, | |
3, | |
30, | |
37, | |
43, | |
10, | |
28, | |
33, | |
32, | |
]; | |
let deck2 = [ | |
29, | |
49, | |
11, | |
42, | |
35, | |
18, | |
39, | |
40, | |
36, | |
19, | |
48, | |
22, | |
2, | |
20, | |
26, | |
8, | |
12, | |
44, | |
45, | |
21, | |
38, | |
41, | |
34, | |
5, | |
27, | |
]; | |
function recursiveCombat(d1, d2) { | |
const played = {}; | |
while (d1.length && d2.length) { | |
let hash = `${d1.join("-")}-${d2.join("-")}`; | |
if (played[hash]) { | |
// player 1 wins | |
return [d1, []]; | |
} else { | |
played[hash] = true; | |
} | |
if (deck1.length && deck2.length) { | |
const card1 = d1.shift(); | |
const card2 = d2.shift(); | |
if (card1 <= d1.length && card2 <= d2.length) { | |
const [subgameDeck1, subgameDeck2] = recursiveCombat( | |
d1.slice(0, card1), | |
d2.slice(0, card2) | |
); | |
if (subgameDeck1.length > subgameDeck2.length) { | |
d1.push(card1, card2); | |
} else if (subgameDeck2.length > subgameDeck1.length) { | |
d2.push(card2, card1); | |
} | |
} else if (card1 > card2) { | |
d1.push(card1, card2); | |
} else { | |
d2.push(card2, card1); | |
} | |
} | |
} | |
return [d1, d2]; | |
} | |
[deck1, deck2] = recursiveCombat(deck1, deck2); | |
console.log(deck1, deck2); | |
const winner = [...deck1, ...deck2]; | |
console.log( | |
winner.reduce((acc, curr, idx) => acc + curr * (winner.length - idx), 0) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment