From 8780290662b653f60b3b3edb08d0cf5ef928b99b Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sat, 5 Dec 2020 01:43:42 +0100 Subject: [PATCH] Add 2020 day 1,2,3 --- 2020/day1/day1.js | 24 ++++++++++++++++++++++++ 2020/day2/day2.js | 29 +++++++++++++++++++++++++++++ 2020/day3/day3.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100755 2020/day1/day1.js create mode 100755 2020/day2/day2.js create mode 100755 2020/day3/day3.js diff --git a/2020/day1/day1.js b/2020/day1/day1.js new file mode 100755 index 0000000..f0ab370 --- /dev/null +++ b/2020/day1/day1.js @@ -0,0 +1,24 @@ +#!/bin/node + +const fs = require('fs'); + +fs.readFile('input.txt', (err, data) => { + const numbers = data.toString().split('\n').map(n => parseInt(n)); + for (let i = 0; i < numbers.length; i++) { + for (let j = i + 1; j < numbers.length; j++) { + if (numbers[i] + numbers[j] === 2020) { + console.log(numbers[i] * numbers[j]); + } + } + } + + for (let i = 0; i < numbers.length; i++) { + for (let j = i + 1; j < numbers.length; j++) { + for (let k = j + 1; k < numbers.length; k++) { + if (numbers[i] + numbers[j] + numbers[k] === 2020) { + console.log(numbers[i] * numbers[j] * numbers[k]); + } + } + } + } +}); diff --git a/2020/day2/day2.js b/2020/day2/day2.js new file mode 100755 index 0000000..d4c7b01 --- /dev/null +++ b/2020/day2/day2.js @@ -0,0 +1,29 @@ +#!/bin/node + +const fs = require('fs'); + +fs.readFile('input.txt', (err, data) => { + const passwords = data.toString().split('\n').filter(l => l.length > 0); + let q1 = 0, q2 = 0; + + for (const [format, password] of passwords.map(line => line.split(':'))) { + const [occurrences, letter] = format.split(' '); + const [min, max] = occurrences.split('-').map(n => parseInt(n)); + + console.log(min, max, letter, password); + + // q1 + let count = 0; + for (let j = 0; j < password.length; j++) { + if (password[j] === letter) { + count++; + } + } + if (count >= min && count <= max) q1++; + + // q2 + if (password[min] === letter && password[max] !== letter || password[min] !== letter && password[max] === letter) q2++; + } + + console.log(q1, q2); +}); diff --git a/2020/day3/day3.js b/2020/day3/day3.js new file mode 100755 index 0000000..d4a5975 --- /dev/null +++ b/2020/day3/day3.js @@ -0,0 +1,34 @@ +#!/bin/node + +const fs = require('fs'); + +fs.readFile('input.txt', (err, data) => { + const lines = data.toString().split('\n').filter(l => l.length > 0); + width = lines[0].length; + + const slopes = [ + [1, 1], + [3, 1], + [5, 1], + [7, 1], + [1, 2], + ]; + const positions = slopes.map(_ => 0); + const results = slopes.map(_ => 0); + + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + + for (let j = 0; j < slopes.length; j++) { + const [right, down] = slopes[j]; + + if (i % down === 0) { + if (line[positions[j]] === '#') results[j]++; + + positions[j] = (positions[j] + right) % width; + } + } + } + + console.log(results, results.reduce((a, n) => a * n, 1)); +});