Skip to content

Commit 7648476

Browse files
committed
solved(python): programmers
- PCCP 기출문제 / 충돌위험 찾기
1 parent 7a6288d commit 7648476

4 files changed

Lines changed: 109 additions & 0 deletions

File tree

programmers/PCCP_기출문제/python/충돌위험_찾기/__init__.py

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from collections import Counter
2+
from typing import List
3+
4+
5+
def solution(points: List[List[int]], routes: List[List[int]]) -> int:
6+
points = [(y - 1, x - 1) for y, x in points]
7+
queue, count = [(0, points[route[0] - 1], [r - 1 for r in route]) for route in routes], 0
8+
9+
while queue:
10+
new_queue, counter = [], Counter([point for idx, point, path in queue])
11+
count += len([key for key, count in counter.items() if count > 1])
12+
13+
for idx, (y, x), path in queue:
14+
if (y, x) == points[path[idx]]:
15+
idx += 1
16+
17+
if idx == len(path):
18+
continue
19+
20+
target_y, target_x = points[path[idx]]
21+
if y != target_y:
22+
dy = 1 if y < target_y else -1
23+
new_queue.append((idx, (y + dy, x), path))
24+
else:
25+
dx = 1 if x < target_x else -1
26+
new_queue.append((idx, (y, x + dx), path))
27+
28+
queue = new_queue
29+
30+
return count
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[
2+
{
3+
"params": [
4+
[
5+
[3, 2],
6+
[6, 4],
7+
[4, 7],
8+
[1, 4]
9+
],
10+
[
11+
[4, 2],
12+
[1, 3],
13+
[2, 4]
14+
]
15+
],
16+
"expected": "1"
17+
},
18+
{
19+
"params": [
20+
[
21+
[3, 2],
22+
[6, 4],
23+
[4, 7],
24+
[1, 4]
25+
],
26+
[
27+
[4, 2],
28+
[1, 3],
29+
[4, 2],
30+
[4, 3]
31+
]
32+
],
33+
"expected": "9"
34+
},
35+
{
36+
"params": [
37+
[
38+
[2, 2],
39+
[2, 3],
40+
[2, 7],
41+
[6, 6],
42+
[5, 2]
43+
],
44+
[
45+
[2, 3, 4, 5],
46+
[1, 3, 4, 5]
47+
]
48+
],
49+
"expected": "0"
50+
}
51+
]
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: str):
20+
# When
21+
result = solution(*params)
22+
23+
# Then
24+
self.assertEqual(expected, str(result))
25+
26+
27+
if __name__ == "__main__":
28+
unittest.main()

0 commit comments

Comments
 (0)