Finished day 5
This commit is contained in:
5
AdventOfCode2018/5thDay/.vscode/settings.json
vendored
Normal file
5
AdventOfCode2018/5thDay/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"ostream": "cpp"
|
||||
}
|
||||
}
|
||||
5
AdventOfCode2018/5thDay/challenge1/index.js
Normal file
5
AdventOfCode2018/5thDay/challenge1/index.js
Normal file
@@ -0,0 +1,5 @@
|
||||
const day4 = require('./istolethisfromsomeone');
|
||||
const fs = require('fs');
|
||||
|
||||
console.log('Part 1: ' + day4.part1(fs.readFileSync('input.txt').toString()));
|
||||
console.log('Part 2: ' + day4.part2(fs.readFileSync('input.txt').toString()));
|
||||
44
AdventOfCode2018/5thDay/challenge1/istolethisfromsomeone.js
Normal file
44
AdventOfCode2018/5thDay/challenge1/istolethisfromsomeone.js
Normal file
@@ -0,0 +1,44 @@
|
||||
function part1(data) {
|
||||
const polymer = data.split("");
|
||||
const removeChar = ""; // No removal
|
||||
const stack = collapsePloymer(polymer, removeChar);
|
||||
return stack.length;
|
||||
}
|
||||
|
||||
function collapsePloymer(ploymerChars, removeChar) {
|
||||
const stack = [];
|
||||
for (let i = 0; i < ploymerChars.length; i++) {
|
||||
const char = ploymerChars[i];
|
||||
if (char.toLowerCase() === removeChar.toLowerCase()) {
|
||||
continue;
|
||||
}
|
||||
const last = stack.pop();
|
||||
if (!last) {
|
||||
stack.push(char);
|
||||
continue;
|
||||
}
|
||||
if (last.toLowerCase() === char.toLowerCase() && last !== char) {
|
||||
continue;
|
||||
}
|
||||
stack.push(last);
|
||||
stack.push(char);
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
|
||||
function part2(data) {
|
||||
const polymer = data.split("");
|
||||
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
|
||||
const collapsedPolymers = alphabet.map(letter =>
|
||||
collapsePloymer(polymer, letter)
|
||||
);
|
||||
const shortestCollapse = collapsedPolymers.reduce((longest, curr) =>
|
||||
longest.length < curr.length ? longest : curr
|
||||
);
|
||||
return shortestCollapse.length;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
part1: part1,
|
||||
part2: part2
|
||||
};
|
||||
14
AdventOfCode2018/5thDay/challenge2/challenge.txt
Normal file
14
AdventOfCode2018/5thDay/challenge2/challenge.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
--- Part Two ---
|
||||
Time to improve the polymer.
|
||||
|
||||
One of the unit types is causing problems; it's preventing the polymer from collapsing as much as it should. Your goal is to figure out which unit type is causing the most problems, remove all instances of it (regardless of polarity), fully react the remaining polymer, and measure its length.
|
||||
|
||||
For example, again using the polymer dabAcCaCBAcCcaDA from above:
|
||||
|
||||
Removing all A/a units produces dbcCCBcCcD. Fully reacting this polymer produces dbCBcD, which has length 6.
|
||||
Removing all B/b units produces daAcCaCAcCcaDA. Fully reacting this polymer produces daCAcaDA, which has length 8.
|
||||
Removing all C/c units produces dabAaBAaDA. Fully reacting this polymer produces daDA, which has length 4.
|
||||
Removing all D/d units produces abAcCaCBAcCcaA. Fully reacting this polymer produces abCBAc, which has length 6.
|
||||
In this example, removing all C/c units was best, producing the answer 4.
|
||||
|
||||
What is the length of the shortest polymer you can produce by removing all units of exactly one type and fully reacting the result?
|
||||
1
AdventOfCode2018/5thDay/challenge2/input.txt
Normal file
1
AdventOfCode2018/5thDay/challenge2/input.txt
Normal file
File diff suppressed because one or more lines are too long
0
AdventOfCode2018/5thDay/challenge2/main.cpp
Normal file
0
AdventOfCode2018/5thDay/challenge2/main.cpp
Normal file
Reference in New Issue
Block a user