diff --git a/2022/19/example.txt b/2022/19/example.txt new file mode 100644 index 0000000..236cde4 --- /dev/null +++ b/2022/19/example.txt @@ -0,0 +1,2 @@ +Blueprint 1:Each ore robot costs 4 ore.Each clay robot costs 2 ore.Each obsidian robot costs 3 ore and 14 clay.Each geode robot costs 2 ore and 7 obsidian. +Blueprint 2:Each ore robot costs 2 ore.Each clay robot costs 3 ore.Each obsidian robot costs 3 ore and 8 clay.Each geode robot costs 3 ore and 12 obsidian. diff --git a/2022/19/index.js b/2022/19/index.js new file mode 100644 index 0000000..ea5af48 --- /dev/null +++ b/2022/19/index.js @@ -0,0 +1,105 @@ +const fs = require('fs/promises'); + +const sum = (arr) => arr.reduce((total, num) => total+num, 0); +const arrayOfLength = n => Array.from(Array(n).keys()).map(() => {}); + //minus +const keypress = async () => { + process.stdin.setRawMode(true) + return new Promise(resolve => process.stdin.once('data', data => { + const byteArray = [...data] + if (byteArray.length > 0 && byteArray[0] === 3) { + console.log('^C') + process.exit(1) + } + process.stdin.setRawMode(false) + resolve() + })) +} + +const coords = ([x, y, z]) => `${x},${y},${z}`; +const createPlan = ([id, orO, crO, obrO, obrC, grO, grObs]) => { + return { + id, + botCosts: { + ore: {ore: orO}, + clay: {ore: crO}, + obs: { + ore: obrO, + clay: obrC + }, + geo: { + ore: grO, + obs: grObs + } + }, + maxBotNeed: { + ore: Math.max(orO, crO, obrO, grO), + clay: obrC, + obs: grObs, + geo: Infinity + } + } +}; + +let exhaustedPossibilities = 0; + +const runPlan = ( + plan, + buildNext, + bots = {ore: 1, clay: 0, obs: 0, geo: 0}, + resources = {ore: 0, clay: 0, obs: 0, geo: 0}, + time=24 +) => { + if(time < 1) { + exhaustedPossibilities++; + if(exhaustedPossibilities % 1000000 === 0) { + console.log("Tried ", exhaustedPossibilities, " possible outcomes"); + } + return resources.geo; + } + + const {maxBotNeed, botCosts} = plan; + const nextResources = Object.entries(resources).reduce((next, [type, count]) => { + next[type] = count + bots[type]; + return next; + }, {}); + let nextBots = bots; + const canBuild = buildNext && Object.entries(botCosts[buildNext]).every(([type, needed]) => resources[type] >= needed); + if(canBuild) { + nextBots = {...bots, [buildNext]: bots[buildNext] + 1}; + for(const type in botCosts[buildNext]) { + nextResources[type] -= botCosts[buildNext][type]; + } + buildNext = undefined; + } + if(buildNext) { + return runPlan(plan, buildNext, nextBots, nextResources, time -1); + } + + const outcomes = Object.entries(botCosts).filter(([k]) => k !== "id").map(([type, costs]) => { + for(const key in costs) { + if(!nextBots[key]) { + return 0; + } + } + if(nextBots[type] >= maxBotNeed[type]) { + return 0; + } + + return runPlan(plan, type, nextBots, nextResources, time - 1); + }); + + return Math.max(...outcomes); +}; + +const main = async () => { + const input = (await fs.readFile("./input.txt", 'utf8')).slice(0, -1).split("\n"); + const plans = input.map(p => createPlan(p.match(/\d+/g).map(x => parseInt(x)))); + + const results = plans.map(plan => plan.id * Math.max(runPlan(plan, "ore"), runPlan(plan, "clay"))); + + console.log("Answer: ", sum(results)); +} + + +main(); diff --git a/2022/19/index2.js b/2022/19/index2.js new file mode 100644 index 0000000..c85b2ad --- /dev/null +++ b/2022/19/index2.js @@ -0,0 +1,119 @@ +const fs = require('fs/promises'); + +const sum = (arr) => arr.reduce((total, num) => total+num); +const arrayOfLength = n => Array.from(Array(n).keys()).map(() => {}); + //minus +const keypress = async () => { + process.stdin.setRawMode(true) + return new Promise(resolve => process.stdin.once('data', data => { + const byteArray = [...data] + if (byteArray.length > 0 && byteArray[0] === 3) { + console.log('^C') + process.exit(1) + } + process.stdin.setRawMode(false) + resolve() + })) +} + +const coords = ([x, y, z]) => `${x},${y},${z}`; +const createPlan = ([id, orO, crO, obrO, obrC, grO, grObs]) => { + return { + id, + botCosts: { + ore: {ore: orO}, + clay: {ore: crO}, + obs: { + ore: obrO, + clay: obrC + }, + geo: { + ore: grO, + obs: grObs + } + }, + maxBotNeed: { + ore: Math.max(orO, crO, obrO, grO), + clay: obrC, + obs: grObs, + geo: Infinity + } + } +}; + +let exhaustedPossibilities = 0; + + +const main = async () => { + const input = (await fs.readFile("./input.txt", 'utf8')).slice(0, -1).split("\n").slice(0,3); + const plans = input.map(p => createPlan(p.match(/\d+/g).map(x => parseInt(x)))); + + const results = plans.map(plan => { + let runningBest = 0; + const runPlan = ( + plan, + buildNext, + bots = {ore: 1, clay: 0, obs: 0, geo: 0}, + resources = {ore: 0, clay: 0, obs: 0, geo: 0}, + time=32 + ) => { + if(runningBest < resources.geo) { + runningBest = resources.geo; + } + if(time < 1) { + exhaustedPossibilities++; + if(exhaustedPossibilities % 1000000 === 0) { + console.log("Tried ", exhaustedPossibilities, " possible outcomes"); + } + return resources.geo; + } + if(time * (time - 1) / 2 + resources.geo + bots.geo * time < runningBest) { + return resources.geo; + } + + const {maxBotNeed, botCosts} = plan; + const nextResources = Object.entries(resources).reduce((next, [type, count]) => { + next[type] = count + bots[type]; + return next; + }, {}); + let nextBots = bots; + const canBuild = buildNext && Object.entries(botCosts[buildNext]).every(([type, needed]) => resources[type] >= needed); + if(canBuild) { + nextBots = {...bots, [buildNext]: bots[buildNext] + 1}; + for(const type in botCosts[buildNext]) { + nextResources[type] -= botCosts[buildNext][type]; + } + buildNext = undefined; + } + if(buildNext) { + return runPlan(plan, buildNext, nextBots, nextResources, time -1); + } + + const outcomes = Object.entries(botCosts).filter(([k]) => k !== "id").map(([type, costs]) => { + for(const key in costs) { + if(!nextBots[key]) { + return 0; + } + } + if(nextBots[type] >= maxBotNeed[type]) { + return 0; + } + + return runPlan(plan, type, nextBots, nextResources, time - 1); + }); + + return Math.max(...outcomes); + }; + console.log("Plan: ", plan.id); + const res = Math.max(runPlan(plan, "ore"), runPlan(plan, "clay")); + console.log("Best: ", runningBest); + return res; + }); + + console.log("Results: ", results); + + console.log("Answer: ", results.reduce((prod, n) => prod * n)); +} + + +main(); diff --git a/2022/19/input.txt b/2022/19/input.txt new file mode 100644 index 0000000..dd4b22b --- /dev/null +++ b/2022/19/input.txt @@ -0,0 +1,30 @@ +Blueprint 1: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 8 clay. Each geode robot costs 2 ore and 10 obsidian. +Blueprint 2: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 19 clay. Each geode robot costs 4 ore and 11 obsidian. +Blueprint 3: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 18 clay. Each geode robot costs 4 ore and 9 obsidian. +Blueprint 4: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 4 ore and 15 obsidian. +Blueprint 5: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 18 clay. Each geode robot costs 4 ore and 8 obsidian. +Blueprint 6: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 9 clay. Each geode robot costs 3 ore and 7 obsidian. +Blueprint 7: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 4 ore and 11 obsidian. +Blueprint 8: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 12 clay. Each geode robot costs 3 ore and 15 obsidian. +Blueprint 9: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 12 clay. Each geode robot costs 3 ore and 17 obsidian. +Blueprint 10: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 19 clay. Each geode robot costs 2 ore and 14 obsidian. +Blueprint 11: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 20 clay. Each geode robot costs 3 ore and 18 obsidian. +Blueprint 12: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 15 clay. Each geode robot costs 2 ore and 20 obsidian. +Blueprint 13: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 5 clay. Each geode robot costs 3 ore and 7 obsidian. +Blueprint 14: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 17 clay. Each geode robot costs 2 ore and 13 obsidian. +Blueprint 15: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 20 clay. Each geode robot costs 3 ore and 15 obsidian. +Blueprint 16: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 19 clay. Each geode robot costs 4 ore and 8 obsidian. +Blueprint 17: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 8 clay. Each geode robot costs 3 ore and 20 obsidian. +Blueprint 18: Each ore robot costs 3 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 16 clay. Each geode robot costs 3 ore and 14 obsidian. +Blueprint 19: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 3 ore and 8 obsidian. +Blueprint 20: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 18 clay. Each geode robot costs 4 ore and 20 obsidian. +Blueprint 21: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 4 ore and 15 obsidian. +Blueprint 22: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 18 clay. Each geode robot costs 2 ore and 11 obsidian. +Blueprint 23: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 11 clay. Each geode robot costs 4 ore and 7 obsidian. +Blueprint 24: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 12 clay. Each geode robot costs 3 ore and 8 obsidian. +Blueprint 25: Each ore robot costs 4 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 7 clay. Each geode robot costs 3 ore and 8 obsidian. +Blueprint 26: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 11 clay. Each geode robot costs 2 ore and 19 obsidian. +Blueprint 27: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 14 clay. Each geode robot costs 3 ore and 20 obsidian. +Blueprint 28: Each ore robot costs 2 ore. Each clay robot costs 4 ore. Each obsidian robot costs 2 ore and 16 clay. Each geode robot costs 4 ore and 12 obsidian. +Blueprint 29: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 6 clay. Each geode robot costs 2 ore and 16 obsidian. +Blueprint 30: Each ore robot costs 3 ore. Each clay robot costs 3 ore. Each obsidian robot costs 2 ore and 13 clay. Each geode robot costs 3 ore and 12 obsidian.