Skip to content

Commit 1f3c524

Browse files
committed
solved(python): programmers
- 2024 KAKAO WINTER INTERNSHIP / 주사위 고르기
1 parent 84647c9 commit 1f3c524

4 files changed

Lines changed: 204 additions & 0 deletions

File tree

programmers/2024_KAKAO_WINTER_INTERNSHIP/python/주사위_고르기/__init__.py

Whitespace-only changes.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import itertools
2+
from collections import defaultdict
3+
from typing import List, Tuple, Dict
4+
5+
6+
def solution(dice: List[List[int]]) -> List[int]:
7+
n = len(dice)
8+
cached = {
9+
to_binary(ids): find_combinations([dice[idx] for idx in ids])
10+
for ids in itertools.combinations(range(n), n // 2)
11+
}
12+
13+
result, visited = (0, []), set()
14+
for x_key in cached:
15+
if x_key in visited:
16+
continue
17+
18+
y_key = (2**n - 1) - x_key
19+
visited |= {x_key, y_key}
20+
21+
win, draw, lose = compare(cached[x_key], cached[y_key])
22+
if max(win, lose) > result[0]:
23+
result = (max(win, lose), to_num(x_key if win > lose else y_key))
24+
25+
return result[1]
26+
27+
28+
def to_binary(nums: Tuple[int, ...]) -> int:
29+
key = 0
30+
for idx in nums:
31+
key |= 1 << idx
32+
33+
return key
34+
35+
36+
def to_num(binary: int) -> List[int]:
37+
nums = []
38+
for idx in range(len(bin(binary)) - 2):
39+
if (1 << idx) & binary:
40+
nums.append(idx + 1)
41+
42+
return nums
43+
44+
45+
def find_combinations(dices: List[List[int]]) -> Dict[int, int]:
46+
combinations = defaultdict(int)
47+
for ids in itertools.product(range(6), repeat=len(dices)):
48+
key = sum([dice[idx] for idx, dice in zip(ids, dices)])
49+
combinations[key] += 1
50+
51+
return combinations
52+
53+
54+
def compare(x: Dict[int, int], y: Dict[int, int]) -> Tuple[int, int, int]:
55+
count = [0, 0, 0]
56+
57+
for (x_key, x_value), (y_key, y_value) in itertools.product(x.items(), y.items()):
58+
if x_key > y_key:
59+
count[0] += x_value * y_value
60+
elif x_key == y_key:
61+
count[1] += x_value * y_value
62+
else:
63+
count[2] += x_value * y_value
64+
65+
return count[0], count[1], count[2]
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
[
2+
{
3+
"params": [
4+
[
5+
[
6+
1,
7+
2,
8+
3,
9+
4,
10+
5,
11+
6
12+
],
13+
[
14+
3,
15+
3,
16+
3,
17+
3,
18+
4,
19+
4
20+
],
21+
[
22+
1,
23+
3,
24+
3,
25+
4,
26+
4,
27+
4
28+
],
29+
[
30+
1,
31+
1,
32+
4,
33+
4,
34+
5,
35+
5
36+
]
37+
]
38+
],
39+
"expected": [
40+
1,
41+
4
42+
]
43+
},
44+
{
45+
"params": [
46+
[
47+
[
48+
1,
49+
2,
50+
3,
51+
4,
52+
5,
53+
6
54+
],
55+
[
56+
2,
57+
2,
58+
4,
59+
4,
60+
6,
61+
6
62+
]
63+
]
64+
],
65+
"expected": [
66+
2
67+
]
68+
},
69+
{
70+
"params": [
71+
[
72+
[
73+
40,
74+
41,
75+
42,
76+
43,
77+
44,
78+
45
79+
],
80+
[
81+
43,
82+
43,
83+
42,
84+
42,
85+
41,
86+
41
87+
],
88+
[
89+
1,
90+
1,
91+
80,
92+
80,
93+
80,
94+
80
95+
],
96+
[
97+
70,
98+
70,
99+
1,
100+
1,
101+
70,
102+
70
103+
]
104+
]
105+
],
106+
"expected": [
107+
1,
108+
3
109+
]
110+
}
111+
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import os
3+
import unittest
4+
5+
from parameterized import parameterized
6+
7+
from .main import solution
8+
9+
10+
def load_sample(filename: str):
11+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
12+
13+
with open(path, "r") as file:
14+
return [(case["params"], case["expected"]) for case in json.load(file)]
15+
16+
17+
class TestCase(unittest.TestCase):
18+
@parameterized.expand(load_sample("sample.json"))
19+
def test_case(self, params: list, expected: any):
20+
# When
21+
result = solution(*params)
22+
23+
# Then
24+
self.assertEqual(expected, result)
25+
26+
27+
if __name__ == "__main__":
28+
unittest.main()

0 commit comments

Comments
 (0)