#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import string

KEYBOARD_SCANCODES = [
    0x2A, 0x2E, 0xAE, 0x14, 0x94, 0x21, 0xA1, 0x1A,  # *....!..
    0x9A, 0xAA, 0x1E, 0x9E, 0x2E, 0xAE, 0x19, 0x99,  # ........
    0x17, 0x97, 0x2A, 0x0C, 0x8C, 0xAA, 0x32, 0xB2,  # ..*...2.
    0x1E, 0x9E, 0x2E, 0xAE, 0x23, 0xA3, 0x17, 0x97,  # ....#...
    0x31, 0xB1, 0x12, 0x92, 0x2A, 0x0C, 0x8C, 0xAA,  # 1...*...
    0x26, 0xA6, 0x1E, 0x9E, 0x31, 0xB1, 0x22, 0xA2,  # &...1.".
    0x16, 0x96, 0x1E, 0x9E, 0x22, 0xA2, 0x12, 0x92,  # ...."...
    0x2A, 0x1B, 0x9B, 0xAA, 0x1C, 0x9C,              # *.....
]

VSC_TO_VK_MAPPING = {
    0x29: 0x60,  # `
    0x02: 0x31,  # 1
    0x03: 0x32,  # 2
    0x04: 0x33,  # 3
    0x05: 0x34,  # 4
    0x06: 0x35,  # 5
    0x07: 0x36,  # 6
    0x08: 0x37,  # 7
    0x09: 0x38,  # 8
    0x0A: 0x39,  # 9
    0x0B: 0x30,  # 0
    0x0C: 0x2D,  # -
    0x0D: 0x3D,  # =
    0x10: 0x71,  # q
    0x11: 0x77,  # w
    0x12: 0x65,  # e
    0x13: 0x72,  # r
    0x14: 0x74,  # t
    0x15: 0x79,  # y
    0x16: 0x75,  # u
    0x17: 0x69,  # i
    0x18: 0x6F,  # o
    0x19: 0x70,  # p
    0x1A: 0x5B,  # [
    0x1B: 0x5D,  # ]
    0x2B: 0x5C,  # \
    0x1E: 0x61,  # a
    0x1F: 0x73,  # s
    0x20: 0x64,  # d
    0x21: 0x66,  # f
    0x22: 0x67,  # g
    0x23: 0x68,  # h
    0x24: 0x6A,  # j
    0x25: 0x6B,  # k
    0x26: 0x6C,  # l
    0x27: 0x3B,  # ;
    0x28: 0x27,  # '
    0x2C: 0x7A,  # z
    0x2D: 0x78,  # x
    0x2E: 0x63,  # c
    0x2F: 0x76,  # v
    0x30: 0x62,  # b
    0x31: 0x6E,  # n
    0x32: 0x6D,  # m
    0x33: 0x2C,  # ,
    0x34: 0x2E,  # .
    0x35: 0x2F,  # /
}


# Convert all of the physical scancodes into virtual key codes first.
# Non-printable scan codes that wouldn't convert to an ASCII key will be ignored.
virtual_key_codes = []
for key in KEYBOARD_SCANCODES:
    if match := VSC_TO_VK_MAPPING.get(key):
        virtual_key_codes.append(match)

# Convert all the key codes from the KEYMAP into printable ASCII characters.
# Non-printable key codes will be ignored.
flag = ""
for code in virtual_key_codes:
    code = chr(code)
    flag += code if code in string.printable else ""

# Print out final flag.
print("Flag:", flag)
