-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.py
More file actions
64 lines (51 loc) · 2.32 KB
/
decode.py
File metadata and controls
64 lines (51 loc) · 2.32 KB
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
import requests
from bs4 import BeautifulSoup
def decode_secret_message(url):
# Fetch the Google Doc content
response = requests.get("https://docs.google.com/document/d/e/2PACX-1vQGUck9HIFCyezsrBSnmENk5ieJuYwpt7YHYEzeNJkIb9OSDdx-ov2nRNReKQyey-cwJOoEKUhLmN9z/pub")
response.raise_for_status() # Ensure the request was successful
# Parse the HTML content with BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
# Find the table in the document
table = soup.find('table')
if not table:
raise ValueError("No table found in the Google Doc")
# Extract rows from the table
rows = table.find_all('tr')
if len(rows) < 2:
raise ValueError("Table does not contain enough rows")
# Extract headers (expecting 'x-coordinate', 'Character', 'y-coordinate')
header = [cell.get_text(strip=True) for cell in rows[0].find_all('td')]
if header != ['x-coordinate', 'Character', 'y-coordinate']:
raise ValueError("Unexpected table headers")
# Parse table data into a list of dictionaries
data = []
for row in rows[1:]:
cells = row.find_all('td')
if len(cells) != 3:
continue # Skip malformed rows
entry = {
'x-coordinate': int(cells[0].get_text(strip=True)),
'Character': cells[1].get_text(strip=True),
'y-coordinate': int(cells[2].get_text(strip=True))
}
data.append(entry)
if not data:
raise ValueError("No valid data extracted from the table")
# Determine grid dimensions
max_x = max(entry['x-coordinate'] for entry in data)
max_y = max(entry['y-coordinate'] for entry in data)
# Create a grid filled with spaces
grid = [[' ' for _ in range(max_x + 1)] for _ in range(max_y + 1)]
# Populate the grid with characters
for entry in data:
x = entry['x-coordinate']
y = entry['y-coordinate']
char = entry['Character']
grid[y][x] = char
# Print the grid row by row, reversing y-axis to match example orientation
for row in reversed(grid):
print(''.join(row))
if __name__ == "__main__":
url = "https://docs.google.com/document/d/e/2PACX-1vQGUck9HIFCyezsrBSnmENk5ieJuYwpt7YHYEzeNJkIb9OSDdx-ov2nRNReKQyey-cwJOoEKUhLmN9z/pub"
decode_secret_message(url)