GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — AdventOfCode/languages/js/2023/02_CubeConundrum.js at main · wolflu05/AdventOfCode Expand file tree/ Copy path02_CubeConundrum.jsMore file actions 43 lines (33 loc) · 1.05 KB / Copy path02_CubeConundrum.jsFile metadata and controls43 lines (33 loc) · 1.05 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 import { readInput, textToArray } from '../lib/utils.js'; const list = textToArray(readInput()); const part1 = (list) => { const res = list.reduce((acc, [id, colors]) => { if (colors["red"] <= 12 && colors["green"] <= 13 && colors["blue"] <= 14) { return acc + id; } return acc; }, 0); return res; }; const part2 = (list) => { const res = list.reduce((acc, [, colors]) => { return acc + (colors["red"] * colors["green"] * colors["blue"]); }, 0); return res; }; const parsed = list.map(l => { const [game, sets] = l.split(":"); const [, i] = game.split(" "); const colors = sets .split(/,|;/) .map(f => f.trim().split(" ")) .reduce((colors, [a, color]) => { if (!(color in colors)) colors[color] = []; colors[color].push(parseInt(a)); return colors; }, {}); const maxColors = Object.fromEntries(Object.entries(colors).map(([c, v]) => [c, Math.max(...v)])); return [parseInt(i), maxColors]; }); console.log(part1(parsed)); console.log(part2(parsed)); |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |