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

12
src/day3/example.txt Normal file
View File

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

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);

1000
src/day3/input.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
import * as fs from 'fs'
export const readFile = (file: string): any => {
return fs.readFileSync(file, {encoding: "utf-8"}).split("\r\n");
import * as fs from 'fs'
export const readFile = (file: string): any => {
return fs.readFileSync(file, {encoding: "utf-8"}).split("\r\n");
}