Created
November 21, 2025 06:52
-
-
Save dominicgan/eed209a7b62df47dc445081a83c2f365 to your computer and use it in GitHub Desktop.
Advent of Code 2024 Day 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const input = await fetch('https://adventofcode.com/2024/day/2/input').then(res => res.text()).then(data => data.trim()); | |
| console.log(input); | |
| const data = input.split('\n').map(val => val.split(' ').map((str) => parseInt(str, 10))) | |
| // console.log(data); | |
| const safeCount = data.reduce((acc, arr) => { | |
| if (arr[0] - arr[1] === 0) { | |
| return acc | |
| } | |
| let isSafe = true; | |
| let dir = arr[0] - arr[1] > 0 ? 'down' : 'up'; | |
| // console.log(dir) | |
| for (let i=0; i<arr.length - 1; i++) { | |
| if (arr[i] - arr[i+1] === 0) { | |
| isSafe = false; | |
| break; | |
| } | |
| if (dir === 'down') { | |
| const delta = arr[i] - arr[i+1]; | |
| // console.log({delta}); | |
| if (delta < 1 || delta > 3) { | |
| // console.log(arr, 'down not safe'); | |
| isSafe = false; | |
| break; | |
| } | |
| } | |
| if (dir === 'up') { | |
| const delta = arr[i+1] - arr[i]; | |
| // console.log({delta}); | |
| if (delta < 1 || delta > 3) { | |
| // console.log(arr, 'up not safe'); | |
| isSafe = false; | |
| break; | |
| } | |
| } | |
| } | |
| if (isSafe) { | |
| // console.log(arr, 'is safe') | |
| safe.push(arr) | |
| return acc + 1 | |
| } | |
| return acc | |
| }, 0) | |
| console.log({safeCount}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment