From 9403ce0e167d3fb1b209d75c622e5e08eab26858 Mon Sep 17 00:00:00 2001 From: "Glitch (branch-three-oviraptor)" Date: Tue, 5 Dec 2023 06:12:27 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=8E=F0=9F=9B=97=20Updated=20with=20Gli?= =?UTF-8?q?tch:=20day=205,=20part=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/funs.js | 67 +++++++++++++++++++++++++++++++++++++++++------- server.js | 7 ++++- views/index.html | 2 +- views/stats.ntl | 7 ++--- 4 files changed, 69 insertions(+), 14 deletions(-) diff --git a/public/funs.js b/public/funs.js index 7be0ec1..543d990 100644 --- a/public/funs.js +++ b/public/funs.js @@ -282,7 +282,56 @@ }, day5: { part1: (data) => { - return data; + const almanac = data.trim().split('\n\n'); + const seeds = almanac[0].split(':')[1].trim().split(' ').map(Number); + const maps = almanac.slice(1).map(m => { + const lines = m.split('\n'); + const matchName = /(\w+)-to-(\w+)/; + const matched = lines[0].match(matchName); + const source = matched[1]; + const dest = matched[2]; + const routes = lines.slice(1).map(l => { + const nums = l.split(' ').map(Number); + return { + s1: nums[1], + s2: nums[1] + nums[2], + d1: nums[0], + d2: nums[0] + nums[2], + r: nums[2] + }; + }); + return { + source, + dest, + routes + }; + }); + console.log(seeds, maps); + const paths = seeds.reduce((acc, s) => { + acc.push({ seed: s }); + return acc; + }, []); + let step = 'seed'; + maps.forEach(m => { + const next = m.dest; + paths.forEach(p => { + const val = p[step]; + let isMapped = false; + for (let i = 0; i < m.routes.length; i++) { + if (val >= m.routes[i].s1 && val <= m.routes[i].s2) { + p[next] = (val - m.routes[i].s1) + m.routes[i].d1; + isMapped = true; + break; + } + } + if (!isMapped) { + p[next] = val; + } + }); + step = next; + }); + console.log(paths) + return Math.min(...paths.map(p => p.location)); }, part2: (data) => { return data; @@ -407,20 +456,20 @@ } }, day22: { - part1: () => {}, - part2: () => {} + part1: (d) => { return d; }, + part2: (d) => { return d; } }, day23: { - part1: () => {}, - part2: () => {} + part1: (d) => { return d; }, + part2: (d) => { return d; } }, day24: { - part1: () => {}, - part2: () => {} + part1: (d) => { return d; }, + part2: (d) => { return d; } }, day25: { - part1: () => {}, - part2: () => {} + part1: (d) => { return d; }, + part2: (d) => { return d; } } }; diff --git a/server.js b/server.js index d4603ff..e1ffeb3 100644 --- a/server.js +++ b/server.js @@ -3,7 +3,7 @@ const https = require('https'); const path = require('path'); const express = require('express'); const app = express(); -const timeout = require('connect-timeout'); // express v4 +const timeout = require('connect-timeout'); const rateLimit = require('express-rate-limit'); const { JSHINT } = require('jshint'); @@ -70,7 +70,9 @@ app.engine('ntl', (filePath, options, callback) => { // define the template engi app.set('views', './views'); // specify the views directory app.set('view engine', 'ntl'); // register the template engine +// store stats for each day const dayStats = []; + // bind 25 days of html files, and post functions for both parts of each for (let d = 1; d <= 25; d++) { const digit = d; @@ -136,6 +138,7 @@ for (let d = 1; d <= 25; d++) { response.status(200).json({ output: answer }); }); } + // store strings of functions for stats below dayStats.push(partStats); } @@ -156,7 +159,9 @@ fs.readFile(path.join(__dirname, 'views/stats.ntl'), function (err, content) { devel: true }, {}); const part = { + // subtract the name and brackets charCount: p.length - 8, + // number of newlines lineCount: p.split('\n').length, complex: JSHINT.data().functions[0].metrics.complexity }; diff --git a/views/index.html b/views/index.html index aac2372..0943c7e 100644 --- a/views/index.html +++ b/views/index.html @@ -24,8 +24,8 @@

solutions:

  • day 02
  • day 03
  • day 04
  • -