From f858b3d412a4f4382aec783be9343a77048ec13c Mon Sep 17 00:00:00 2001 From: Travis Hardiman Date: Sat, 7 Dec 2024 00:55:18 -0500 Subject: [PATCH 1/2] day 7, part 1 --- public/funs.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/public/funs.js b/public/funs.js index 2195eb8..1fd76eb 100644 --- a/public/funs.js +++ b/public/funs.js @@ -348,7 +348,39 @@ part2: d => d }, day7: { - part1: 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: d => d }, day8: { From fa1b64bdedaf578d65bb46f2675e71d0086a163d Mon Sep 17 00:00:00 2001 From: Travis Hardiman Date: Sat, 7 Dec 2024 01:06:20 -0500 Subject: [PATCH 2/2] day 7, part 2 --- README.md | 2 +- package.json | 2 +- public/funs.js | 35 ++++++++++++++++++++++++++++++++++- views/index.html | 2 +- 4 files changed, 37 insertions(+), 4 deletions(-) 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 c70fd20..ee9a929 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "meowing-holy-carbon", - "version": "2024.12.05", + "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 1fd76eb..8d03eb0 100644 --- a/public/funs.js +++ b/public/funs.js @@ -381,7 +381,40 @@ const sum = good.reduce((acc, n) => acc + n, 0); return sum; }, - part2: d => d + 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 9765750..2ab3542 100644 --- a/views/index.html +++ b/views/index.html @@ -25,9 +25,9 @@

solutions:

  • day 03
  • day 04
  • day 05
  • -