diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..93d866f --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +v18.20.5 diff --git a/README.md b/README.md index 9410ba9..9173ece 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/package.json b/package.json index 2aa64af..7dea2ab 100644 --- a/package.json +++ b/package.json @@ -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/", diff --git a/public/funs.js b/public/funs.js index 80eed54..0a0b58f 100644 --- a/public/funs.js +++ b/public/funs.js @@ -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, diff --git a/views/index.html b/views/index.html index a7715bd..3b1e63e 100644 --- a/views/index.html +++ b/views/index.html @@ -21,8 +21,8 @@

Travis's Advent of Code 2024

solutions:

  1. day 01
  2. -