Skip to content

Instantly share code, notes, and snippets.

@sreetamdas
Last active December 9, 2020 06:07
Show Gist options
  • Save sreetamdas/a094dc7cc17b75dc7563e04240cf5618 to your computer and use it in GitHub Desktop.
Save sreetamdas/a094dc7cc17b75dc7563e04240cf5618 to your computer and use it in GitHub Desktop.
AdventOfCode2020
const findProduct = (input: number[], target) => {
let product = 0;
input.some((num) => {
const otherNum = target - num;
const otherNumIndex = input.indexOf(otherNum);
if (otherNumIndex > -1) {
product = num * otherNum;
return true;
}
return false;
});
return product;
};
const findTripleSumProduct = (input: number[], target: number) => {
let product = 0;
input.sort().some((a, index) => {
const slicedArr = input.slice(index + 1);
product = a * findProduct(slicedArr, target - a);
return product;
});
return product;
};
// https://codesandbox.io/s/ecstatic-sinoussi-tgmi1?file=/src/index.ts
const isValid = (str: string, char: string, min: number, max: number) => {
const letterFrequency = (str.match(new RegExp(char, "gi")) || []).length;
return letterFrequency >= min && letterFrequency <= max;
};
const isValidPartTwo = (
str: string,
char: string,
first: number,
second: number
) => {
const res1 = str[first - 1] === char;
const res2 = str[second - 1] === char;
if (res1 && res2) return false;
if (!res1 && !res2) return false;
return true;
};
const getValidPaswords = (input: string[]) => {
let validPasswords = 0;
let validPasswordsPartTwo = 0;
input.forEach((line) => {
const [counts, letter, password] = line.split(" ");
const [start, end] = counts.split("-");
if (isValid(password, letter.slice(0, -1), Number(start), Number(end)))
validPasswords += 1;
if (
isValidPartTwo(
password,
letter.slice(0, -1),
Number(start),
Number(end)
)
)
validPasswordsPartTwo += 1;
});
return [validPasswords, validPasswordsPartTwo];
};
// https://codesandbox.io/s/agitated-mcnulty-vsohb?file=/src/index.ts
const moveSquares = (
count: [number, number],
matrix: string[],
curr: [number, number],
[x, y]: [number, number]
): [boolean, [number, number]] => {
const newPos: [number, number] = [
Math.floor(curr[0] + count[0]),
curr[1] + count[1]
];
if (newPos[0] >= x) newPos[0] %= x;
const isTree = matrix[newPos[1]][newPos[0]] === "#";
return [isTree, newPos];
};
const countTrees = (input: string) => {
const rows = input.split("\n");
const width = rows[0].length,
height = rows.length;
let current: [number, number] = [0, 0];
let treeCount = 0;
while (current[1] < height - 2) {
const [isTree, newPos] = moveSquares([3, 1], rows, current, [
width,
height
]);
if (isTree) {
treeCount += 1;
}
current = newPos;
}
return treeCount;
};
// https://codesandbox.io/s/brave-faraday-7wm9z?file=/src/index.ts
const REQUIRED_FIELDS = [
"byr",
"iyr",
"eyr",
"hgt",
"hcl",
"ecl",
"pid"
// "cid"
];
const replaceAtIndex = (s: string, x: string, index: number) => {
return s.substring(0, index) + x + s.substring(index + 1);
};
const checkIfValidValue = (index: number, value: string) => {
if (index === 0) {
return (
value.match(/^\d{4}$/) &&
Number(value) >= 1920 &&
Number(value) <= 2002
);
}
if (index === 1) {
return (
value.match(/^\d{4}$/) &&
Number(value) >= 2010 &&
Number(value) <= 2020
);
}
if (index === 2) {
return (
value.match(/^\d{4}$/) &&
Number(value) >= 2020 &&
Number(value) <= 2030
);
}
if (index === 3) {
if (value.slice(-2) === "cm")
return (
Number(value.slice(0, -2)) >= 150 &&
Number(value.slice(0, -2)) <= 193
);
if (value.slice(-2) === "in")
return (
Number(value.slice(0, -2)) >= 59 &&
Number(value.slice(0, -2)) <= 76
);
return false;
}
if (index === 4) {
return value[0] === "#" && value.slice(1).match(/^[a-f0-9]{6}$/);
}
if (index === 5) {
return ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"].includes(
value
);
}
if (index === 6) {
return value.match(/^\d{9}$/);
}
};
const checkIsValid = (input: string) => {
const fields: string[] = input.replaceAll("\n", " ").split(" ");
if (fields.length < REQUIRED_FIELDS.length) return false;
let fieldPresentMap = "00000000";
fields.forEach((fieldEntry) => {
const key = fieldEntry.split(":")[0];
const index = REQUIRED_FIELDS.indexOf(key);
if (index > -1 && !checkIfValidValue(index, fieldEntry.split(":")[1]))
return false;
if (index > -1) {
const toReplace = (Number(fieldPresentMap[index]) + 1).toString();
fieldPresentMap = replaceAtIndex(fieldPresentMap, toReplace, index);
} else return false;
});
if (fieldPresentMap.slice(0, -1).indexOf("0") > -1) return false;
return true;
};
const getValidPasspors = (input: string) => {
let count = 0;
const entries = input.split("\n\n");
console.log(entries.length);
entries.forEach((entry) => {
if (checkIsValid(entry)) {
count += 1;
}
});
return count;
};
// https://codesandbox.io/s/dazzling-galois-vjiw0?file=/src/index.ts
const calculateSeat = (input: string) => {
const row = parseInt(
input.slice(0, 7).replaceAll("F", "0").replaceAll("B", "1"),
2
);
const col = parseInt(
input.slice(7).replaceAll("L", "0").replaceAll("R", "1"),
2
);
return [row, col];
};
const getSeatID = (input: string[]) => {
let highestSeatID = 0,
seatIDs: number[] = [],
lastID = 0;
input.forEach((line) => {
const [row, col] = calculateSeat(line);
const seatID = row * 8 + col;
seatIDs.push(seatID);
if (seatID > highestSeatID) highestSeatID = seatID;
});
seatIDs
.sort((a, b) => a - b)
.forEach((seat) => {
if (seat === lastID + 2) {
console.log(`Part two: ${seat - 1}`);
}
lastID = seat;
});
return `Part one: ${highestSeatID}`;
};
// https://codesandbox.io/s/compassionate-dream-q6s5i?file=/src/index.ts
const second = (input: string[]) => {
let set = new Set(input[0]);
input.forEach((_person, i) => {
if (i < input.length - 1)
set = new Set([...set].filter((a) => new Set(input[i + 1]).has(a)));
});
return set.size;
};
const getCount = (input: string) => {
const normalized = input.replaceAll("\n", "").replace(/(.)(?=.*\1)/g, "");
return [...normalized].length;
};
const calculate = (input: string) => {
let total = 0,
total2 = 0;
const groups = input.split("\n\n");
groups.forEach((group) => {
const count = getCount(group);
total += count;
const count2 = second(
group.split("\n").filter((person) => person.length <= 26)
);
total2 += count2;
});
return [total, total2];
};
// https://codesandbox.io/s/magical-hermann-3hnmv?file=/src/index.ts
const OURS = "shiny gold";
const createMap = (input: string[]) => {
const map: Object = input.reduce((map, rule) => {
const [outer, inner] = rule
.replaceAll(".", "")
.replaceAll("bags", "")
.replaceAll("bag", "")
.trim()
.split("contain");
const innerBags: string[] = inner
.split(",")
.map((entry) => entry.trim());
// console.log(innerBags);
return { ...map, [outer.trim()]: innerBags };
}, {});
return map;
};
const findBags = (input: string, allBags: Object, alreadyDone = []) => {
let count = 0;
Object.keys(allBags).forEach((outer) => {
// console.log(outer, allBags[outer]);
if (allBags[outer].indexOf(input) > -1) {
if (alreadyDone && alreadyDone.includes(outer)) return 0;
count += 1;
alreadyDone.push(outer);
// other bags
const otherCount = findBags(outer, allBags, alreadyDone);
count += otherCount;
}
});
// console.log(count);
return count;
};
const countInnerBags = (bags: Object, bag: string) => {
if (!bags[bag]) {
return 0;
}
let innerBags = 0;
for (const innerBag of bags[bag]) {
innerBags +=
innerBag[0] + innerBag[0] * countInnerBags(bags, innerBag[1]);
}
return innerBags;
};
const get = (input: string) => {
const rules = input.split("\n");
const map = createMap(rules);
const res1 = findBags(OURS, map);
let bags = {};
for (const line of rules) {
let innerBagTypes = line.replace(/.*?bags/, "").split(",");
innerBagTypes = innerBagTypes.map((x) => [
Number(x.replace(/[^\d]+/g, "")),
x
.replace(/(bags|bag)/, "")
.replace(/.*\d /, "")
.replace(/[^a-zA-Z ]/g, "")
.trim()
]);
bags[line.replace(/bags.*/, "").trim()] = innerBagTypes;
}
return countInnerBags(bags, OURS);
};
// https://codesandbox.io/s/jovial-firefly-h5cnj?file=/src/index.ts
const checkMap = (map: string) => {
return map.split("").every((int) => int === "0" || int === "1");
};
const updateMap = (str: string, index: number) => {
return str.substr(0, index) + `${+str[index] + 1}` + str.substr(index + 1);
};
const getMap = (length: number) => {
return Array.from(Array(length), () => "0").join("");
};
const makeMove = (move: string, counter: number): [number, number] => {
const [type, count_] = move.split(" ");
const sign = count_[0] === "-" ? -1 : 1;
const count = +count_.slice(1) * sign;
// console.log(type, count);
if (type === "nop") return [counter, 1];
if (type === "acc") return [counter + count, 1];
if (type === "jmp") return [counter, count];
return [counter, count];
};
const get = (input: string) => {
const instructions = input.split("\n");
let map = getMap(instructions.length);
let counter = 0,
i = 0,
last = 0;
while (checkMap(map)) {
last = counter;
const move = instructions[i];
const [counter_, index] = makeMove(move, counter);
counter = counter_;
map = updateMap(map, i);
i += index;
}
return last;
};
// https://codesandbox.io/s/friendly-antonelli-d7y2o?file=/src/index.ts
const get = (input) => {
const instructions = input.split("\n").map((line) => {
const [, operation, value] = /(\w+) ([+-]\d+)/.exec(line.trim());
return [operation, parseInt(value)];
});
for (let i = 0; i < instructions.length; i++) {
const patched = instructions.map(([operation, value]) => [
operation,
value
]);
if (instructions[i][0] === "jmp") {
patched[i][0] = "nop";
} else if (instructions[i][0] === "nop") {
patched[i][0] = "jmp";
} else {
continue;
}
const executed = new Set();
let acc = 0;
let p = 0;
while (!executed.has(p)) {
executed.add(p);
const [operation, argument] = patched[p];
switch (operation) {
case "acc":
acc += argument;
p++;
break;
case "jmp":
p += argument;
break;
case "nop":
p++;
break;
}
if (p >= patched.length) {
return acc;
}
}
}
};
// https://codesandbox.io/s/wild-microservice-sqrq5?file=/src/index.ts
const PREV = 5;
const findContiguousSumRange = (num: number, data: number[]) => {
let l = -1;
for (let m = 0; m < data.length; m++) {
l++;
for (let i = 3; i < data.length - 3; i++) {
let j = data.slice(m, l + i);
let el = j.reduce((a, b) => a + b);
if (el === num) {
console.log(Math.min(...j), Math.max(...j));
i = data.length;
} else if (el > num) {
i = data.length;
}
}
}
};
const checkIfSumOf = (num: number, input: number[]) => {
const res = input.some((str) => {
if (input.indexOf(num - str) > -1) return true;
return false;
});
return res;
};
const get = (input: string) => {
const lines = input.split("\n").map(Number);
lines.slice(PREV).forEach((num, index) => {
const check = checkIfSumOf(num, lines.slice(index, index + PREV));
if (!check) {
console.log(num);
findContiguousSumRange(num, lines.slice(0, index));
}
});
};
// https://codesandbox.io/s/relaxed-shaw-ot4wo?file=/src/index.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment