d3p1
This commit is contained in:
23
src/day3/index.ts
Normal file
23
src/day3/index.ts
Normal 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);
|
||||
|
||||
Reference in New Issue
Block a user