Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace AdventOfCodeDay15
{
class Program
{
static void Main(string[] args)
{
var ss = new StreamReader("../../input1.txt");
//var ss = new StreamReader("../../input-test.txt");
string line;
var input = new List<string>();
while ((line = ss.ReadLine()) != null)
{
input.Add(line);
}
var regex = new Regex(@"^(?<name>.*): capacity (?<capacity>[\-]?[0-9]+), durability (?<durability>[\-]?[0-9]+), flavor (?<flavor>[\-]?[0-9]+), texture (?<texture>[\-]?[0-9]+), calories (?<calories>[\-]?[0-9]+)$");
var ingredients = new List<Ingredient>();
foreach (var c in input)
{
var m = regex.Matches(c)[0];
var name = m.Groups["name"].Value;
var capacity = long.Parse(m.Groups["capacity"].Value);
var durability = long.Parse(m.Groups["durability"].Value);
var flavor = long.Parse(m.Groups["flavor"].Value);
var texture = long.Parse(m.Groups["texture"].Value);
var calories = long.Parse(m.Groups["calories"].Value);
Console.WriteLine($"{name}, {capacity}, {durability}, {flavor}, {texture}, {calories}");
ingredients.Add(new Ingredient
{
Name = name,
Capacity = capacity,
Durability = durability,
Flavor = flavor,
Texture = texture,
Calories = calories
});
}
var numbers = Enumerable.Range(0, 100).ToList();
var combinations = numbers.Combinations(ingredients.Count).Where(a => a.Sum() == 100).ToList();
long max1 = 0;
long max2 = 0;
ulong j = 0;
Console.WriteLine($"Combination count: {combinations.Count}");
foreach (var c in combinations)
{
var a = c.ToArray();
long capacity = 0;
long durability = 0;
long flavor = 0;
long texture = 0;
long calories = 0;
for (var i = 0; i < ingredients.Count; i++)
{
capacity += ingredients[i].Capacity * a[i];
durability += ingredients[i].Durability * a[i];
flavor += ingredients[i].Flavor * a[i];
texture += ingredients[i].Texture * a[i];
calories += ingredients[i].Calories * a[i];
}
capacity = Math.Max(0, capacity);
durability = Math.Max(0, durability);
flavor = Math.Max(0, flavor);
texture = Math.Max(0, texture);
long v = capacity * durability * flavor * texture;
max1 = Math.Max(v, max1);
if (calories == 500)
{
max2 = Math.Max(v, max2);
}
if (j % 10000 == 0)
{
Console.WriteLine($"{string.Join(",", a)} : {capacity},{durability},{flavor},{texture}");
Console.WriteLine($"{j} : {max1} : {max2}");
}
j++;
}
Console.WriteLine("Max Part 1: " + max1);
Console.WriteLine("Max Part 2: " + max2);
Console.ReadLine();
}
}
public static class Extensions
{
public static IEnumerable<IEnumerable<T>> Combinations<T>(this List<T> elements, int k)
{
return k == 0 ? new[] { new T[0] } :
elements.SelectMany((e, i) =>
elements.Combinations(k - 1).Select(c => (new[] { e }).Concat(c)));
}
}
public class Ingredient
{
public string Name { get; set; }
public long Capacity { get; set; }
public long Durability { get; set; }
public long Flavor { get; set; }
public long Texture { get; set; }
public long Calories { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.