bastien.monsarrat 6 éve
szülő
commit
7ddb8c845a
5 módosított fájl, 141 hozzáadás és 0 törlés
  1. 6 0
      Adv2015.sln
  2. 9 0
      D17.1/D17.csproj
  3. 98 0
      D17.1/Program.cs
  4. 8 0
      D17.1/Properties/launchSettings.json
  5. 20 0
      D17.1/input.txt

+ 6 - 0
Adv2015.sln

@@ -63,6 +63,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D16.1", "D16.1\D16.1.csproj
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D16.2", "D16.2\D16.2.csproj", "{E77FF61C-90C8-4C1C-A458-CA0479C5BAA6}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D17", "D17.1\D17.csproj", "{513BD630-6A66-487D-8199-68CE213DECF4}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -189,6 +191,10 @@ Global
 		{E77FF61C-90C8-4C1C-A458-CA0479C5BAA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{E77FF61C-90C8-4C1C-A458-CA0479C5BAA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{E77FF61C-90C8-4C1C-A458-CA0479C5BAA6}.Release|Any CPU.Build.0 = Release|Any CPU
+		{513BD630-6A66-487D-8199-68CE213DECF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{513BD630-6A66-487D-8199-68CE213DECF4}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{513BD630-6A66-487D-8199-68CE213DECF4}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{513BD630-6A66-487D-8199-68CE213DECF4}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 9 - 0
D17.1/D17.csproj

@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <RootNamespace>D17._1</RootNamespace>
+  </PropertyGroup>
+
+</Project>

+ 98 - 0
D17.1/Program.cs

@@ -0,0 +1,98 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace D17._1
+{
+    public class ContainerList
+    {
+        public int Id { get; }
+        public int Volume;
+        public ContainerList Next;
+        public ContainerList(int id) => Id = id;
+
+        public bool Contains(int id)
+        {
+            var cur = this;
+            bool found = false;
+            while (cur != null && found == false)
+            {
+                if (cur.Id == id)
+                    found = true;
+                cur = cur.Next;
+            }
+            return found;
+        }
+
+        public int Count() => 1 + (Next?.Count() ?? 0);
+
+        public override int GetHashCode() => (Id > 0 ? 1 << Id : 0) + (Next?.GetHashCode() ?? 0);
+
+        public override bool Equals(object obj) => obj is ContainerList list && list.GetHashCode() == GetHashCode();
+    }
+
+    class Program
+    {
+        static void TestCombinations(List<int> containers, HashSet<(ContainerList, int)> found, ContainerList containerComb = null, int totalVolume = 0, int it = 0)
+        {
+            if (found.Contains((containerComb, totalVolume))) return;
+            found.Add((containerComb, totalVolume));
+            if (totalVolume >= 150) return;
+            
+            for (int i = it; i < containers.Count; i++)
+            {
+                if (containerComb != null && containerComb.Contains(i)) continue;
+
+                int volume = containers[i];
+                TestCombinations(containers, found, new ContainerList(i) { Next = containerComb, Volume = volume }, totalVolume + volume, it + 1);
+            }
+        }
+
+        static void Main(string[] args)
+        {
+            List<int> containers = new List<int>();
+            HashSet<(ContainerList, int volume)> found = new HashSet<(ContainerList, int)>();
+
+            ParseFile(args[0], containers);
+
+            TestCombinations(containers, found);
+
+            var validCombinations = found.Where(f => f.volume == 150);
+
+            Console.WriteLine($"The part 1 answer is : {validCombinations.Count()}");
+
+            int[] combn = new int[containers.Count];
+            int mincount = CountAllComb(validCombinations, combn);
+
+            Console.WriteLine($"The part 2 answer is : {combn[mincount]}");
+        }
+
+        private static int CountAllComb(IEnumerable<(ContainerList, int volume)> validCombinations, int[] combn)
+        {
+            int mincount = combn.Length;
+            foreach (var comb in validCombinations)
+            {
+                var count = comb.Item1.Count();
+                combn[count]++;
+                if (count < mincount) mincount = count;
+            }
+
+            return mincount;
+        }
+
+        private static void ParseFile(string arg, List<int> containers)
+        {
+            using (var file = File.OpenText(arg))
+            {
+                while (true)
+                {
+                    var line = file.ReadLine();
+                    if (line == null) break;
+
+                    containers.Add(int.Parse(line));
+                }
+            }
+        }
+    }
+}

+ 8 - 0
D17.1/Properties/launchSettings.json

@@ -0,0 +1,8 @@
+{
+  "profiles": {
+    "D17.1": {
+      "commandName": "Project",
+      "commandLineArgs": "\"..\\..\\..\\..\\D17.1\\input.txt\""
+    }
+  }
+}

+ 20 - 0
D17.1/input.txt

@@ -0,0 +1,20 @@
+33
+14
+18
+20
+45
+35
+16
+35
+1
+13
+18
+13
+50
+44
+48
+6
+24
+41
+30
+42