From 8ec8a12e53b624f805845f8fa793e3860054efbb Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Fri, 27 Sep 2024 12:41:11 +0200 Subject: [PATCH 01/14] Add day15 task --- scripts/aoc2015/day15/input.txt | 4 +++ scripts/aoc2015/day15/puzzle1.ts | 56 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 scripts/aoc2015/day15/input.txt create mode 100644 scripts/aoc2015/day15/puzzle1.ts diff --git a/scripts/aoc2015/day15/input.txt b/scripts/aoc2015/day15/input.txt new file mode 100644 index 0000000..66e1882 --- /dev/null +++ b/scripts/aoc2015/day15/input.txt @@ -0,0 +1,4 @@ +Sugar: capacity 3, durability 0, flavor 0, texture -3, calories 2 +Sprinkles: capacity -3, durability 3, flavor 0, texture 0, calories 9 +Candy: capacity -1, durability 0, flavor 4, texture 0, calories 1 +Chocolate: capacity 0, durability 0, flavor -2, texture 2, calories 8 \ No newline at end of file diff --git a/scripts/aoc2015/day15/puzzle1.ts b/scripts/aoc2015/day15/puzzle1.ts new file mode 100644 index 0000000..c2e6931 --- /dev/null +++ b/scripts/aoc2015/day15/puzzle1.ts @@ -0,0 +1,56 @@ +/* +--- Day 15: Science for Hungry People --- +Today, you set out on the task of perfecting your milk-dunking cookie recipe. +All you have to do is find the right balance of ingredients. + +Your recipe leaves room for exactly 100 teaspoons of ingredients. +You make a list of the remaining ingredients you could use to finish the recipe (your puzzle input) and their properties per teaspoon: + +capacity (how well it helps the cookie absorb milk) +durability (how well it keeps the cookie intact when full of milk) +flavor (how tasty it makes the cookie) +texture (how it improves the feel of the cookie) +calories (how many calories it adds to the cookie) +You can only measure ingredients in whole-teaspoon amounts accurately, and you have to be accurate so you can reproduce your results in the future. The total score of a cookie can be found by adding up each of the properties (negative totals become 0) and then multiplying together everything except calories. + +For instance, suppose you have these two ingredients: + +Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8 +Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3 +Then, choosing to use 44 teaspoons of butterscotch and 56 teaspoons of cinnamon (because the amounts of each ingredient must add up to 100) would result in a cookie with the following properties: + +A capacity of 44*-1 + 56*2 = 68 +A durability of 44*-2 + 56*3 = 80 +A flavor of 44*6 + 56*-2 = 152 +A texture of 44*3 + 56*-1 = 76 +Multiplying these together (68 * 80 * 152 * 76, ignoring calories for now) results in a total score of 62842880, which happens to be the best score possible given these ingredients. If any properties had produced a negative total, it would have instead become zero, causing the whole score to multiply to zero. + +Given the ingredients in your kitchen and their properties, what is the total score of the highest-scoring cookie you can make? + +ALGORITHM + +- read all ingredients from file + - we will need a specific parseIngredient function for that + +- make variations of 100 ingredients from all ingredients + +- add score to each variation + - take score for each of: + capacity + durability + flavor + texture + if one of ingredients is bellow 0, total score is zero + if all ingredients are above 0, total score is multiplying of those properties + +- create a board of results +- add score of each variation to the board +- find one with best score + + + + + + + +*/ From e3706d588a9a6ae287b179c3f51983d02a180e98 Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Fri, 27 Sep 2024 13:15:48 +0200 Subject: [PATCH 02/14] Add an alternative algorithm. --- scripts/aoc2015/day15/puzzle1.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/scripts/aoc2015/day15/puzzle1.ts b/scripts/aoc2015/day15/puzzle1.ts index c2e6931..670379c 100644 --- a/scripts/aoc2015/day15/puzzle1.ts +++ b/scripts/aoc2015/day15/puzzle1.ts @@ -47,6 +47,28 @@ ALGORITHM - add score of each variation to the board - find one with best score +ALGORITHM II + +- read all ingredients from file + - we will need a specific parseIngredient function for that + +- make an iterator, that will provide you with next combination with repetition of 100 ingredients from all ingredients + +- start to iterate, in each iteration, compute score. If score is above zero, add it to the board + +- add score to each variation + - take score for each of: + capacity + durability + flavor + texture + if one of ingredients is bellow 0, total score is zero + if all ingredients are above 0, total score is multiplying of those properties + +- create a board of results +- add score of each variation to the board +- find one with best score + From ec5fcd0e11cfe73ae8ddfe7632dcfa6dd7790b88 Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Fri, 27 Sep 2024 13:48:15 +0200 Subject: [PATCH 03/14] Implement parseIngredientLine --- scripts/aoc2015/day15/simpleInput.txt | 2 ++ scripts/aoc2015/day15/utils.test.ts | 40 +++++++++++++++++++++++++++ scripts/aoc2015/day15/utils.ts | 26 +++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 scripts/aoc2015/day15/simpleInput.txt create mode 100644 scripts/aoc2015/day15/utils.test.ts create mode 100644 scripts/aoc2015/day15/utils.ts diff --git a/scripts/aoc2015/day15/simpleInput.txt b/scripts/aoc2015/day15/simpleInput.txt new file mode 100644 index 0000000..1a7ff25 --- /dev/null +++ b/scripts/aoc2015/day15/simpleInput.txt @@ -0,0 +1,2 @@ +Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8 +Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3 diff --git a/scripts/aoc2015/day15/utils.test.ts b/scripts/aoc2015/day15/utils.test.ts new file mode 100644 index 0000000..0902719 --- /dev/null +++ b/scripts/aoc2015/day15/utils.test.ts @@ -0,0 +1,40 @@ +import { describe, it } from "@std/testing/bdd"; +import { assertEquals } from "@std/assert"; +import { parseIngredientLine } from "./utils.ts"; + +describe("parseIngredientLine", function () { + it("should return null for invalid line", function () { + assertEquals(parseIngredientLine("abc"), null); + }); + it("should return ingredient for valid line", function () { + assertEquals( + parseIngredientLine( + "Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8", + ), + { + name: "Butterscotch", + capacity: -1, + durability: -2, + flavor: 6, + texture: 3, + calories: 8, + }, + ); + }); + + it("should return ingredient for more complex valid line", function () { + assertEquals( + parseIngredientLine( + "Butterscotch: capacity -19, durability -2, flavor 64, texture 3, calories 8", + ), + { + name: "Butterscotch", + capacity: -19, + durability: -2, + flavor: 64, + texture: 3, + calories: 8, + }, + ); + }); +}); diff --git a/scripts/aoc2015/day15/utils.ts b/scripts/aoc2015/day15/utils.ts new file mode 100644 index 0000000..eaa4a90 --- /dev/null +++ b/scripts/aoc2015/day15/utils.ts @@ -0,0 +1,26 @@ +export type Ingredient = { + name: string; + capacity: number; + durability: number; + flavor: number; + texture: number; + calories: number; +}; + +const ingredientRegex = + /(\w+): capacity (-?\d+), durability (-?\d+), flavor (-?\d+), texture (-?\d+), calories (-?\d+)/; + +export function parseIngredientLine(line: string): Ingredient | null { + const match = line.match(ingredientRegex); + if (match) { + return { + name: match[1], + capacity: parseInt(match[2], 10), + durability: parseInt(match[3], 10), + flavor: parseInt(match[4], 10), + texture: parseInt(match[5], 10), + calories: parseInt(match[6], 10), + }; + } + return null; +} From 0a7256425b182dc8da1fb178a2faecba483c2d59 Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Fri, 27 Sep 2024 14:25:59 +0200 Subject: [PATCH 04/14] Implement computeScore --- scripts/aoc2015/day15/utils.test.ts | 49 +++++++++++++++++++++++++++-- scripts/aoc2015/day15/utils.ts | 47 +++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/scripts/aoc2015/day15/utils.test.ts b/scripts/aoc2015/day15/utils.test.ts index 0902719..30bdf3a 100644 --- a/scripts/aoc2015/day15/utils.test.ts +++ b/scripts/aoc2015/day15/utils.test.ts @@ -1,6 +1,6 @@ import { describe, it } from "@std/testing/bdd"; -import { assertEquals } from "@std/assert"; -import { parseIngredientLine } from "./utils.ts"; +import { assertEquals, assertThrows } from "@std/assert"; +import { computeScore, parseIngredientLine } from "./utils.ts"; describe("parseIngredientLine", function () { it("should return null for invalid line", function () { @@ -38,3 +38,48 @@ describe("parseIngredientLine", function () { ); }); }); + +describe("computeScore", function () { + const ingredients = { + Butterscotch: { + name: "Butterscotch", + capacity: -1, + durability: -2, + flavor: 6, + texture: 3, + calories: 8, + }, + Cinnamon: { + name: "Cinnamon", + capacity: 2, + durability: 3, + flavor: -2, + texture: -1, + calories: 3, + }, + }; + it("should throw error, when ingredient is not found", function () { + assertThrows(() => + computeScore({ + Butterscotch: 1, + }, {}) + ); + }); + it("should return 0, when an property for combination of ingredients is negative", function () { + assertEquals( + computeScore({ + Butterscotch: 1, + }, ingredients), + 0, + ); + }); + it("should return a value for combination of ingredients", function () { + assertEquals( + computeScore({ + Butterscotch: 44, + Cinnamon: 56, + }, ingredients), + 62842880, + ); + }); +}); diff --git a/scripts/aoc2015/day15/utils.ts b/scripts/aoc2015/day15/utils.ts index eaa4a90..7cbdf5b 100644 --- a/scripts/aoc2015/day15/utils.ts +++ b/scripts/aoc2015/day15/utils.ts @@ -24,3 +24,50 @@ export function parseIngredientLine(line: string): Ingredient | null { } return null; } + +type RecipeProperties = { + capacity: number; + durability: number; + flavor: number; + texture: number; +}; + +export function computeScore( + combination: Record, + ingredients: Record, +): number { + const ingredientNames = Object.keys(combination); + ingredientNames.forEach((name) => { + if (!ingredients[name]) { + throw new Error("Missing ingredient " + name); + } + }); + const recipeProperties: RecipeProperties = { + capacity: 0, + durability: 0, + flavor: 0, + texture: 0, + }; + for ( + const property of [ + "capacity", + "durability", + "flavor", + "texture", + ] + ) { + let value = 0; + for (const [name, spoonsCount] of Object.entries(combination)) { + const ingredient = ingredients[name]; + value = value + spoonsCount * (ingredient[ + property as keyof RecipeProperties + ]); + } + if (value <= 0) { + return 0; + } + recipeProperties[property as keyof RecipeProperties] = value; + } + return recipeProperties.flavor * recipeProperties.texture * + recipeProperties.capacity * recipeProperties.durability; +} From f049f7fc150472d17fd72ba4a4acdc98db5daf38 Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Mon, 30 Sep 2024 09:35:48 +0200 Subject: [PATCH 05/14] Start implementing generateCombinationWithRepetition --- scripts/aoc2015/day15/utils.test.ts | 34 ++++++++++++++++++++++++++++- scripts/aoc2015/day15/utils.ts | 31 ++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/scripts/aoc2015/day15/utils.test.ts b/scripts/aoc2015/day15/utils.test.ts index 30bdf3a..d2cb630 100644 --- a/scripts/aoc2015/day15/utils.test.ts +++ b/scripts/aoc2015/day15/utils.test.ts @@ -1,6 +1,10 @@ import { describe, it } from "@std/testing/bdd"; import { assertEquals, assertThrows } from "@std/assert"; -import { computeScore, parseIngredientLine } from "./utils.ts"; +import { + computeScore, + generateCombinationWithRepetition, + parseIngredientLine, +} from "./utils.ts"; describe("parseIngredientLine", function () { it("should return null for invalid line", function () { @@ -83,3 +87,31 @@ describe("computeScore", function () { ); }); }); + +describe("generateCombinationWithRepetition", function () { + it("should provide 1 result for n = 0", function () { + const generator = generateCombinationWithRepetition( + ["blue", "brown", "black"], + 0, + ); + const result = generator.next(); + assertEquals(result.done, true); + assertEquals(result.value, { + blue: 0, + brown: 0, + black: 0, + }); + }); + + it("should provide 1 result for names with lenght 1", function () { + const generator = generateCombinationWithRepetition( + ["blue"], + 100, + ); + const result = generator.next(); + assertEquals(result.done, true); + assertEquals(result.value, { + blue: 100, + }); + }); +}); diff --git a/scripts/aoc2015/day15/utils.ts b/scripts/aoc2015/day15/utils.ts index 7cbdf5b..5ce8e65 100644 --- a/scripts/aoc2015/day15/utils.ts +++ b/scripts/aoc2015/day15/utils.ts @@ -71,3 +71,34 @@ export function computeScore( return recipeProperties.flavor * recipeProperties.texture * recipeProperties.capacity * recipeProperties.durability; } + +export function* generateCombinationWithRepetition( + names: string[], + n: number, // number of item types, you can choose from +): Generator<{ + [name: string]: number; +}> { + // if n = 0 + // return { name[0]: 0, name[1]: 0, name[2]: 0, ...} + // if names length === 1 + // return {[names[0]]: n} + + // solve for count of names 2 + + // run loop for i = 0 to n - 1 + // if i !== n -1 + // yield {name[0] = i; name[1] = n - i} + // if i === n -1 + // return {name[0] = i; name[1] = n - i} + + // solve for count of names 3+ + // take one name X and iterate loop for i = 0 to n + // if i !== n - 1 + // for (const partialCombination of (generateCombinationWithRepetition( {combinations without X}, n - 1 - i))) + // yield {X: i, ...partialCombination} + // if i === n - 1 + // + // return {X: n - 1, ... + // generateCombinationWithRepetition( {combinations without X field}, 0).next().value + // } +} From 641a9a14bf6e0d5a8f335084c0015557243be638 Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Mon, 30 Sep 2024 09:40:19 +0200 Subject: [PATCH 06/14] Solve simple 2 cases --- scripts/aoc2015/day15/utils.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/scripts/aoc2015/day15/utils.ts b/scripts/aoc2015/day15/utils.ts index 5ce8e65..aabf6ff 100644 --- a/scripts/aoc2015/day15/utils.ts +++ b/scripts/aoc2015/day15/utils.ts @@ -79,9 +79,20 @@ export function* generateCombinationWithRepetition( [name: string]: number; }> { // if n = 0 - // return { name[0]: 0, name[1]: 0, name[2]: 0, ...} + if (n === 0) { + // return { name[0]: 0, name[1]: 0, name[2]: 0, ...} + return names.reduce((acc: { + [name: string]: number; + }, name) => ({ + ...acc, + [name]: 0, + }), {}); + } // if names length === 1 - // return {[names[0]]: n} + if (names.length === 1) { + // return {[names[0]]: n} + return { [names[0]]: n }; + } // solve for count of names 2 From be0831ce012aff750614cfd5b92668fd3f315229 Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Mon, 30 Sep 2024 09:45:07 +0200 Subject: [PATCH 07/14] Add test for names count 2. --- scripts/aoc2015/day15/utils.test.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/scripts/aoc2015/day15/utils.test.ts b/scripts/aoc2015/day15/utils.test.ts index d2cb630..96c05d6 100644 --- a/scripts/aoc2015/day15/utils.test.ts +++ b/scripts/aoc2015/day15/utils.test.ts @@ -103,7 +103,7 @@ describe("generateCombinationWithRepetition", function () { }); }); - it("should provide 1 result for names with lenght 1", function () { + it("should provide 1 result for names with length 1", function () { const generator = generateCombinationWithRepetition( ["blue"], 100, @@ -114,4 +114,21 @@ describe("generateCombinationWithRepetition", function () { blue: 100, }); }); + + it("should provide 1 result for names with length 2", function () { + const expectedResults = [{ + blue: 0, + red: 2, + }, { + blue: 1, + red: 1, + }, { + blue: 2, + red: 0, + }]; + assertEquals([...generateCombinationWithRepetition( + ["blue", "red"], + 2, + )], expectedResults); + }); }); From d06ccb35fb2745906152961ac578eb47fe1dc946 Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Mon, 30 Sep 2024 10:21:05 +0200 Subject: [PATCH 08/14] Solve for count 2 --- scripts/aoc2015/day15/utils.test.ts | 4 +--- scripts/aoc2015/day15/utils.ts | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/scripts/aoc2015/day15/utils.test.ts b/scripts/aoc2015/day15/utils.test.ts index 96c05d6..fbe4dc0 100644 --- a/scripts/aoc2015/day15/utils.test.ts +++ b/scripts/aoc2015/day15/utils.test.ts @@ -95,7 +95,6 @@ describe("generateCombinationWithRepetition", function () { 0, ); const result = generator.next(); - assertEquals(result.done, true); assertEquals(result.value, { blue: 0, brown: 0, @@ -109,13 +108,12 @@ describe("generateCombinationWithRepetition", function () { 100, ); const result = generator.next(); - assertEquals(result.done, true); assertEquals(result.value, { blue: 100, }); }); - it("should provide 1 result for names with length 2", function () { + it("should provide 3 results for names with length 2", function () { const expectedResults = [{ blue: 0, red: 2, diff --git a/scripts/aoc2015/day15/utils.ts b/scripts/aoc2015/day15/utils.ts index aabf6ff..4e9aa18 100644 --- a/scripts/aoc2015/day15/utils.ts +++ b/scripts/aoc2015/day15/utils.ts @@ -80,8 +80,8 @@ export function* generateCombinationWithRepetition( }> { // if n = 0 if (n === 0) { - // return { name[0]: 0, name[1]: 0, name[2]: 0, ...} - return names.reduce((acc: { + // yield { name[0]: 0, name[1]: 0, name[2]: 0, ...} + yield names.reduce((acc: { [name: string]: number; }, name) => ({ ...acc, @@ -90,17 +90,23 @@ export function* generateCombinationWithRepetition( } // if names length === 1 if (names.length === 1) { - // return {[names[0]]: n} - return { [names[0]]: n }; + // yield {[names[0]]: n} + yield { [names[0]]: n }; } // solve for count of names 2 + if (names.length === 2) { + // run loop for i = 0 to n + for (let i = 0; i <= n; i++) { + // yield {names[0] = i; names[1] = n - i} + yield { + [names[0]]: i, + [names[1]]: n - i, + }; + } + } - // run loop for i = 0 to n - 1 - // if i !== n -1 - // yield {name[0] = i; name[1] = n - i} - // if i === n -1 - // return {name[0] = i; name[1] = n - i} + // throw (new Error("Not implemented")); // solve for count of names 3+ // take one name X and iterate loop for i = 0 to n From 0fabf610f08179136a206a87255d6f2de5cffc66 Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Mon, 30 Sep 2024 10:33:03 +0200 Subject: [PATCH 09/14] Solve for count 3 --- scripts/aoc2015/day15/utils.test.ts | 32 ++++++++++++++++++++++++++++ scripts/aoc2015/day15/utils.ts | 33 +++++++++++++++++++---------- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/scripts/aoc2015/day15/utils.test.ts b/scripts/aoc2015/day15/utils.test.ts index fbe4dc0..15e7bc0 100644 --- a/scripts/aoc2015/day15/utils.test.ts +++ b/scripts/aoc2015/day15/utils.test.ts @@ -129,4 +129,36 @@ describe("generateCombinationWithRepetition", function () { 2, )], expectedResults); }); + + it("should provide 6 results for names with length 3", function () { + const expectedResults = [{ + blue: 0, + red: 0, + green: 2, + }, { + blue: 0, + red: 1, + green: 1, + }, { + blue: 0, + red: 2, + green: 0, + }, { + blue: 1, + red: 0, + green: 1, + }, { + blue: 1, + red: 1, + green: 0, + }, { + blue: 2, + red: 0, + green: 0, + }]; + assertEquals([...generateCombinationWithRepetition( + ["blue", "red", "green"], + 2, + )], expectedResults); + }); }); diff --git a/scripts/aoc2015/day15/utils.ts b/scripts/aoc2015/day15/utils.ts index 4e9aa18..1876f61 100644 --- a/scripts/aoc2015/day15/utils.ts +++ b/scripts/aoc2015/day15/utils.ts @@ -87,11 +87,13 @@ export function* generateCombinationWithRepetition( ...acc, [name]: 0, }), {}); + return; } // if names length === 1 if (names.length === 1) { // yield {[names[0]]: n} yield { [names[0]]: n }; + return; } // solve for count of names 2 @@ -106,16 +108,25 @@ export function* generateCombinationWithRepetition( } } - // throw (new Error("Not implemented")); - // solve for count of names 3+ - // take one name X and iterate loop for i = 0 to n - // if i !== n - 1 - // for (const partialCombination of (generateCombinationWithRepetition( {combinations without X}, n - 1 - i))) - // yield {X: i, ...partialCombination} - // if i === n - 1 - // - // return {X: n - 1, ... - // generateCombinationWithRepetition( {combinations without X field}, 0).next().value - // } + if (names.length === 3) { + const firstName = names[0]; + const restNames = names.slice(1); + // take one name X and iterate loop for i = 0 to n + for (let i = 0; i <= n; i++) { + // for (const partialCombination of (generateCombinationWithRepetition( {combinations without X}, n - 1 - i))) + for ( + const partialCombination of generateCombinationWithRepetition( + restNames, + n - i, + ) + ) { + // yield {X: i, ...partialCombination} + yield { + [firstName]: i, + ...partialCombination, + }; + } + } + } } From 798b1940cb8549b98fd1ad68649d038b06a3c8d1 Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Mon, 30 Sep 2024 10:37:28 +0200 Subject: [PATCH 10/14] Solve for count 3+ --- scripts/aoc2015/day15/utils.test.ts | 58 +++++++++++++++++++++++++++++ scripts/aoc2015/day15/utils.ts | 2 +- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/scripts/aoc2015/day15/utils.test.ts b/scripts/aoc2015/day15/utils.test.ts index 15e7bc0..0018d2b 100644 --- a/scripts/aoc2015/day15/utils.test.ts +++ b/scripts/aoc2015/day15/utils.test.ts @@ -161,4 +161,62 @@ describe("generateCombinationWithRepetition", function () { 2, )], expectedResults); }); + + it("should provide x results for names with length 4", function () { + const expectedResults = [{ + blue: 0, + red: 0, + green: 0, + black: 2, + }, { + blue: 0, + red: 0, + green: 1, + black: 1, + }, { + blue: 0, + red: 0, + green: 2, + black: 0, + }, { + blue: 0, + red: 1, + green: 0, + black: 1, + }, { + blue: 0, + red: 1, + green: 1, + black: 0, + }, { + blue: 0, + red: 2, + green: 0, + black: 0, + }, { + blue: 1, + red: 0, + green: 0, + black: 1, + }, { + blue: 1, + red: 0, + green: 1, + black: 0, + }, { + blue: 1, + red: 1, + green: 0, + black: 0, + }, { + blue: 2, + red: 0, + green: 0, + black: 0, + }]; + assertEquals([...generateCombinationWithRepetition( + ["blue", "red", "green", "black"], + 2, + )], expectedResults); + }); }); diff --git a/scripts/aoc2015/day15/utils.ts b/scripts/aoc2015/day15/utils.ts index 1876f61..8f8a518 100644 --- a/scripts/aoc2015/day15/utils.ts +++ b/scripts/aoc2015/day15/utils.ts @@ -109,7 +109,7 @@ export function* generateCombinationWithRepetition( } // solve for count of names 3+ - if (names.length === 3) { + if (names.length > 2) { const firstName = names[0]; const restNames = names.slice(1); // take one name X and iterate loop for i = 0 to n From 8da3b87f47c86032529f5a29e8737d39ff263844 Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Mon, 30 Sep 2024 10:56:31 +0200 Subject: [PATCH 11/14] Solve for simpleInput --- scripts/aoc2015/day15/puzzle1.ts | 80 ++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 29 deletions(-) diff --git a/scripts/aoc2015/day15/puzzle1.ts b/scripts/aoc2015/day15/puzzle1.ts index 670379c..39a198c 100644 --- a/scripts/aoc2015/day15/puzzle1.ts +++ b/scripts/aoc2015/day15/puzzle1.ts @@ -46,33 +46,55 @@ ALGORITHM - create a board of results - add score of each variation to the board - find one with best score - -ALGORITHM II - -- read all ingredients from file - - we will need a specific parseIngredient function for that - -- make an iterator, that will provide you with next combination with repetition of 100 ingredients from all ingredients - -- start to iterate, in each iteration, compute score. If score is above zero, add it to the board - -- add score to each variation - - take score for each of: - capacity - durability - flavor - texture - if one of ingredients is bellow 0, total score is zero - if all ingredients are above 0, total score is multiplying of those properties - -- create a board of results -- add score of each variation to the board -- find one with best score - - - - - - - */ + +// ALGORITHM II +// +// +// - read all ingredients from file +// - we will need a specific parseIngredient function for that +import { + computeScore, + generateCombinationWithRepetition, + Ingredient, + parseIngredientLine, +} from "./utils.ts"; + +async function processFile(filename: string): Promise { + const input = await Deno.readTextFileSync(filename); + const ingredients: Ingredient[] = []; + for (const line of input.split("\n")) { + const ingredient = parseIngredientLine(line); + if (ingredient) { + ingredients.push(ingredient); + } + } + console.log("ingredients", ingredients); + // - make an iterator, that will provide you with next combination with repetition of 100 ingredients from all ingredients + const generator = generateCombinationWithRepetition( + ingredients.map((i) => i.name), + 100, + ); + // - create a board of results + const board: number[] = []; + const ingredientsMap = ingredients.reduce( + (acc: Record, ingredient) => ({ + ...acc, + [ingredient.name]: ingredient, + }), + {}, + ); + // - start to iterate, in each iteration, compute score. + for (const combination of generator) { + const scoreForCombination = computeScore(combination, ingredientsMap); + // If score is above zero, add it to the board + if (scoreForCombination) { + board.push(scoreForCombination); + } + } + // - find one with best score + console.log("max score", Math.max(...board)); +} + +// expected score 62842880 +processFile("./simpleInput.txt"); From 6a7a84457b0c0129ed5ac9a3f9513dc1ce8bc83d Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Mon, 30 Sep 2024 10:58:11 +0200 Subject: [PATCH 12/14] Solve for input --- scripts/aoc2015/day15/puzzle1.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/aoc2015/day15/puzzle1.ts b/scripts/aoc2015/day15/puzzle1.ts index 39a198c..e7d56a8 100644 --- a/scripts/aoc2015/day15/puzzle1.ts +++ b/scripts/aoc2015/day15/puzzle1.ts @@ -98,3 +98,4 @@ async function processFile(filename: string): Promise { // expected score 62842880 processFile("./simpleInput.txt"); +processFile("./input.txt"); From 6d9a38188e3398db0d7e180f5e72bf79b72d6bbb Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Mon, 30 Sep 2024 12:42:14 +0200 Subject: [PATCH 13/14] Implement computeCalories --- scripts/aoc2015/day15/utils.test.ts | 31 +++++++++++++++++++++++++++++ scripts/aoc2015/day15/utils.ts | 10 ++++++++++ 2 files changed, 41 insertions(+) diff --git a/scripts/aoc2015/day15/utils.test.ts b/scripts/aoc2015/day15/utils.test.ts index 0018d2b..95c3449 100644 --- a/scripts/aoc2015/day15/utils.test.ts +++ b/scripts/aoc2015/day15/utils.test.ts @@ -1,6 +1,7 @@ import { describe, it } from "@std/testing/bdd"; import { assertEquals, assertThrows } from "@std/assert"; import { + computeCalories, computeScore, generateCombinationWithRepetition, parseIngredientLine, @@ -88,6 +89,36 @@ describe("computeScore", function () { }); }); +describe("computeCalories", function () { + const ingredients = { + Butterscotch: { + name: "Butterscotch", + capacity: -1, + durability: -2, + flavor: 6, + texture: 3, + calories: 8, + }, + Cinnamon: { + name: "Cinnamon", + capacity: 2, + durability: 3, + flavor: -2, + texture: -1, + calories: 3, + }, + }; + it("should compute calories", function () { + assertEquals( + computeCalories({ + [ingredients.Butterscotch.name]: 2, + [ingredients.Cinnamon.name]: 2, + }, ingredients), + 22, + ); + }); +}); + describe("generateCombinationWithRepetition", function () { it("should provide 1 result for n = 0", function () { const generator = generateCombinationWithRepetition( diff --git a/scripts/aoc2015/day15/utils.ts b/scripts/aoc2015/day15/utils.ts index 8f8a518..26c5118 100644 --- a/scripts/aoc2015/day15/utils.ts +++ b/scripts/aoc2015/day15/utils.ts @@ -130,3 +130,13 @@ export function* generateCombinationWithRepetition( } } } + +export function computeCalories( + combination: Record, + ingredients: Record, +): number { + return Object.entries(combination).reduce( + (acc, [name, spoons]) => acc + spoons * ingredients[name].calories, + 0, + ); +} From bf194eefa8673b40f4fa53c4f8ce7da7a2da0a1e Mon Sep 17 00:00:00 2001 From: lukasbicus Date: Mon, 30 Sep 2024 12:46:15 +0200 Subject: [PATCH 14/14] Implement puzzle2 --- scripts/aoc2015/day15/puzzle2.ts | 115 +++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 scripts/aoc2015/day15/puzzle2.ts diff --git a/scripts/aoc2015/day15/puzzle2.ts b/scripts/aoc2015/day15/puzzle2.ts new file mode 100644 index 0000000..07955a0 --- /dev/null +++ b/scripts/aoc2015/day15/puzzle2.ts @@ -0,0 +1,115 @@ +/* +--- Day 15: Science for Hungry People --- +Today, you set out on the task of perfecting your milk-dunking cookie recipe. +All you have to do is find the right balance of ingredients. + +Your recipe leaves room for exactly 100 teaspoons of ingredients. +You make a list of the remaining ingredients you could use to finish the recipe (your puzzle input) and their properties per teaspoon: + +capacity (how well it helps the cookie absorb milk) +durability (how well it keeps the cookie intact when full of milk) +flavor (how tasty it makes the cookie) +texture (how it improves the feel of the cookie) +calories (how many calories it adds to the cookie) +You can only measure ingredients in whole-teaspoon amounts accurately, and you have to be accurate so you can reproduce your results in the future. The total score of a cookie can be found by adding up each of the properties (negative totals become 0) and then multiplying together everything except calories. + +For instance, suppose you have these two ingredients: + +Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8 +Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3 +Then, choosing to use 44 teaspoons of butterscotch and 56 teaspoons of cinnamon (because the amounts of each ingredient must add up to 100) would result in a cookie with the following properties: + +A capacity of 44*-1 + 56*2 = 68 +A durability of 44*-2 + 56*3 = 80 +A flavor of 44*6 + 56*-2 = 152 +A texture of 44*3 + 56*-1 = 76 +Multiplying these together (68 * 80 * 152 * 76, ignoring calories for now) results in a total score of 62842880, which happens to be the best score possible given these ingredients. If any properties had produced a negative total, it would have instead become zero, causing the whole score to multiply to zero. + +Given the ingredients in your kitchen and their properties, what is the total score of the highest-scoring cookie you can make? + +--- Part Two --- +Your cookie recipe becomes wildly popular! +Someone asks if you can make another recipe that has exactly 500 calories per cookie (so they can use it as a meal replacement). Keep the rest of your award-winning process the same (100 teaspoons, same ingredients, same scoring system). + +For example, given the ingredients above, if you had instead selected 40 teaspoons of butterscotch and 60 teaspoons of cinnamon (which still adds to 100), the total calorie count would be 40*8 + 60*3 = 500. The total score would go down, though: only 57600000, the best you can do in such trying circumstances. + +Given the ingredients in your kitchen and their properties, what is the total score of the highest-scoring cookie you can make with a calorie total of 500? + + + +ALGORITHM + +- read all ingredients from file + - we will need a specific parseIngredient function for that + +- make variations of 100 ingredients from all ingredients + +- add score to each variation + - take score for each of: + capacity + durability + flavor + texture + if one of ingredients is bellow 0, total score is zero + if all ingredients are above 0, total score is multiplying of those properties + +- create a board of results +- add score of each variation to the board +- find one with best score +*/ + +// ALGORITHM II +// +// +// - read all ingredients from file +// - we will need a specific parseIngredient function for that +import { + computeCalories, + computeScore, + generateCombinationWithRepetition, + Ingredient, + parseIngredientLine, +} from "./utils.ts"; + +async function processFile(filename: string): Promise { + const input = await Deno.readTextFileSync(filename); + const ingredients: Ingredient[] = []; + for (const line of input.split("\n")) { + const ingredient = parseIngredientLine(line); + if (ingredient) { + ingredients.push(ingredient); + } + } + console.log("ingredients", ingredients); + // - make an iterator, that will provide you with next combination with repetition of 100 ingredients from all ingredients + const generator = generateCombinationWithRepetition( + ingredients.map((i) => i.name), + 100, + ); + // - create a board of results + const board: number[] = []; + const ingredientsMap = ingredients.reduce( + (acc: Record, ingredient) => ({ + ...acc, + [ingredient.name]: ingredient, + }), + {}, + ); + // - start to iterate, in each iteration, compute score. + for (const combination of generator) { + const calories = computeCalories(combination, ingredientsMap); + if (calories === 500) { + const scoreForCombination = computeScore(combination, ingredientsMap); + // If score is above zero, add it to the board + if (scoreForCombination) { + board.push(scoreForCombination); + } + } + } + // - find one with best score + console.log("max score", Math.max(...board)); +} + +// expected score 57600000 +processFile("./simpleInput.txt"); +processFile("./input.txt");