diff --git a/README.md b/README.md index a4066ee..0237d94 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-5-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) +[![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`, run `yarn install`, and then `yarn start`. diff --git a/package.json b/package.json index f441f6b..ee9a929 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "meowing-holy-carbon", - "version": "2024.12.06", + "version": "2024.12.07", "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 4ba8492..5793e8d 100644 --- a/public/funs.js +++ b/public/funs.js @@ -477,8 +477,73 @@ } }, day7: { - part1: d => d, - part2: d => d + part1: (data) => { + const input = data.trim().split('\n').map(r => { + const row = r.split(':').map(p => p.trim()); + return { + key: +row[0], + vals: row[1].split(' ').map(Number) + }; + }); + console.log(input); + const ops = [ + (a, b) => a + b, + (a, b) => a * b + ]; + const good = []; + for (let l = input.length; l--;) { + const key = input[l].key; + const vals = input[l].vals; + const valen = vals.length; + for (let dec = Math.pow(2, valen - 1); dec--;) { + const bin = dec.toString(2).padStart(valen - 1, '0').split('').map(Number); + let test = vals[0]; + for (let i = 1; i < valen; i++) { + test = ops[bin[i - 1]](test, vals[i]); + } + if (key === test) { + good.push(key); + break; + } + } + } + const sum = good.reduce((acc, n) => acc + n, 0); + return sum; + }, + part2: (data) => { + const input = data.trim().split('\n').map(r => { + const row = r.split(':').map(p => p.trim()); + return { + key: +row[0], + vals: row[1].split(' ').map(Number) + }; + }); + console.log(input); + const ops = [ + (a, b) => a + b, + (a, b) => a * b, + (a, b) => +(a + '' + b) + ]; + const good = []; + for (let l = input.length; l--;) { + const key = input[l].key; + const vals = input[l].vals; + const valen = vals.length; + for (let dec = Math.pow(3, valen - 1); dec--;) { + const bin = dec.toString(3).padStart(valen - 1, '0').split('').map(Number); + let test = vals[0]; + for (let i = 1; i < valen; i++) { + test = ops[bin[i - 1]](test, vals[i]); + } + if (key === test) { + good.push(key); + break; + } + } + } + const sum = good.reduce((acc, n) => acc + n, 0); + return sum; + } }, day8: { part1: d => d, diff --git a/views/index.html b/views/index.html index 40b870c..4456bde 100644 --- a/views/index.html +++ b/views/index.html @@ -28,6 +28,7 @@