This commit is contained in:
Yanick Champoux 2021-12-05 11:44:51 -05:00
parent 902e83b8ba
commit 5e38f0bae2
4 changed files with 82 additions and 0 deletions

25
2021/03/part1.mjs Normal file
View File

@ -0,0 +1,25 @@
import fs from "fs-extra";
import fp from "lodash/fp.js";
import _ from "lodash";
export const processFile = async (f) =>
fs
.readFile(f, "utf8")
.then((x) => x.split("\n").filter((x) => x))
.then((lines) =>
lines.map((l) => l.split("").map((x) => parseInt(x)))
);
const mostCommon = (numbers) => (_.sum(numbers) * 2 > numbers.length ? 1 : 0);
export const toDecimal = (numbers) => numbers.reduce((a, x) => a * 2 + x, 0);
export const solution = (entries) => {
const groups = _.zip(...entries).map(mostCommon);
const gamma = toDecimal(groups);
const epsilon = toDecimal(groups.map((x) => 1 - x));
return gamma * epsilon;
};

27
2021/03/part2.mjs Normal file
View File

@ -0,0 +1,27 @@
import fp from "lodash/fp.js";
import _ from "lodash";
import u from "updeep";
import { toDecimal } from "./part1.mjs";
function findNumber(entries, index, most) {
// which number to filter on
const mostSeen =
_.sum(entries.map((e) => e[index])) * 2 >= entries.length ? 1 : 0;
const filter = most ? mostSeen : 1 - mostSeen;
const filtered = entries.filter((e) => e[index] === filter);
if (filtered.length === 1) return filtered[0];
return findNumber(filtered, index + 1, most);
}
export const solution = (entries) => {
const oxyGenerator = toDecimal(findNumber(entries, 0, true));
const scrubber = toDecimal(findNumber(entries, 0, false));
return oxyGenerator * scrubber;
};

12
2021/03/sample Normal file
View File

@ -0,0 +1,12 @@
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010

18
2021/03/test.mjs Normal file
View File

@ -0,0 +1,18 @@
import tap from "tap";
import * as p1 from "./part1.mjs";
import * as p2 from "./part2.mjs";
const sample = p1.processFile("sample");
const input = p1.processFile("input");
tap.test("part1", async (t) => {
t.equal(p1.solution(await sample), 198);
t.equal(p1.solution(await input), 3813416 );
});
tap.test("part2", async (t) => {
t.equal(p2.solution(await sample), 230);
t.equal(p2.solution(await input), 2990784);
});