d3p1
This commit is contained in:
12
src/day3/example.txt
Normal file
12
src/day3/example.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
00100
|
||||
11110
|
||||
10110
|
||||
10111
|
||||
10101
|
||||
01111
|
||||
00111
|
||||
11100
|
||||
10000
|
||||
11001
|
||||
00010
|
||||
01010
|
||||
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);
|
||||
|
||||
1000
src/day3/input.txt
Normal file
1000
src/day3/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -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");
|
||||
}
|
||||
Reference in New Issue
Block a user