using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.RegularExpressions;

class User
{
    public string Username { get; set; }
    public string PasswordHash { get; set; }
    public string Email { get; set; }

    public static bool IsPasswordValid(string password)
    {
        return password.Length >= 8 && 
               Regex.IsMatch(password, "[0-9]") && 
               Regex.IsMatch(password, "[^a-zA-Z0-9]");
    }
}

class UserManager
{
    public static void SaveToFile(List<User> users, string path)
    {
        File.WriteAllText(path, JsonSerializer.Serialize(users));
    }

    public static List<User> LoadFromFile(string path)
    {
        return File.Exists(path) ? 
            JsonSerializer.Deserialize<List<User>>(File.ReadAllText(path)) : 
            new List<User>();
    }
}

class Program
{
    static void Main()
    {
        List<User> users = UserManager.LoadFromFile("users.json");
        Console.Write("Enter username: ");
        string username = Console.ReadLine();
        var user = users.Find(u => u.Username == username);
        if (user != null)
        {
            Console.WriteLine("Enter password:");
            string password = Console.ReadLine();
            if (user.PasswordHash == HashPassword(password))
                Console.WriteLine($"Welcome, {username}!");
            else
                Console.WriteLine("Invalid password");
        }
        else
        {
            Console.WriteLine("User not found. Register? (y/n)");
            if (Console.ReadKey().Key == ConsoleKey.Y)
            {
                Console.WriteLine("\nEnter password (min 8 chars, with numbers and symbols):");
                string password = Console.ReadLine();
                if (User.IsPasswordValid(password))
                {
                    users.Add(new User { Username = username, PasswordHash = HashPassword(password) });
                    UserManager.SaveToFile(users, "users.json");
                    Console.WriteLine("Registered successfully!");
                }
                else
                {
                    Console.WriteLine("Password does not meet requirements");
                }
            }
        }
    }

    static string HashPassword(string password)
    {
        using (SHA256 sha256 = SHA256.Create())
        {
            byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));
            return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
        }
    }
}