khrisparrales

Day 3 Adventofcode 2021

Dec 3rd, 2021
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Adevntofcode Day 3 2021
  3. //leer datos
  4. var datos = [];
  5. var fs = require("fs");
  6. var data = fs.readFileSync("input3.txt", "utf8");
  7. data = data.trim();
  8. datos = data.split(/\n/);
  9. //convertir los datos a String
  10. for (let i = 0; i < datos.length; i++) {
  11.   let str = datos[i];
  12.   str = String(str);
  13.   datos[i] = str.trim();
  14. }
  15.  
  16. let length = datos.length;
  17.  
  18. length = datos[length - 1].length;
  19.  
  20. function Day3Part1(input) {
  21.   let gr = "";
  22.   let er = "";
  23.  
  24.   for (let j = 0; j < datos[0].length; j++) {
  25.     let count0 = 0;
  26.     let count1 = 0;
  27.     for (let i = 0; i < input.length; i++) {
  28.       let word = input[i];
  29.       if (word[j] == "0") {
  30.         count0++;
  31.       } else {
  32.         count1++;
  33.       }
  34.     }
  35.     if (count0 < count1) {
  36.       er += "0";
  37.       gr += "1";
  38.     } else {
  39.       er += "1";
  40.       gr += "0";
  41.     }
  42.   }
  43.   return { gr, er };
  44. }
  45.  
  46. function binarioToDecimal(binario) {
  47.   return parseInt(binario, 2);
  48. }
  49.  
  50. function getbits(input, position) {
  51.   //limpiar datos de espacios y saltos de linea
  52.  
  53.   let count0 = 0;
  54.   let count1 = 0;
  55.   for (let i = 0; i < input.length; i++) {
  56.     let word = input[i];
  57.     if (word[position] == "0") {
  58.       count0++;
  59.     } else {
  60.       count1++;
  61.     }
  62.   }
  63.  
  64.   if (count0 < count1) {
  65.     return { oxygen: "1", c02: "0" };
  66.   } else {
  67.     return { oxygen: "0", c02: "1" };
  68.   }
  69. }
  70. function getbitsC02(input, position) {
  71.   //limpiar datos de espacios y saltos de linea
  72.  
  73.   let count0 = 0;
  74.   let count1 = 0;
  75.   for (let i = 0; i < input.length; i++) {
  76.     let word = input[i];
  77.     if (word[position] == "0") {
  78.       count0++;
  79.     } else {
  80.       count1++;
  81.     }
  82.   }
  83.  
  84.   if (count0 <= count1) {
  85.     return { oxygen: "1", c02: "0" };
  86.   } else {
  87.     return { oxygen: "0", c02: "1" };
  88.   }
  89. }
  90. //Parte1
  91. console.log(
  92.   binarioToDecimal(Day3Part1(datos).gr),
  93.   binarioToDecimal(Day3Part1(datos).er)
  94. );
  95. //resultado
  96. console.log("Parte 1");
  97. console.log(
  98.   binarioToDecimal(Day3Part1(datos).gr) * binarioToDecimal(Day3Part1(datos).er)
  99. );
  100.  
  101. function oxygen_generator_rating(position, input, value) {
  102.   let arr = [];
  103.   if (input.length > 0) {
  104.     if (position == input[0].length - 1) {
  105.       console.log("final");
  106.       if (input[0][position] == "1") {
  107.         return input[0];
  108.       } else {
  109.         return input[1];
  110.       }
  111.     } else {
  112.       for (let i = 0; i < input.length; i++) {
  113.         let word = input[i];
  114.         if (word[position] == value) {
  115.           arr.push(input[i]);
  116.         }
  117.       }
  118.     }
  119.     return arr;
  120.   } else {
  121.     return arr;
  122.   }
  123. }
  124.  
  125. let a = oxygen_generator_rating(0, datos, getbits(datos, 0).oxygen);
  126.  
  127. for (let i = 1; i < datos[0].length; i++) {
  128.   if (a.length == 2) {
  129.     console.log("stop");
  130.     if (a[0][i] == "1") {
  131.       a = a[0];
  132.     } else {
  133.       a = a[1];
  134.     }
  135.     break;
  136.   } else {
  137.     a = oxygen_generator_rating(i, a, getbits(a, i).oxygen);
  138.   }
  139. }
  140.  
  141. let b = oxygen_generator_rating(0, datos, getbitsC02(datos, 0).c02);
  142.  
  143. for (let i = 1; i < datos[0].length; i++) {
  144.   if (b.length == 2) {
  145.     console.log("stop");
  146.     if (b[0][i] == "0") {
  147.       b = b[0];
  148.     } else {
  149.       b = b[1];
  150.     }
  151.     break;
  152.   } else if (b.length == 1) {
  153.     break;
  154.   } else {
  155.     b = oxygen_generator_rating(i, b, getbitsC02(b, i).c02);
  156.   }
  157. }
  158. //Parte 2
  159. console.log(binarioToDecimal(a), binarioToDecimal(b));
  160. console.log("Parte 2");
  161. console.log(binarioToDecimal(a) * binarioToDecimal(b));
  162.  
Advertisement
Add Comment
Please, Sign In to add comment