Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day02 #2

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v18.20.5
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ my 2024 solutions on ~~glitch: [meowing-holy-carbon](https://meowing-holy-carbon

also on github: [dieseltravis/aoc2024](https://github.com/dieseltravis/aoc2024)

[![Days completed in a row](https://img.shields.io/badge/⭐%20days%20in%20a%20row-01-blueviolet)](https://adventofcode.com/2024/) [![Node.js CI](https://github.com/dieseltravis/aoc2024/actions/workflows/node.js.yml/badge.svg)](https://github.com/dieseltravis/aoc2024/actions/workflows/node.js.yml) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?logo=javascript)](https://github.com/standard/semistandard)
[![Days completed in a row](https://img.shields.io/badge/⭐%20days%20in%20a%20row-2-blueviolet)](https://adventofcode.com/2024/) [![Node.js CI](https://github.com/dieseltravis/aoc2024/actions/workflows/node.js.yml/badge.svg)](https://github.com/dieseltravis/aoc2024/actions/workflows/node.js.yml) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?logo=javascript)](https://github.com/standard/semistandard)

## solutions:
Install node `>18.18.x`, `yarn`, and then run `yarn install` and then `yarn start`.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meowing-holy-carbon",
"version": "2024.12.01",
"version": "2024.12.02",
"description": "Travis's Advent of Code 2024",
"author": "Travis Hardiman",
"homepage": "https://github.com/dieseltravis/aoc2024/",
Expand Down
91 changes: 89 additions & 2 deletions public/funs.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,95 @@
}
},
day2: {
part1: d => d,
part2: d => d
part1: (data) => {
const input = data.trim().split('\n').map(r => r.split(' ').map(Number)).reduce((acc, r) => {
let ascending = 0;
let descending = 0;
const max = r.reduce((diff, c, i) => {
if (i > 0) {
const fromPrev = r[i - 1] - c;
diff = Math.max(diff, Math.abs(fromPrev));
const ordering = fromPrev > 0 ? 1 : fromPrev < 0 ? -1 : 0;
if (ordering > 0) {
ascending++;
} else if (ordering < 0) {
descending++;
}
}
return diff;
}, 0);
acc.push({
row: r,
max,
allMoved: (r.length - 1 === ascending + descending),
isOrder: (ascending === 0 || descending === 0)
});
return acc;
}, []);
console.log(input);

return input.filter(r => r.max <= 3 && r.allMoved && r.isOrder).length;
},
part2: (data) => {
const input = data.trim().split('\n').map(r => r.split(' ').map(Number)).reduce((acc, r) => {
let ascending = 0;
let descending = 0;
const max = r.reduce((diff, c, i) => {
if (i > 0) {
const fromPrev = r[i - 1] - c;
diff = Math.max(diff, Math.abs(fromPrev));
const ordering = fromPrev > 0 ? 1 : fromPrev < 0 ? -1 : 0;
if (ordering > 0) {
ascending++;
} else if (ordering < 0) {
descending++;
}
}
return diff;
}, 0);
acc.push({
row: r,
max,
allMoved: (r.length - 1 === ascending + descending),
isOrder: (ascending === 0 || descending === 0)
});
return acc;
}, []);
console.log(input);

const valid = input.filter(r => r.max <= 3 && r.allMoved && r.isOrder).length;
const second = input.filter(r => !(r.max <= 3 && r.allMoved && r.isOrder)).reduce((acc, r) => {
for (let l = r.row.length; l--;) {
const index = l;
const chance = r.row.filter((_, i) => i !== index);
let ascending = 0;
let descending = 0;
for (let ll = chance.length; ll--;) {
if (ll > 0) {
const fromPrev = chance[ll - 1] - chance[ll];
if (Math.abs(fromPrev) <= 3) {
const ordering = fromPrev > 0 ? 1 : fromPrev < 0 ? -1 : 0;
if (ordering > 0) {
ascending++;
} else if (ordering < 0) {
descending++;
} else {
break;
}
}
}
}
if ((ascending === 0 || descending === 0) && (chance.length - 1 === ascending + descending)) {
acc++;
console.log(acc);
break;
}
}
return acc;
}, 0);

return valid + second;
}
},
day3: {
part1: d => d,
Expand Down
2 changes: 1 addition & 1 deletion views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ <h1>Travis's Advent of Code 2024</h1>
<h2>solutions:</h2>
<ol>
<li><a href="/day/01">day 01</a></li>
<!--
<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>
Expand Down
Loading