made some progress
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -8,135 +9,127 @@ namespace Program
|
||||
{
|
||||
class Program
|
||||
{
|
||||
public static Dictionary<int, List<int>> Contacts = new Dictionary<int, List<int>>();
|
||||
public static HashSet<int> InfectedPeople = new HashSet<int>();
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
List<int> primes = new List<int>();
|
||||
string path = $@"../../../forrasok/elek.txt";
|
||||
|
||||
for (int i = 1000; i < 10000; i++)
|
||||
var data = File.ReadAllLines(path);
|
||||
|
||||
foreach (var item in data)
|
||||
{
|
||||
if (IsPrime(i))
|
||||
string[] nums = item.Split(' ');
|
||||
int infector = Convert.ToInt32(nums[0]);
|
||||
int infected = Convert.ToInt32(nums[1]);
|
||||
|
||||
if (Contacts.ContainsKey(infector))
|
||||
{
|
||||
primes.Add(i);
|
||||
string result = string.Join("", GetSortedDigits(i));
|
||||
Console.WriteLine($"({i},{result}),");
|
||||
Contacts[infector].Add(infected);
|
||||
}
|
||||
else
|
||||
{
|
||||
Contacts[infector] = new List<int> { infected };
|
||||
}
|
||||
}
|
||||
|
||||
List<int[]> primesWithDecimalValuesOrdered = new List<int[]>();
|
||||
Virus rootVirus = new Virus(2);
|
||||
InfectedPeople.Clear(); // Clear the set before starting the spread
|
||||
InfectedPeople.Add(2); // Add the root virus to the infected set
|
||||
SpreadBFS(rootVirus, 2);
|
||||
//Spread(rootVirus, 9);
|
||||
|
||||
foreach (int i in primes)
|
||||
//rootVirus.PrintInfectionTreeWithDepth(2, 0, "->");
|
||||
rootVirus.PrintInfectionTree();
|
||||
|
||||
Console.WriteLine(InfectedPeople.Count);
|
||||
}
|
||||
|
||||
|
||||
public static void SpreadBFS(Virus rootVirus, int maxDepth)
|
||||
{
|
||||
Queue<(Virus virus, int depth)> queue = new Queue<(Virus virus, int depth)>();
|
||||
queue.Enqueue((rootVirus, 0));
|
||||
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
primesWithDecimalValuesOrdered.Add(GetSortedDigits(i));
|
||||
}
|
||||
var (currentVirus, currentDepth) = queue.Dequeue();
|
||||
|
||||
var keyValuePairs = primesWithDecimalValuesOrdered
|
||||
.GroupBy(x => x, new IntArrayComparer())
|
||||
.Select(g => new KeyValuePair<int[], int>(g.Key, g.Count()))
|
||||
.Where(kvp => kvp.Value >= 6)
|
||||
.ToList().Count();
|
||||
|
||||
// Print the key-value pairs
|
||||
/*
|
||||
foreach (var kvp in keyValuePairs)
|
||||
{
|
||||
Console.WriteLine($"Key: {string.Join("", kvp.Key)}, Value: {kvp.Value}");
|
||||
}
|
||||
*/
|
||||
|
||||
Console.WriteLine(keyValuePairs);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int[] digits = { 0, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9 };
|
||||
int length = 4;
|
||||
|
||||
PermutationGenerator generator = new PermutationGenerator();
|
||||
generator.GeneratePermutations(digits, length);
|
||||
//generator.PrintGroupedPermutations();
|
||||
|
||||
// Get groups with at least 6 primes
|
||||
var groupsWithAtLeast6Primes = generator.GroupedPermutations
|
||||
.Where(group => group.Value.Count(permutation => IsPrime(int.Parse(string.Join("", permutation)))) >= 6)
|
||||
.ToList();
|
||||
|
||||
// Print the groups with at least 6 primes
|
||||
foreach (var group in groupsWithAtLeast6Primes)
|
||||
{
|
||||
Console.WriteLine($"Group: {group.Key}");
|
||||
foreach (var permutation in group.Value)
|
||||
if (currentDepth >= maxDepth)
|
||||
{
|
||||
Console.WriteLine(string.Join("", permutation));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Contacts.ContainsKey(currentVirus.Name))
|
||||
{
|
||||
foreach (var contact in Contacts[currentVirus.Name])
|
||||
{
|
||||
if (!InfectedPeople.Contains(contact))
|
||||
{
|
||||
Virus newInfected = new Virus(contact, currentDepth + 1);
|
||||
currentVirus.Infect(newInfected);
|
||||
InfectedPeople.Add(contact); // Mark as infected
|
||||
queue.Enqueue((newInfected, currentDepth + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the current virus should be healed
|
||||
if (currentDepth - currentVirus.InfectionTurn >= 8)
|
||||
{
|
||||
InfectedPeople.Remove(currentVirus.Name);
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
static int[] GetSortedDigits(int number)
|
||||
public static void Spread(Virus virus, int maxDepth, int currentDepth = 0)
|
||||
{
|
||||
// Convert the number to a string to access each digit
|
||||
string numberString = number.ToString();
|
||||
|
||||
// Convert each character to an integer
|
||||
int[] digits = numberString.Select(c => int.Parse(c.ToString())).ToArray();
|
||||
|
||||
// Sort the array of integers
|
||||
Array.Sort(digits);
|
||||
|
||||
return digits;
|
||||
}
|
||||
|
||||
static bool IsPrime(int number)
|
||||
{
|
||||
if (number <= 1) return false;
|
||||
if (number == 2) return true;
|
||||
if (number % 2 == 0) return false;
|
||||
|
||||
int boundary = (int)Math.Floor(Math.Sqrt(number));
|
||||
|
||||
for (int i = 3; i <= boundary; i += 2)
|
||||
if (currentDepth >= maxDepth)
|
||||
{
|
||||
if (number % i == 0) return false;
|
||||
return;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class IntArrayComparer : IEqualityComparer<int[]>
|
||||
{
|
||||
public bool Equals(int[] x, int[] y)
|
||||
{
|
||||
if (x == null || y == null)
|
||||
return false;
|
||||
|
||||
if (x.Length != y.Length)
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < x.Length; i++)
|
||||
if (Contacts.ContainsKey(virus.Name))
|
||||
{
|
||||
if (x[i] != y[i])
|
||||
return false;
|
||||
}
|
||||
foreach (var contact in Contacts[virus.Name])
|
||||
{
|
||||
if (!InfectedPeople.Contains(contact))
|
||||
{
|
||||
Virus newInfected = new Virus(contact);
|
||||
virus.Infect(newInfected);
|
||||
InfectedPeople.Add(contact); // Mark as infected
|
||||
Spread(newInfected, maxDepth, currentDepth + 1);
|
||||
} else
|
||||
{
|
||||
Virus newInfected = new Virus(contact);
|
||||
virus.Infect(newInfected);
|
||||
//InfectedPeople.Add(contact); // Mark as infected
|
||||
Spread(newInfected, maxDepth, currentDepth + 1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int GetHashCode(int[] obj)
|
||||
/*
|
||||
public static void Spread(Virus virus)
|
||||
{
|
||||
if (obj == null)
|
||||
return 0;
|
||||
|
||||
int hash = 17;
|
||||
foreach (int i in obj)
|
||||
if (Contacts.ContainsKey(virus.Name))
|
||||
{
|
||||
hash = hash * 31 + i;
|
||||
foreach (var contact in Contacts[virus.Name])
|
||||
{
|
||||
if (!InfectedPeople.Contains(contact))
|
||||
{
|
||||
Virus newInfected = new Virus(contact);
|
||||
virus.Infect(newInfected);
|
||||
InfectedPeople.Add(contact); // Mark as infected
|
||||
Spread(newInfected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user