Skip to content

Day19 #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Oct 4, 2024
45 changes: 45 additions & 0 deletions scripts/aoc2015/day19/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Al => ThF
Al => ThRnFAr
B => BCa
B => TiB
B => TiRnFAr
Ca => CaCa
Ca => PB
Ca => PRnFAr
Ca => SiRnFYFAr
Ca => SiRnMgAr
Ca => SiTh
F => CaF
F => PMg
F => SiAl
H => CRnAlAr
H => CRnFYFYFAr
H => CRnFYMgAr
H => CRnMgYFAr
H => HCa
H => NRnFYFAr
H => NRnMgAr
H => NTh
H => OB
H => ORnFAr
Mg => BF
Mg => TiMg
N => CRnFAr
N => HSi
O => CRnFYFAr
O => CRnMgAr
O => HP
O => NRnFAr
O => OTi
P => CaP
P => PTi
P => SiRnFAr
Si => CaSi
Th => ThCa
Ti => BP
Ti => TiTi
e => HF
e => NAl
e => OMg

CRnSiRnCaPTiMgYCaPTiRnFArSiThFArCaSiThSiThPBCaCaSiRnSiRnTiTiMgArPBCaPMgYPTiRnFArFArCaSiRnBPMgArPRnCaPTiRnFArCaSiThCaCaFArPBCaCaPTiTiRnFArCaSiRnSiAlYSiThRnFArArCaSiRnBFArCaCaSiRnSiThCaCaCaFYCaPTiBCaSiThCaSiThPMgArSiRnCaPBFYCaCaFArCaCaCaCaSiThCaSiRnPRnFArPBSiThPRnFArSiRnMgArCaFYFArCaSiRnSiAlArTiTiTiTiTiTiTiRnPMgArPTiTiTiBSiRnSiAlArTiTiRnPMgArCaFYBPBPTiRnSiRnMgArSiThCaFArCaSiThFArPRnFArCaSiRnTiBSiThSiRnSiAlYCaFArPRnFArSiThCaFArCaCaSiThCaCaCaSiRnPRnCaFArFYPMgArCaPBCaPBSiRnFYPBCaFArCaSiAl
78 changes: 78 additions & 0 deletions scripts/aoc2015/day19/puzzle1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
--- Day 19: Medicine for Rudolph ---
Rudolph the Red-Nosed Reindeer is sick! His nose isn't shining very brightly, and he needs medicine.

Red-Nosed Reindeer biology isn't similar to regular reindeer biology; Rudolph is going to need custom-made medicine. Unfortunately, Red-Nosed Reindeer chemistry isn't similar to regular reindeer chemistry, either.

The North Pole is equipped with a Red-Nosed Reindeer nuclear fusion/fission plant, capable of constructing any Red-Nosed Reindeer molecule you need.

It works by starting with some input molecule
and then doing a series of replacements,
one per step, until it has the right molecule.

However, the machine has to be calibrated before it can be used.
Calibration involves determining the number of molecules that can be generated in one step from a given starting point.

For example, imagine a simpler machine that supports only the following replacements:

H => HO
H => OH
O => HH

Given the replacements above and starting with HOH, the following molecules could be generated:

HOOH (via H => HO on the first H).
HOHO (via H => HO on the second H).
OHOH (via H => OH on the first H).
HOOH (via H => OH on the second H).
HHHH (via O => HH).

So, in the example above, there are 4 distinct molecules (not five, because HOOH appears twice) after one replacement from HOH.

Santa's favorite molecule, HOHOHO, can become 7 distinct molecules (over nine replacements: six from H, and three from O).

The machine replaces without regard for the surrounding characters.
For example, given the string H2O, the transition H => OO would result in OO2O.

Your puzzle input describes all of the possible replacements

and, at the bottom, the medicine molecule for which you need to calibrate the machine.

How many distinct molecules can be created after all the different ways you can do one replacement on the medicine molecule?
*/

// Algorithm

// read file, get replacements and medicine molecule from it

// create set of distinctMolecules

// loop replacements
// each replacement will provide you string to be replaced (STBR) and new string (NS)
// traverse medicine molecule,
// find each occurrence of STBR and replace it with NS, add new molecule to set of distinct molecules

// get size of set of distinct molecules

import { generateMolecules, parseInput } from "./utils.ts";

async function processFile(filename: string) {
const input = await Deno.readTextFile(filename);
const parsedInput = parseInput(input);
if (!parsedInput) {
return;
}
const { medicineMolecule, replacements } = parsedInput;

const molecules = new Set<string>();
for (const replacement of replacements) {
const newMolecules = generateMolecules(medicineMolecule, replacement);
for (const molecule of newMolecules) {
molecules.add(molecule);
}
}
console.log("distinct molecules", molecules.size);
}

processFile("simpleInput.txt");
processFile("input.txt");
141 changes: 141 additions & 0 deletions scripts/aoc2015/day19/puzzle2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
--- Day 19: Medicine for Rudolph ---
Rudolph the Red-Nosed Reindeer is sick! His nose isn't shining very brightly, and he needs medicine.

Red-Nosed Reindeer biology isn't similar to regular reindeer biology; Rudolph is going to need custom-made medicine. Unfortunately, Red-Nosed Reindeer chemistry isn't similar to regular reindeer chemistry, either.

The North Pole is equipped with a Red-Nosed Reindeer nuclear fusion/fission plant, capable of constructing any Red-Nosed Reindeer molecule you need.

It works by starting with some input molecule
and then doing a series of replacements,
one per step, until it has the right molecule.

However, the machine has to be calibrated before it can be used.
Calibration involves determining the number of molecules that can be generated in one step from a given starting point.

For example, imagine a simpler machine that supports only the following replacements:

H => HO
H => OH
O => HH

Given the replacements above and starting with HOH, the following molecules could be generated:

HOOH (via H => HO on the first H).
HOHO (via H => HO on the second H).
OHOH (via H => OH on the first H).
HOOH (via H => OH on the second H).
HHHH (via O => HH).

So, in the example above, there are 4 distinct molecules (not five, because HOOH appears twice) after one replacement from HOH.

Santa's favorite molecule, HOHOHO, can become 7 distinct molecules (over nine replacements: six from H, and three from O).

The machine replaces without regard for the surrounding characters.
For example, given the string H2O, the transition H => OO would result in OO2O.

Your puzzle input describes all of the possible replacements

and, at the bottom, the medicine molecule for which you need to calibrate the machine.

How many distinct molecules can be created after all the different ways you can do one replacement on the medicine molecule?


--- Part Two ---
Now that the machine is calibrated, you're ready to begin molecule fabrication.

Molecule fabrication always begins with just a single electron, e, and applying replacements one at a time, just like the ones during calibration.

For example, suppose you have the following replacements:

e => H
e => O
H => HO
H => OH
O => HH
If you'd like to make HOH, you start with e, and then make the following replacements:

e => O to get O
O => HH to get HH
H => OH (on the second H) to get HOH
So, you could make HOH after 3 steps. Santa's favorite molecule, HOHOHO, can be made in 6 steps.

How long will it take to make the medicine?
Given the available replacements and the medicine molecule in your puzzle input, what is the fewest number of steps to go from e to the medicine molecule?
*/

// Algorithm

import { parseInput, Replacement, reverseStep } from "./utils.ts";

async function processFile(filename: string) {
const input = await Deno.readTextFile(filename);
const parsedInput = parseInput(input);
if (!parsedInput) {
return;
}
const { medicineMolecule, replacements } = parsedInput;
// get reversed replacements
const reversedReplacements = replacements.reduce(
(acc: Replacement[], replacement) => {
acc.push({
from: replacement.to,
to: replacement.from,
});
return acc;
},
[],
);
const aggressiveReversedReplacements = reversedReplacements.sort((ra, rb) =>
ra.from.length < rb.from.length ? 1 : -1
);

console.log(aggressiveReversedReplacements);

// create set of stepMolecules with medicine molecule
let stepMolecules = new Set<string>([medicineMolecule]);

// reverseStep function
// -> will take stepMolecules and reversedReplacements
// -> will provide set of possiblePrevSteps
// loop reversed replacements
// each reversed replacement will provide you array of prevMolecules
// add prevMolecules to possiblePrevSteps
let stepCount = 0;

do {
stepCount++;
console.log("stepCount", stepCount);
stepMolecules = reverseStep(
stepMolecules,
reversedReplacements,
);

console.log("stepMolecules set", stepMolecules.size);
} while (!stepMolecules.has("e"));

console.log("Step count", stepCount);
}

// processFile("simpleInput.txt");
// processFile("simpleInput2.txt");
processFile("input.txt");

// algorithm 2 for puzzle 2
// sort replacements by "agresivity" => replacement is aggressive, when replacement.from.length > 5
// run first 10 rounds only with aggressive replacements

// algorithm 3 for puzzle 2
// sort replacements by "agresivity" => replacement is aggressive, when replacement.from.length > 5
// run first 10 rounds only with aggressive replacements

// filter each step -> find length of shortest molecule = LOSM
// // sort out all molecules from the step, that are longer than LOSM + 2

// algorithm 4 for puzzle 2
// find length
// on every step add use more and more less aggressive replacements

// algorithm 5 -> WINNER!
// order replacements by most aggresive
// stop adding items to set, when set size is 10000
7 changes: 7 additions & 0 deletions scripts/aoc2015/day19/simpleInput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
e => H
e => O
H => HO
H => OH
O => HH

HOH
7 changes: 7 additions & 0 deletions scripts/aoc2015/day19/simpleInput2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
e => H
e => O
H => HO
H => OH
O => HH

HOHOHO
Loading
Loading