diff --git a/src/day3/example.txt b/src/day3/example.txt new file mode 100644 index 0000000..76247fd --- /dev/null +++ b/src/day3/example.txt @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 \ No newline at end of file diff --git a/src/day3/index.ts b/src/day3/index.ts new file mode 100644 index 0000000..4062ef5 --- /dev/null +++ b/src/day3/index.ts @@ -0,0 +1,57 @@ +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])); + +const toDecimal = (binary) => parseInt(binary, 2); + + +input.map((x: string) => { + x.split('') + .map((c: string, idx: number) => { + if (c === '0') bucket[idx][0]++; else bucket[idx][1]++ + }) +}) + +const MSB = bucket.map(x => x[0] < x[1] ? "0" : "1").join(""); +const LSB = bucket.map(x => x[0] < x[1] ? "1" : "0").join(""); + +let gamma = toDecimal(MSB) +let eps = toDecimal(LSB) + +const power = gamma * eps; + +console.log("Part 1: ", power); + +const getBitCrit = (array, MSB, eqValue) => { + let tmpArray = [...array]; + + for (const idx in [...Array(size).keys()]) { + + let count = [0, 0]; + let keepMe; + + // Count 0/1 values + tmpArray.forEach((x: string) => { + x[idx] === "0" ? count[0]++ : count[1]++; + }) + + // Swap results if we look for the least common value + if (!MSB) count = [count[1], count[0]] + + // Which value to keep + if (count[0] === count[1]) keepMe = eqValue; + else keepMe = count[0] < count[1] ? '1' : '0' + + // Filter + tmpArray = tmpArray.filter((x: string) => x[idx] === keepMe) + + if (tmpArray.length === 1) return tmpArray[0]; + } +} + +const oxygen = toDecimal(getBitCrit(input, true, "1")); +const co2 = toDecimal(getBitCrit(input, false, "0")); +console.log("Part 2: ", oxygen * co2); \ No newline at end of file diff --git a/src/day3/input.txt b/src/day3/input.txt new file mode 100644 index 0000000..513f2d5 --- /dev/null +++ b/src/day3/input.txt @@ -0,0 +1,1000 @@ +110001010110 +011101111101 +111011110101 +011110101000 +100100011001 +011001001110 +101011000101 +110000111111 +110110101000 +010101101000 +001101001101 +111000001011 +111000011010 +001111001000 +100110001111 +111100110100 +100000010101 +001110100000 +100111110110 +100110000110 +111100101101 +100101010100 +011100010011 +111010110000 +110100011110 +111010101100 +111001001101 +110001110100 +100110111011 +101111111011 +011100111000 +010011110010 +111001011011 +110100111000 +111000011001 +010100000000 +100000101111 +010100110010 +110001101000 +000101101001 +001111101010 +100100000001 +101000111011 +101110001111 +000100011000 +100110101110 +111011101010 +010110001101 +110100001111 +011110101100 +101000011100 +111101111101 +110101010100 +101100111111 +111110110101 +011011110111 +101101000111 +111101000011 +001011110110 +110011110111 +000011101111 +100001101010 +111011101101 +010110111000 +110101111100 +110010101000 +001011001010 +000110101001 +000001000011 +111100001010 +100110111010 +111011111000 +011111101111 +000010100111 +111011011111 +011101110010 +000000100111 +100111000100 +000010010111 +101111101011 +011111000101 +011100100101 +011000111101 +011110001010 +100110101011 +011011010011 +011100000101 +011110000100 +000110110010 +001101101110 +110011000001 +110100111110 +110011000101 +000101011110 +001010000000 +000011000010 +110010111110 +011111110110 +100000110000 +011101111000 +100011100101 +101000000101 +001111010011 +010110110011 +000100100100 +001010001001 +010011100100 +110101110011 +001001000001 +101110110010 +001001000110 +010010100000 +000111101011 +010010001000 +011111000111 +010011010001 +110101101101 +000110010101 +111011110100 +010000110101 +110110101111 +010110111101 +100000010000 +001010011111 +111001001111 +011110110111 +101011000111 +010100011110 +111100100011 +110100110110 +001000011000 +011011111101 +111111110010 +000011110010 +110101111101 +001011111101 +000010011000 +001111101101 +100010010101 +111101110001 +011010110101 +011000000110 +111110100011 +000010111001 +101001111100 +001011011010 +100110111110 +000111001010 +001011101110 +100010110110 +001100110000 +000110001010 +100010001000 +111110011110 +110011111000 +100011101110 +001110100101 +010110110000 +101110101010 +110111010111 +110011000100 +100110101100 +101110100110 +010101011101 +100111100010 +010110110010 +000111100001 +101001101001 +011101110101 +011111100111 +000101010011 +110111100010 +011110000000 +011101110110 +110100110100 +111110111001 +000100000011 +011110111011 +011010100111 +111100110111 +100010011101 +011001001111 +001010101100 +010110111010 +111000110010 +011011100111 +101111000010 +110011011100 +110110011110 +000101011100 +100100110000 +110000100011 +011001101001 +110000100010 +001000000011 +010110001100 +111111000001 +101111011100 +100100100101 +101010100110 +011110000001 +010100110110 +010111011000 +011110100010 +101000110010 +110001011011 +111110011111 +111100100110 +111110101000 +010001111101 +111000101111 +111001010110 +101000001111 +000100101001 +011101011001 +111101111100 +010001100100 +010111011100 +100101010110 +001010110010 +010010111110 +101110011100 +111100101010 +001001111111 +011101010000 +111011111101 +011000001110 +111100110010 +001001111101 +011100001110 +001001001000 +110011101111 +010001011110 +001100100100 +011100110000 +111100001110 +111110110000 +000011000011 +011000000011 +110010111111 +011110001011 +101110100010 +100100111000 +111101011010 +000111110101 +010100001001 +110010101101 +001101011111 +000101100101 +110011101100 +110111111111 +111000011000 +010000100100 +110101101011 +101010101001 +100111001101 +111011000001 +101101111010 +111001111001 +001001110100 +000111110000 +010111010110 +100011010111 +111011011101 +100010100001 +000111001110 +111100010001 +101001101101 +111101100111 +100110100010 +000010100101 +010010110111 +101100000101 +111011010110 +101101101101 +001110110111 +110011011111 +011111111001 +100100001100 +101010011101 +100000110110 +110101010000 +110110110111 +101001110110 +100001000101 +110100001001 +000111000100 +101000101001 +111111001100 +101011000100 +111000010100 +100111000110 +111011010100 +101101011101 +010000101101 +000110110110 +001110000101 +001010001000 +100111101111 +001011101001 +001100111101 +110111011111 +111010011010 +001110101111 +101000001110 +101001110001 +110111010010 +011111100101 +110001001001 +010010111010 +000001001010 +011000001100 +100000111011 +111000010101 +001110011001 +010001001100 +001100010101 +001011011000 +001101100100 +011001111111 +010010001011 +100010011111 +100011000100 +000110011110 +010010000000 +110110011001 +111000111010 +111010000111 +111011110110 +101101100111 +111100101001 +110001101110 +111101100001 +011111110111 +110110101001 +000111010101 +101011111111 +100110001000 +101000100000 +110101110010 +010100011011 +000000100101 +000000011110 +101011110000 +101001100001 +111010101101 +000011011100 +000000100010 +001011100000 +001110111111 +110100111001 +010110110111 +010111111010 +110100011010 +000101011111 +001000011100 +010011001011 +111011001100 +101111001101 +100010000001 +011000001010 +101010000011 +110110010100 +101001011101 +100001010001 +110011010101 +000001100000 +100101000101 +110110100110 +110001111010 +111011101011 +010010111101 +000110100101 +011110101110 +000101110001 +111111101100 +011100110010 +100011111010 +010011111101 +101000010101 +011000101000 +011111001111 +111001111010 +100000000100 +010000101001 +110000001000 +100100001101 +011001111010 +110111110111 +000011100111 +001100110001 +000111010001 +101110011110 +110010000010 +101100001101 +001000001100 +101110011001 +111000000110 +000111111100 +001110001110 +100011110100 +001011101100 +011010010010 +101000011111 +110001011010 +011001000000 +010011110001 +100001111011 +011011001111 +110100010011 +010111010001 +010101011011 +100000001100 +100111101010 +100010100000 +110000110001 +010000010101 +010101000010 +010100101100 +110101000001 +100010010110 +110011101011 +010101011001 +010001000011 +000011111110 +111110011101 +011010010000 +011011111100 +110010110011 +001101111011 +000011111101 +110000010110 +111011011011 +111110000000 +101100111010 +100110011111 +110111011001 +110110010010 +010010011110 +010110010101 +101110000000 +011001110110 +100110100011 +111101111010 +110101010011 +111110100010 +100110001100 +010010100010 +101010100001 +000000000101 +111001011100 +100100111110 +000000011010 +110011111101 +010100000011 +100001111111 +110001011001 +111111100011 +111101110000 +100011001101 +110001010100 +100110000111 +111110110110 +001011000011 +111111000000 +101011101101 +110001111011 +011111001000 +000001011101 +001101000011 +001000010000 +000011001110 +011001101000 +110101100101 +001111110111 +111110111101 +000011000111 +001010011100 +110010010011 +110001110111 +111010010011 +100010001110 +110000101000 +011001011000 +011100100000 +001000011001 +101011110011 +001010011110 +110100000101 +010011011110 +011001100001 +011110011010 +100110000000 +010110010100 +000100111110 +110101110101 +000110111100 +010000000111 +111101010100 +111011011100 +000010000011 +001100100001 +110010110001 +010010000010 +001000010110 +011001000001 +010100101110 +011100010100 +100000011010 +010111100000 +010111011001 +011100101001 +101110101011 +110010011001 +111110001110 +011100011100 +110000101010 +000010100010 +100101101110 +100010111010 +110101100001 +101111000100 +101000111001 +011111101101 +111100111110 +011001001000 +110100110010 +000011010101 +111010111011 +010010011000 +100101000100 +111010100100 +000111001101 +010001011000 +111101101100 +111110001111 +110100011011 +111100010101 +111001111100 +011100111011 +011011101111 +011001000011 +011100111101 +010100100010 +110101010110 +101100001000 +001100101001 +000001001111 +000110010100 +111101101010 +100111000111 +001011000111 +001011111011 +110101011100 +110000110101 +010001000110 +011100101111 +101110010001 +001011001000 +100100110010 +000011111000 +010010010000 +001011100010 +000000110011 +000101011010 +101111110000 +100001010100 +101110110110 +001100000101 +110111001101 +010001010011 +101011001111 +111111111110 +110011010011 +011101000101 +100001101000 +011001101100 +011100010000 +001111111000 +010110011111 +111101110100 +100011011101 +100010110011 +110111011000 +100110010101 +001001000000 +101000100110 +011010011010 +000000000110 +010011001010 +110000110010 +100001011010 +101111001001 +100010101001 +110111111100 +000010111110 +000110101111 +011101001011 +101110011010 +111101010101 +001101100110 +111100001101 +000011100000 +100111010110 +110100100111 +100011100110 +100001011110 +001110110011 +101011010011 +001100001000 +111011001110 +110011100000 +101000011001 +011100100001 +000110001011 +001001001001 +000011011101 +000010111010 +011001100111 +101001011011 +010001101101 +101110001011 +000001011110 +011100110001 +110011111011 +001101010011 +000101000110 +110101000000 +110000101111 +000110110111 +110100101100 +111011110000 +011110011101 +001101000001 +011010001010 +101000111010 +111101111000 +000010000000 +000010001010 +110000100110 +110010100111 +110000001100 +100010000100 +110010100101 +111011101001 +100011110000 +010110110110 +111100011011 +110001101111 +110010101111 +001001001010 +100100000011 +110100100011 +101010110001 +001001011111 +101101110110 +001111010101 +110000010011 +010110001110 +011011011100 +000000110100 +100101110001 +011011001100 +100101000001 +100101101101 +101010001110 +110101100110 +111010111101 +111001000101 +010101111100 +000110000000 +001010111010 +110110000001 +100000101110 +111011110010 +000111000101 +011001110010 +000100101010 +101001010010 +010001100001 +000101100000 +111000101110 +000100010010 +110110000011 +010101001010 +110101101110 +101111101000 +110011100101 +111110110011 +000100010011 +110110010001 +111100000010 +011011111011 +101101101001 +101011011001 +011011011111 +101100110001 +011011100010 +011101010101 +011101110100 +010000101011 +010110011011 +111001101001 +010010010010 +010111000010 +100001101100 +000000101110 +000000110101 +111001000100 +110111001110 +111100111001 +110101010010 +111100011000 +100110100100 +000100000000 +110111101011 +001011110111 +010101101011 +101110001100 +111111101010 +111100010110 +101000110011 +110100010000 +010010001100 +011101111100 +000011110001 +011010101010 +110001101010 +101001000011 +100010000110 +110100111101 +000011110110 +001010100110 +010111010101 +101011100111 +000101001000 +110010001000 +001100000100 +010000111100 +111011111111 +100101001010 +110011100111 +100011100000 +101010011111 +010111100101 +110110110101 +010111010000 +110011001110 +101100111101 +000001100001 +111010100001 +010000010110 +001001000101 +101011110001 +000101101110 +011110000010 +110011110100 +100001000001 +100000011110 +001101100101 +101111101101 +110010010100 +010111101110 +010101110111 +101010101000 +000100000010 +111000001010 +101100001100 +101010000001 +101001101011 +110110000000 +101100101101 +101010110011 +011000001001 +101010111010 +000001001100 +101001000010 +110000001111 +110101001101 +110011111111 +010110101111 +001101110110 +111010001010 +001010100010 +000101101011 +001100010111 +101111100111 +010100000101 +110111110000 +000110111011 +110000011000 +100101010000 +101010111100 +100101000111 +001110100111 +001000111011 +011010010100 +111110010010 +010101010000 +101100010110 +000011001100 +010010101011 +011011000100 +011100111100 +111010111010 +101111010000 +101011010000 +001010100111 +010100111100 +011001000010 +011110110100 +111000001001 +110010110010 +101011111001 +110001111001 +101001000100 +010100100011 +000110110101 +001000100010 +100110110010 +000100010001 +010100010010 +001011000000 +110101011000 +001001101011 +000111101110 +000100110000 +010000010100 +001011011011 +100000001110 +111010110100 +111001101011 +101110110011 +111010111111 +010101010100 +001110111110 +111010110110 +001101001011 +010111101011 +000110100011 +100001010000 +011111001010 +011001001001 +101100001001 +001100111011 +001001110111 +101110110000 +010011010100 +001100001111 +001111100001 +010100010011 +001001000010 +100101001100 +001010001101 +011011001001 +101110110001 +010001110100 +101101011001 +010001111001 +011101001000 +010011100011 +001000111010 +111110100101 +110000010001 +110100000000 +101011101110 +011101001111 +111110001101 +101100010000 +010100010101 +000010001000 +111111010110 +000100010100 +110100010101 +110010000110 +001111010111 +000100101100 +001111100111 +011110001101 +000111001111 +000001000110 +101100100000 +110100010100 +001011010000 +101011100110 +010000101010 +101001000101 +011000010110 +010111110100 +110010001001 +001111010000 +010010100111 +011001010001 +100010100010 +011111010101 +011100000010 +110101110000 +001010110100 +101110111110 +000010110100 +101100000100 +111010110111 +001001101010 +111010010110 +011011001010 +100001010111 +110010011101 +010110100001 +110000111101 +011001110111 +111110001010 +000101101010 +010011101101 +110111010001 +111011011010 +001111111010 +100111001111 +010111111111 +010000010000 +001111110011 +100110100000 +000100101111 +001000010010 +110001010001 +001011100111 +101111111111 +100101101001 +000010011001 +011011001101 +010110100000 +101001101100 +010010001111 +110111001001 +101011001000 +001001010010 +011011001000 +001110010110 +011011101011 +100111111101 +110001001111 +001110101001 +101101111011 +101011011101 +100100011111 +100001001111 +101000001010 +000010011010 +010110010010 +100101110100 +010100010100 +110111001111 +100000101001 +010110101100 +011101010011 +000111000011 +000000101010 +101001110010 +010101001000 +111100000001 +100101000010 +010011011010 +011111110011 +011111010011 +110001000101 +100010101011 +111010011101 +010110010110 +110011101000 +101001000000 +000000011100 +010011100000 +101010111000 +000101110101 +100111111110 +010100100110 +110110110100 +110001001011 +100010111101 +111111111101 +111111110101 +000110100110 +001100110011 +000011100101 +111000110011 +111010100010 +101010011001 +001110011010 +010100000100 +010010000011 +101000000001 +000011110000 +010011011100 +111000110001 +010111101111 +101100010010 +101110111111 +111101100101 +001100101011 +100110111100 +011100011010 \ No newline at end of file diff --git a/src/day5/example.txt b/src/day5/example.txt new file mode 100644 index 0000000..1d4e36d --- /dev/null +++ b/src/day5/example.txt @@ -0,0 +1,10 @@ +0,9 -> 5,9 +8,0 -> 0,8 +9,4 -> 3,4 +2,2 -> 2,1 +7,0 -> 7,4 +6,4 -> 2,0 +0,9 -> 2,9 +3,4 -> 1,4 +0,0 -> 8,8 +5,5 -> 8,2 \ No newline at end of file diff --git a/src/day5/index.ts b/src/day5/index.ts new file mode 100644 index 0000000..e4a43d7 --- /dev/null +++ b/src/day5/index.ts @@ -0,0 +1,78 @@ +import path from 'path'; +import {readFile} from "../utils"; + +let input = readFile(path.resolve(__dirname, 'input.txt')); + +// Parse data +const data = input.map(x => { + const row = x.split(' -> '); + const from = row[0].split(','); + const to = row[1].split(','); + + return { + from: { + x: parseInt(from[0]), + y: parseInt(from[1]) + }, + to: { + x: parseInt(to[0]), + y: parseInt(to[1]) + } + } +}) + +const size = data.reduce((a, b) => { + let x = Math.max(a.x, Math.max(b.to.x, b.from.x)); + let y = Math.max(a.y, Math.max(b.to.y, b.from.y)); + return {x, y}; +}, {x: 0, y: 0}); + + + +const solve = (data, diag) => { + let grid = Array(); + for (let i = 0; i <= size.x; i++) grid.push(Array(size.y + 1).fill(0)); + + data.forEach(entry => { + let eq, from, to; + if (entry.from.x === entry.to.x) { + eq = entry.from.x; + from = Math.min(entry.from.y, entry.to.y); + to = Math.max(entry.from.y, entry.to.y); + for (let i = from; i <= to; i++) { + grid[i][eq]++; + } + } else if (entry.from.y === entry.to.y) { + eq = entry.from.y; + from = Math.min(entry.from.x, entry.to.x); + to = Math.max(entry.from.x, entry.to.x); + for (let i = from; i <= to; i++) { + grid[eq][i]++; + } + } + }) + + return grid; +} + + +const print = (grid) => { + grid.forEach(y => { + let str = ""; + y.forEach(x => str += y) + console.log(str); + }) +} + +const countDangerZones = (grid) => { + let count = 0; + grid.forEach(y => { + y.forEach(v => { + if(v > 1) count++; + }) + }) + return count; +} + +const grid = solve(data, false); +console.log("Part 1:", countDangerZones(grid)); diff --git a/src/day5/input.txt b/src/day5/input.txt new file mode 100644 index 0000000..b9c8e14 --- /dev/null +++ b/src/day5/input.txt @@ -0,0 +1,500 @@ +822,976 -> 822,117 +387,419 -> 387,468 +659,275 -> 318,616 +535,940 -> 684,940 +294,427 -> 294,88 +568,922 -> 425,779 +99,265 -> 97,267 +534,387 -> 402,387 +958,268 -> 958,315 +234,714 -> 234,305 +507,257 -> 507,328 +455,384 -> 455,645 +483,641 -> 483,138 +410,249 -> 825,249 +609,836 -> 528,836 +77,913 -> 77,780 +478,93 -> 478,123 +391,695 -> 715,695 +972,586 -> 649,586 +936,955 -> 39,58 +168,791 -> 944,15 +934,222 -> 811,222 +372,683 -> 372,93 +911,676 -> 771,816 +147,34 -> 147,97 +524,498 -> 986,960 +791,924 -> 239,372 +293,145 -> 776,628 +195,472 -> 195,31 +884,649 -> 215,649 +95,53 -> 95,966 +484,111 -> 866,493 +564,913 -> 681,913 +36,362 -> 486,362 +762,146 -> 762,323 +330,334 -> 937,334 +704,714 -> 744,714 +56,12 -> 958,914 +855,784 -> 855,629 +206,665 -> 206,681 +436,629 -> 117,948 +414,685 -> 414,54 +788,401 -> 788,842 +442,270 -> 309,270 +606,711 -> 416,521 +45,630 -> 45,543 +221,192 -> 221,537 +629,544 -> 417,544 +362,605 -> 885,605 +988,928 -> 738,678 +460,758 -> 981,237 +561,26 -> 561,409 +543,186 -> 833,186 +963,409 -> 910,462 +55,129 -> 742,816 +245,27 -> 806,27 +318,137 -> 318,592 +762,700 -> 762,483 +436,341 -> 436,654 +641,725 -> 641,227 +486,446 -> 56,16 +113,821 -> 113,196 +740,534 -> 821,534 +606,765 -> 125,284 +614,786 -> 371,786 +215,519 -> 215,814 +274,496 -> 482,496 +131,210 -> 473,552 +701,68 -> 701,395 +81,713 -> 81,104 +458,677 -> 622,677 +237,225 -> 237,810 +384,246 -> 384,644 +411,876 -> 445,910 +182,814 -> 182,184 +643,858 -> 407,858 +698,621 -> 971,348 +309,253 -> 511,51 +990,368 -> 671,368 +129,619 -> 750,619 +382,803 -> 382,635 +629,270 -> 415,484 +550,101 -> 56,595 +971,745 -> 971,589 +353,947 -> 786,514 +71,931 -> 974,28 +915,621 -> 363,69 +650,547 -> 650,596 +884,228 -> 574,228 +833,476 -> 833,824 +24,722 -> 24,885 +401,508 -> 401,553 +452,106 -> 740,106 +255,840 -> 60,840 +551,609 -> 551,767 +799,572 -> 356,129 +343,138 -> 945,740 +77,766 -> 77,72 +190,760 -> 190,341 +978,583 -> 911,583 +557,18 -> 557,640 +673,276 -> 882,276 +143,348 -> 775,980 +776,803 -> 776,812 +38,348 -> 38,463 +648,567 -> 210,567 +468,934 -> 534,868 +966,987 -> 26,47 +471,465 -> 126,465 +26,29 -> 940,943 +416,340 -> 361,285 +702,66 -> 702,673 +34,964 -> 951,47 +156,791 -> 65,791 +635,678 -> 355,678 +898,13 -> 898,909 +644,334 -> 644,155 +304,93 -> 445,93 +946,934 -> 930,918 +376,289 -> 317,348 +460,491 -> 500,491 +761,605 -> 866,710 +252,534 -> 413,534 +178,23 -> 895,740 +677,90 -> 677,592 +905,985 -> 211,291 +963,65 -> 963,214 +853,617 -> 815,617 +576,532 -> 319,275 +134,59 -> 829,754 +926,948 -> 11,33 +46,279 -> 760,279 +60,389 -> 60,340 +49,264 -> 561,776 +912,322 -> 790,322 +825,205 -> 825,433 +637,693 -> 780,693 +891,196 -> 200,196 +537,604 -> 604,604 +870,75 -> 870,342 +518,173 -> 455,110 +666,67 -> 919,67 +536,128 -> 137,527 +698,41 -> 44,41 +936,982 -> 106,152 +780,488 -> 780,918 +968,313 -> 56,313 +759,36 -> 140,36 +111,605 -> 111,430 +609,165 -> 54,720 +388,452 -> 745,809 +986,12 -> 11,987 +193,614 -> 335,756 +113,239 -> 38,164 +715,145 -> 305,555 +632,13 -> 508,137 +698,673 -> 698,101 +954,225 -> 720,225 +626,688 -> 626,476 +636,697 -> 91,152 +228,210 -> 621,210 +83,325 -> 989,325 +644,358 -> 644,648 +481,183 -> 481,901 +803,160 -> 250,713 +648,96 -> 913,96 +921,17 -> 26,912 +844,256 -> 844,485 +190,138 -> 578,526 +383,527 -> 357,501 +529,212 -> 757,440 +646,399 -> 944,399 +961,681 -> 961,178 +40,236 -> 484,680 +432,465 -> 832,465 +988,989 -> 10,11 +850,800 -> 850,292 +25,97 -> 372,97 +383,248 -> 383,125 +577,488 -> 852,763 +607,322 -> 432,147 +611,637 -> 614,637 +853,677 -> 755,677 +953,225 -> 953,333 +984,978 -> 45,39 +95,981 -> 314,981 +550,400 -> 327,177 +929,893 -> 99,63 +958,293 -> 152,293 +68,656 -> 563,161 +45,506 -> 45,925 +271,566 -> 289,566 +460,440 -> 920,900 +392,788 -> 392,263 +946,335 -> 946,171 +833,405 -> 876,448 +268,188 -> 268,678 +881,40 -> 30,40 +317,84 -> 277,84 +415,917 -> 415,358 +28,654 -> 28,646 +346,461 -> 346,724 +935,954 -> 935,362 +121,840 -> 121,38 +29,55 -> 344,370 +774,649 -> 621,649 +31,985 -> 396,620 +85,579 -> 85,131 +181,886 -> 978,89 +284,969 -> 284,95 +669,943 -> 54,328 +72,509 -> 72,857 +460,557 -> 986,31 +555,460 -> 555,928 +921,961 -> 666,706 +11,13 -> 951,953 +173,948 -> 173,982 +680,422 -> 503,245 +443,232 -> 443,630 +829,487 -> 773,487 +176,631 -> 631,176 +860,866 -> 559,866 +317,537 -> 624,537 +937,199 -> 753,199 +484,545 -> 293,736 +84,616 -> 489,211 +340,166 -> 340,876 +47,210 -> 47,961 +852,404 -> 852,804 +831,565 -> 454,188 +236,621 -> 556,941 +734,640 -> 734,728 +756,357 -> 756,433 +102,220 -> 157,220 +153,800 -> 104,849 +710,585 -> 710,666 +724,298 -> 724,698 +762,257 -> 123,896 +402,642 -> 402,964 +549,108 -> 945,108 +301,966 -> 851,966 +399,903 -> 101,605 +194,857 -> 605,857 +291,373 -> 85,373 +601,597 -> 844,597 +362,335 -> 362,107 +769,66 -> 107,66 +49,373 -> 49,143 +96,800 -> 894,800 +468,116 -> 557,116 +549,206 -> 956,206 +918,972 -> 35,89 +688,632 -> 106,632 +267,338 -> 414,485 +258,392 -> 258,107 +604,964 -> 139,499 +975,191 -> 977,191 +861,960 -> 93,192 +979,570 -> 439,570 +961,21 -> 63,919 +312,419 -> 881,419 +507,779 -> 507,79 +775,447 -> 645,447 +34,928 -> 626,928 +786,515 -> 786,34 +81,269 -> 734,922 +938,264 -> 266,264 +799,271 -> 799,713 +765,860 -> 765,43 +967,869 -> 135,37 +920,757 -> 602,439 +419,814 -> 655,814 +22,40 -> 472,490 +136,672 -> 770,672 +667,860 -> 288,481 +311,305 -> 311,674 +163,569 -> 163,120 +801,885 -> 139,223 +551,214 -> 551,207 +594,979 -> 942,631 +347,286 -> 448,185 +161,134 -> 855,828 +861,864 -> 20,23 +309,388 -> 309,676 +582,383 -> 86,879 +930,58 -> 206,782 +538,925 -> 242,925 +379,890 -> 704,565 +338,350 -> 338,419 +916,870 -> 916,283 +101,928 -> 402,627 +899,696 -> 899,179 +96,62 -> 658,624 +950,46 -> 11,985 +914,510 -> 910,510 +98,116 -> 556,116 +165,151 -> 913,899 +822,76 -> 41,76 +878,97 -> 75,900 +476,623 -> 873,623 +257,494 -> 746,983 +638,23 -> 638,778 +969,212 -> 259,212 +186,146 -> 923,883 +799,822 -> 736,822 +628,876 -> 532,780 +388,604 -> 388,617 +591,949 -> 868,949 +92,372 -> 107,357 +274,691 -> 254,671 +169,917 -> 169,317 +792,27 -> 710,27 +215,645 -> 713,147 +886,498 -> 689,498 +955,129 -> 214,870 +561,380 -> 375,380 +391,849 -> 391,75 +181,677 -> 759,677 +347,394 -> 347,969 +52,383 -> 875,383 +498,827 -> 729,596 +125,832 -> 125,18 +799,711 -> 105,711 +952,884 -> 129,61 +123,269 -> 123,906 +792,934 -> 12,154 +955,253 -> 753,253 +676,957 -> 676,264 +437,35 -> 40,35 +661,166 -> 509,14 +72,978 -> 270,978 +554,472 -> 554,627 +914,291 -> 464,741 +450,841 -> 827,841 +846,694 -> 846,281 +391,97 -> 890,596 +859,512 -> 685,338 +349,564 -> 349,187 +473,442 -> 896,19 +383,949 -> 383,276 +568,596 -> 875,596 +298,43 -> 298,504 +482,632 -> 12,632 +44,441 -> 44,978 +210,502 -> 210,712 +191,391 -> 191,196 +27,683 -> 27,840 +47,13 -> 960,926 +81,779 -> 81,952 +914,96 -> 145,96 +444,175 -> 151,175 +293,934 -> 293,488 +106,867 -> 221,982 +669,576 -> 858,576 +713,142 -> 84,771 +60,923 -> 299,923 +376,307 -> 281,307 +904,53 -> 41,916 +763,495 -> 588,495 +62,899 -> 843,899 +63,939 -> 592,410 +294,588 -> 294,437 +920,101 -> 879,142 +688,527 -> 688,357 +679,854 -> 950,854 +636,765 -> 329,765 +939,87 -> 62,87 +952,560 -> 952,193 +466,761 -> 32,327 +189,245 -> 876,932 +42,675 -> 42,311 +787,257 -> 62,982 +934,983 -> 64,113 +548,522 -> 626,522 +942,322 -> 942,276 +267,298 -> 267,385 +632,241 -> 632,117 +192,644 -> 192,507 +627,33 -> 627,175 +350,650 -> 350,157 +807,239 -> 113,933 +294,713 -> 87,713 +81,530 -> 453,530 +539,61 -> 240,61 +893,76 -> 893,776 +39,49 -> 978,988 +733,17 -> 606,144 +19,408 -> 493,408 +364,639 -> 43,639 +873,946 -> 69,142 +825,875 -> 233,875 +708,408 -> 197,919 +990,202 -> 990,32 +370,393 -> 664,393 +764,472 -> 284,472 +308,685 -> 308,541 +917,68 -> 45,940 +397,794 -> 524,667 +525,674 -> 313,462 +475,981 -> 297,981 +728,152 -> 40,840 +265,257 -> 162,257 +621,847 -> 828,640 +547,927 -> 21,401 +351,676 -> 357,676 +668,640 -> 668,228 +138,736 -> 446,736 +912,687 -> 912,817 +718,278 -> 718,779 +674,786 -> 986,474 +812,273 -> 891,273 +46,341 -> 46,831 +404,464 -> 118,464 +277,926 -> 114,763 +231,786 -> 106,786 +266,721 -> 446,541 +42,80 -> 675,713 +422,116 -> 422,344 +922,543 -> 922,511 +525,150 -> 525,599 +862,288 -> 498,288 +961,432 -> 961,693 +895,876 -> 895,837 +619,361 -> 174,361 +275,673 -> 275,415 +950,179 -> 405,179 +460,460 -> 460,405 +222,872 -> 222,959 +451,403 -> 21,833 +570,367 -> 570,147 +260,792 -> 82,970 +586,159 -> 108,159 +956,25 -> 55,926 +71,645 -> 988,645 +540,438 -> 44,934 +230,160 -> 395,160 +811,106 -> 811,598 +819,741 -> 596,741 +347,731 -> 347,198 +503,580 -> 495,580 +30,706 -> 603,133 +257,498 -> 619,498 +232,784 -> 359,784 +826,708 -> 396,708 +14,977 -> 895,96 +964,192 -> 279,192 +611,282 -> 611,460 +207,903 -> 207,882 +969,10 -> 22,957 +403,599 -> 970,32 +712,671 -> 712,733 +795,422 -> 235,422 +47,965 -> 939,73 +925,839 -> 275,189 +102,593 -> 973,593 +389,768 -> 389,493 +234,365 -> 624,365 +50,901 -> 50,702 +345,573 -> 638,866 +798,280 -> 111,967 +258,556 -> 258,978 +966,57 -> 88,935 +171,279 -> 171,130 +238,727 -> 947,727 +683,777 -> 290,777 +552,308 -> 552,150 +81,91 -> 911,921 +845,698 -> 845,735 +712,241 -> 26,927 +120,813 -> 716,217 +947,10 -> 25,932 +395,381 -> 338,324 +408,989 -> 408,529 +600,574 -> 600,557 +46,567 -> 46,980 +983,14 -> 10,987 +984,568 -> 642,568 +543,250 -> 543,80 +765,534 -> 108,534 +89,213 -> 120,213 +893,813 -> 271,191 +133,178 -> 133,654 +468,689 -> 468,677 +970,203 -> 970,952 +892,322 -> 892,417 +593,735 -> 152,294 +839,75 -> 839,964 +746,413 -> 89,413 +535,595 -> 535,633 +771,646 -> 614,489 +572,852 -> 572,718 +125,35 -> 838,748 +122,812 -> 179,812 +467,243 -> 188,243 +713,850 -> 51,188 +633,279 -> 932,578 +428,230 -> 216,18 +94,921 -> 973,42 +513,609 -> 513,507 +826,358 -> 330,854 \ No newline at end of file diff --git a/src/day6/example.txt b/src/day6/example.txt new file mode 100644 index 0000000..a7af2b1 --- /dev/null +++ b/src/day6/example.txt @@ -0,0 +1 @@ +3,4,3,1,2 \ No newline at end of file diff --git a/src/day6/index.ts b/src/day6/index.ts new file mode 100644 index 0000000..5e74480 --- /dev/null +++ b/src/day6/index.ts @@ -0,0 +1,35 @@ +import path from 'path'; +import {readFile} from "../utils"; + +let input = readFile(path.resolve(__dirname, 'input.txt'))[0].split(","); + +// prepare the buckets of fish +let buckets = [0, 0, 0, 0, 0, 0, 0, 0, 0]; +input.forEach(number => { + const num = parseInt(number); + buckets[num - 1]++; +}) + +const numFish = (days) => { + for (let k = 1; k < days; k++) { + let diff = [0, 0, 0, 0, 0, 0, 0, 0, 0]; + for (let i = 0; i < 9; i++) { + const numFish = buckets[i]; + if (i === 0) { + diff[8] += numFish; + diff[6] += numFish; + diff[0] -= numFish; + } else { + diff[i] -= numFish; + diff[i - 1] += numFish; + } + } + for (let i = 8; i >= 0; i--) { + buckets[i] += diff[i]; + } + } + return buckets.reduce((a, b) => a + b, 0); +} + +console.log("Part 1:", numFish(80)); +console.log("Part 2:", numFish(256)); \ No newline at end of file diff --git a/src/day6/input.txt b/src/day6/input.txt new file mode 100644 index 0000000..07d6764 --- /dev/null +++ b/src/day6/input.txt @@ -0,0 +1 @@ +3,5,4,1,2,1,5,5,1,1,1,1,4,1,4,5,4,5,1,3,1,1,1,4,1,1,3,1,1,5,3,1,1,3,1,3,1,1,1,4,1,2,5,3,1,4,2,3,1,1,2,1,1,1,4,1,1,1,1,2,1,1,1,3,1,1,4,1,4,1,5,1,4,2,1,1,5,4,4,4,1,4,1,1,1,1,3,1,5,1,4,5,3,1,4,1,5,2,2,5,1,3,2,2,5,4,2,3,4,1,2,1,1,2,1,1,5,4,1,1,1,1,3,1,5,4,1,5,1,1,4,3,4,3,1,5,1,1,2,1,1,5,3,1,1,1,1,1,5,1,1,1,1,1,1,1,2,2,5,5,1,2,1,2,1,1,5,1,3,1,5,2,1,4,1,5,3,1,1,1,2,1,3,1,4,4,1,1,5,1,1,4,1,4,2,3,5,2,5,1,3,1,2,1,4,1,1,1,1,2,1,4,1,3,4,1,1,1,1,1,1,1,2,1,5,1,1,1,1,2,3,1,1,2,3,1,1,3,1,1,3,1,3,1,3,3,1,1,2,1,3,2,3,1,1,3,5,1,1,5,5,1,2,1,2,2,1,1,1,5,3,1,1,3,5,1,3,1,5,3,4,2,3,2,1,3,1,1,3,4,2,1,1,3,1,1,1,1,1,1 \ No newline at end of file