This commit is contained in:
2021-12-03 21:05:54 +01:00
parent 066cf2a37a
commit f188bd0ae7
7 changed files with 1147 additions and 5 deletions

23
src/day3/index.ts Normal file
View File

@@ -0,0 +1,23 @@
import path from 'path';
import { readFile } from "../utils";
let input = readFile(path.resolve(__dirname, 'input.txt'));
const size = input[0].length;
let bucket = Array(size).fill(0).map(x => Object.assign({}, [0, 0]));
input
.map((x: string) => {
x.split('')
.map((c, idx: number) => {
if (c === '0') bucket[idx][0]++; else bucket[idx][1]++
})
})
let gamma = parseInt(bucket.map(x => x[0] < x[1] ? "0" : "1").join(""), 2)
let eps = parseInt(bucket.map(x => x[0] < x[1] ? "1" : "0").join(""), 2)
const power = gamma * eps;
console.log("Part 1: ", power);