| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- using System;
- using System.IO;
- using System.Linq;
- namespace D24._1
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 1) throw new ArgumentException();
- if (File.Exists(args[0]) == false) throw new FileNotFoundException();
- int[] presentsWeight = File.ReadAllLines(args[0]).Select(i => int.Parse(i)).ToArray();
- var sum = presentsWeight.Sum();
- var p1 = Test(presentsWeight, sum / 3);
- var p2 = Test(presentsWeight, sum / 4);
- Console.WriteLine($"The part1 answer is : {p1}");
- Console.WriteLine($"The part2 answer is : {p2}");
- }
- static double? Test(int[] presentsWeight, int weight, double quantumE = 1, int totalWeight = 0, int i = 0 )
- {
- if (totalWeight == weight)
- return quantumE;
- if (i >= presentsWeight.Length || totalWeight > weight)
- return null;
- var take = Test(presentsWeight, weight, quantumE * presentsWeight[i], totalWeight + presentsWeight[i], i + 1);
- var ignore = Test(presentsWeight, weight, quantumE, totalWeight, i + 1);
- if (take == null) return ignore;
- if (ignore == null) return take;
- return Math.Min(take.GetValueOrDefault(0), ignore.GetValueOrDefault(0));
- }
- }
- }
|