File size: 2,746 Bytes
44bd370
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from collections import deque
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.progress import Progress, SpinnerColumn, TextColumn

console = Console()

class WikiSolver:
    def __init__(self, wiki_data):
        """Initialize the solver with wiki data"""
        self.wiki_data = wiki_data
        
    def find_path(self, start_article, target_article):
        """Find the shortest path using BFS"""
        if start_article not in self.wiki_data:
            return None, "Start article not found in wiki data"
        if target_article not in self.wiki_data:
            return None, "Target article not found in wiki data"
            
        # Initialize BFS
        queue = deque([(start_article, [start_article])])
        visited = {start_article}
        
        with Progress(
            SpinnerColumn(),
            TextColumn("[progress.description]{task.description}"),
            console=console
        ) as progress:
            task = progress.add_task("Finding path...", total=None)
            
            while queue:
                current, path = queue.popleft()
                
                if current == target_article:
                    return path, None
                    
                # Get all links from current article
                if current in self.wiki_data:
                    for next_article in self.wiki_data[current].get('links', []):
                        if next_article not in visited and next_article in self.wiki_data:
                            visited.add(next_article)
                            queue.append((next_article, path + [next_article]))
                            
        return None, "No path found"
        
    def display_solution(self, path, start_article, target_article):
        """Display the solution in a beautiful format"""
        if not path:
            console.print("[red]No solution found![/red]")
            return
            
        # Create a panel for the solution
        console.print(Panel(
            f"[bold cyan]Solution Found![/bold cyan]\n"
            f"From: [green]{start_article}[/green]\n"
            f"To: [red]{target_article}[/red]\n"
            f"Steps: [yellow]{len(path)-1}[/yellow]",
            title="Wiki Run Solver",
            border_style="cyan"
        ))
        
        # Create a table for the path
        table = Table(show_header=True, header_style="bold magenta")
        table.add_column("Step", style="dim")
        table.add_column("Article", style="cyan")
        
        for i, article in enumerate(path):
            table.add_row(
                str(i),
                article
            )
            
        console.print(table)