Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Adevntofcode Day 3 2021
- //leer datos
- var datos = [];
- var fs = require("fs");
- var data = fs.readFileSync("input3.txt", "utf8");
- data = data.trim();
- datos = data.split(/\n/);
- //convertir los datos a String
- for (let i = 0; i < datos.length; i++) {
- let str = datos[i];
- str = String(str);
- datos[i] = str.trim();
- }
- let length = datos.length;
- length = datos[length - 1].length;
- function Day3Part1(input) {
- let gr = "";
- let er = "";
- for (let j = 0; j < datos[0].length; j++) {
- let count0 = 0;
- let count1 = 0;
- for (let i = 0; i < input.length; i++) {
- let word = input[i];
- if (word[j] == "0") {
- count0++;
- } else {
- count1++;
- }
- }
- if (count0 < count1) {
- er += "0";
- gr += "1";
- } else {
- er += "1";
- gr += "0";
- }
- }
- return { gr, er };
- }
- function binarioToDecimal(binario) {
- return parseInt(binario, 2);
- }
- function getbits(input, position) {
- //limpiar datos de espacios y saltos de linea
- let count0 = 0;
- let count1 = 0;
- for (let i = 0; i < input.length; i++) {
- let word = input[i];
- if (word[position] == "0") {
- count0++;
- } else {
- count1++;
- }
- }
- if (count0 < count1) {
- return { oxygen: "1", c02: "0" };
- } else {
- return { oxygen: "0", c02: "1" };
- }
- }
- function getbitsC02(input, position) {
- //limpiar datos de espacios y saltos de linea
- let count0 = 0;
- let count1 = 0;
- for (let i = 0; i < input.length; i++) {
- let word = input[i];
- if (word[position] == "0") {
- count0++;
- } else {
- count1++;
- }
- }
- if (count0 <= count1) {
- return { oxygen: "1", c02: "0" };
- } else {
- return { oxygen: "0", c02: "1" };
- }
- }
- //Parte1
- console.log(
- binarioToDecimal(Day3Part1(datos).gr),
- binarioToDecimal(Day3Part1(datos).er)
- );
- //resultado
- console.log("Parte 1");
- console.log(
- binarioToDecimal(Day3Part1(datos).gr) * binarioToDecimal(Day3Part1(datos).er)
- );
- function oxygen_generator_rating(position, input, value) {
- let arr = [];
- if (input.length > 0) {
- if (position == input[0].length - 1) {
- console.log("final");
- if (input[0][position] == "1") {
- return input[0];
- } else {
- return input[1];
- }
- } else {
- for (let i = 0; i < input.length; i++) {
- let word = input[i];
- if (word[position] == value) {
- arr.push(input[i]);
- }
- }
- }
- return arr;
- } else {
- return arr;
- }
- }
- let a = oxygen_generator_rating(0, datos, getbits(datos, 0).oxygen);
- for (let i = 1; i < datos[0].length; i++) {
- if (a.length == 2) {
- console.log("stop");
- if (a[0][i] == "1") {
- a = a[0];
- } else {
- a = a[1];
- }
- break;
- } else {
- a = oxygen_generator_rating(i, a, getbits(a, i).oxygen);
- }
- }
- let b = oxygen_generator_rating(0, datos, getbitsC02(datos, 0).c02);
- for (let i = 1; i < datos[0].length; i++) {
- if (b.length == 2) {
- console.log("stop");
- if (b[0][i] == "0") {
- b = b[0];
- } else {
- b = b[1];
- }
- break;
- } else if (b.length == 1) {
- break;
- } else {
- b = oxygen_generator_rating(i, b, getbitsC02(b, i).c02);
- }
- }
- //Parte 2
- console.log(binarioToDecimal(a), binarioToDecimal(b));
- console.log("Parte 2");
- console.log(binarioToDecimal(a) * binarioToDecimal(b));
Advertisement
Add Comment
Please, Sign In to add comment