<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fixability
{
    public class Mind
    {
        IGraph _graph;
        IDynamicAnalyzer _dynamicAnalyzer;
        IStaticAnalyzer _staticAnalyzer;
        IAssignmentGenerator _assignmentGenerator;
        IListHelper _listHelper;

        public List<IAssignment> Assignments { get; private set; }
        public List<IAssignment> ColorableAssignments { get; private set; }
        public List<IAssignment> NearlyColorableAssignments { get; private set; }
        public List<IAssignment> NonColorableAssignments { get; private set; }
        public List<IAssignment> NonSuperabundantAssignments { get; private set; }

        public Mind(IGraph graph, IDynamicAnalyzer dynamicAnalyzer, IStaticAnalyzer staticAnalyzer, IAssignmentGenerator assignmentGenerator, IListHelper listHelper)
        {
            _graph = graph;
            _dynamicAnalyzer = dynamicAnalyzer;
            _staticAnalyzer = staticAnalyzer;
            _assignmentGenerator = assignmentGenerator;
            _listHelper = listHelper;
        }

        public AnalysisResult Analyze(int[] sizes, int maxPot = int.MaxValue)
        {
            if (sizes.Length != _graph.N)
                throw new Exception("Wrong number of list sizes.");

            var minPot = sizes.Max();
            maxPot = Math.Min(maxPot, sizes.Sum());

            Assignments = _assignmentGenerator.Generate(sizes, minPot, maxPot);
           
            DoStaticAnalysis();
            DoDynamicAnalysis();

            return AnalysisResult.NotFixable;
        }

        void DoDynamicAnalysis()
        {
            var targetAssignments = new HashSet<IAssignment>(ColorableAssignments);
            var remainingAssignments = NonColorableAssignments.ToList();

            while (true)
            {
                var wonAssignments = new List<IAssignment>();

                for (int i = remainingAssignments.Count - 1; i >= 0; i--)
                {
                    var assignment = remainingAssignments[i];
                    if (_dynamicAnalyzer.Analyze(assignment, targetAssignments))
                    {
                        remainingAssignments.RemoveAt(i);
                        wonAssignments.Add(assignment);
                    }
                }

                if (wonAssignments.Count <= 0)
                    break;

                foreach (var assignment in wonAssignments)
                    targetAssignments.Add(assignment);
            }
        }

        void DoStaticAnalysis()
        {
            ColorableAssignments = new List<IAssignment>();
            NonColorableAssignments = new List<IAssignment>();
            NonSuperabundantAssignments = new List<IAssignment>();
            NearlyColorableAssignments = new List<IAssignment>();

            foreach (var assignment in Assignments)
            {
                if (_staticAnalyzer.IsColorable(assignment))
                    ColorableAssignments.Add(assignment);
                else
                {
                    NonColorableAssignments.Add(assignment);
                    if (!_staticAnalyzer.IsSuperabundant(assignment))
                        NonSuperabundantAssignments.Add(assignment);
                    if (_staticAnalyzer.IsNearlyColorable(assignment))
                        NearlyColorableAssignments.Add(assignment);
                }
            }
        }
    }<commit_msg>a little more<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fixability
{
    public class Mind
    {
        IGraph _graph;
        IDynamicAnalyzer _dynamicAnalyzer;
        IStaticAnalyzer _staticAnalyzer;
        IAssignmentGenerator _assignmentGenerator;
        IListHelper _listHelper;

        public List<IAssignment> Assignments { get; private set; }
        public List<IAssignment> ColorableAssignments { get; private set; }
        public List<IAssignment> NearlyColorableAssignments { get; private set; }
        public List<IAssignment> NonColorableAssignments { get; private set; }
        public List<IAssignment> NonSuperabundantAssignments { get; private set; }

        public Mind(IGraph graph, IDynamicAnalyzer dynamicAnalyzer, IStaticAnalyzer staticAnalyzer, IAssignmentGenerator assignmentGenerator, IListHelper listHelper)
        {
            _graph = graph;
            _dynamicAnalyzer = dynamicAnalyzer;
            _staticAnalyzer = staticAnalyzer;
            _assignmentGenerator = assignmentGenerator;
            _listHelper = listHelper;
        }

        public AnalysisResult Analyze(int[] sizes, int maxPot = int.MaxValue)
        {
            if (sizes.Length != _graph.N)
                throw new Exception("Wrong number of list sizes.");

            var minPot = sizes.Max();
            maxPot = Math.Min(maxPot, sizes.Sum());

            Assignments = _assignmentGenerator.Generate(sizes, minPot, maxPot);
           
            DoStaticAnalysis();
            var remainingAssignments = DoDynamicAnalysis();

            return AnalysisResult.NotFixable;
        }

        List<IAssignment> DoDynamicAnalysis()
        {
            var targetAssignments = new HashSet<IAssignment>(ColorableAssignments);
            var remainingAssignments = NonColorableAssignments.ToList();

            while (true)
            {
                var wonAssignments = new List<IAssignment>();

                for (int i = remainingAssignments.Count - 1; i >= 0; i--)
                {
                    var assignment = remainingAssignments[i];
                    if (_dynamicAnalyzer.Analyze(assignment, targetAssignments))
                    {
                        remainingAssignments.RemoveAt(i);
                        wonAssignments.Add(assignment);
                    }
                }

                if (wonAssignments.Count <= 0)
                    break;

                foreach (var assignment in wonAssignments)
                    targetAssignments.Add(assignment);
            }

            return remainingAssignments;
        }

        void DoStaticAnalysis()
        {
            ColorableAssignments = new List<IAssignment>();
            NonColorableAssignments = new List<IAssignment>();
            NonSuperabundantAssignments = new List<IAssignment>();
            NearlyColorableAssignments = new List<IAssignment>();

            foreach (var assignment in Assignments)
            {
                if (_staticAnalyzer.IsColorable(assignment))
                    ColorableAssignments.Add(assignment);
                else
                {
                    NonColorableAssignments.Add(assignment);
                    if (!_staticAnalyzer.IsSuperabundant(assignment))
                        NonSuperabundantAssignments.Add(assignment);
                    if (_staticAnalyzer.IsNearlyColorable(assignment))
                        NearlyColorableAssignments.Add(assignment);
                }
            }
        }
    }
{'fraction_non_alphanumeric': 0.07269155206286837, 'fraction_numerical': 0.0008419870895312938, 'mean_word_length': 1.8588046530284796, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7346173', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1686, 'n_words': 362}