Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tpatel committed Dec 6, 2021
1 parent a963361 commit fafa4cb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
39 changes: 39 additions & 0 deletions day06.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const fs = require("fs");

const fishes = fs
.readFileSync("day06.txt", { encoding: "utf-8" }) // read day??.txt content
.replace(/[\r\n]/g, "") // remove all \r characters to avoid issues on Windows
.split(",") // Split on newline
.map(Number); // Parse each string into a number

function part1() {
const queue = Array(9).fill(0);
for (const fish of fishes) {
queue[fish]++;
}
for (let i = 0; i < 80; i++) {
const currentFishes = queue.shift();
queue.push(currentFishes);
queue[6] += currentFishes;
}

console.log(queue.reduce((a, b) => a + b, 0));
}

part1();

function part2() {
const queue = Array(9).fill(0);
for (const fish of fishes) {
queue[fish]++;
}
for (let i = 0; i < 256; i++) {
const currentFishes = queue.shift();
queue.push(currentFishes);
queue[6] += currentFishes;
}

console.log(queue.reduce((a, b) => a + b, 0));
}

part2();
1 change: 1 addition & 0 deletions day06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5,1,1,4,1,1,4,1,1,1,1,1,1,1,1,1,1,1,4,2,1,1,1,3,5,1,1,1,5,4,1,1,1,2,2,1,1,1,2,1,1,1,2,5,2,1,2,2,3,1,1,1,1,1,1,1,1,5,1,1,4,1,1,1,5,4,1,1,3,3,2,1,1,1,5,1,1,4,1,1,5,1,1,5,1,2,3,1,5,1,3,2,1,3,1,1,4,1,1,1,1,2,1,2,1,1,2,1,1,1,4,4,1,5,1,1,3,5,1,1,5,1,4,1,1,1,1,1,1,1,1,1,2,2,3,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,5,1,1,1,1,4,1,1,1,1,4,1,1,1,1,3,1,2,1,2,1,3,1,3,4,1,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,4,1,1,2,2,1,2,4,1,1,3,1,1,1,5,1,3,1,1,1,5,5,1,1,1,1,2,3,4,1,1,1,1,1,1,1,1,1,1,1,1,5,1,4,3,1,1,1,2,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,3,3,1,2,2,1,4,1,5,1,5,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,5,1,1,1,4,3,1,1,4

0 comments on commit fafa4cb

Please sign in to comment.