Skip to content

Commit

Permalink
🚎🛗 Updated with Glitch: day 5, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Glitch (branch-three-oviraptor) committed Dec 5, 2023
1 parent b5cc91c commit 9403ce0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
67 changes: 58 additions & 9 deletions public/funs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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; }
}
};

Expand Down
7 changes: 6 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
};
Expand Down
2 changes: 1 addition & 1 deletion views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ <h2>solutions:</h2>
<li><a href="/day/02">day 02</a></li>
<li><a href="/day/03">day 03</a></li>
<li><a href="/day/04">day 04</a></li>
<!--
<li><a href="/day/05">day 05</a></li>
<!--
<li><a href="/day/06">day 06</a></li>
<li><a href="/day/07">day 07</a></li>
<li><a href="/day/08">day 08</a></li>
Expand Down
7 changes: 4 additions & 3 deletions views/stats.ntl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
<meta name="theme-color" content="#373fff">
<link rel="stylesheet" href="/style.css">
<style type='text/css'>
ol,li {display:block;margin:0 0 2em 0;padding:0;width:100%;list-style:none;font-size:12px;text-transform:capitalize;}
li li {font-size:11px;}
b {display:block;background:#99F;font-size:10px;line-height:21px;text-align:right;padding-right:0.5em;margin:2px 0;border-right:solid 1px #77D;border-bottom:solid 1px #77D; height: 22px; white-space: nowrap; padding-left: 0.5em; }
ol,li {display:block;margin:0;padding:0;width:100%;list-style:none;font-size:12px;text-transform:capitalize;}
ol ol {margin: 0;padding:0 0 0 1em;}
li li {font-size:11px; margin: 0 0 0.5em 0;}
b {display:block;background:#99F;font-size:10px;line-height:17px;text-align:right;padding-right:0.5em;margin:2px 0;border-right:solid 1px #77D;border-bottom:solid 1px #77D; height: 16px; white-space: nowrap; padding-left: 0.5em; }
b+b {background:#F99;border-color:#D77;}
b+b+b {background:#9F9;border-color:#7D7;}
</style>
Expand Down

0 comments on commit 9403ce0

Please sign in to comment.