Day 1: Secret Entrance
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465


A straightforward day 1 for a Javascript / NodeJS solution.
Poorly Optimized JS Solution
let position = 50; let answer1 = 0; let answer2 = 0; const sequence = require('fs').readFileSync('input-day1.txt', 'utf-8'); sequence.split('\n').forEach(instruction => { const turn = instruction.charAt(0); let distance = parseInt(instruction.slice(1), 10); while (distance > 0) { distance -= 1; if (turn === 'L') { position -= 1; } else if (turn === 'R') { position += 1; } if (position >= 100) { position -= 100; } else if (position < 0) { position += 100; } if (position === 0) { answer2 += 1; } } if (position === 0) { answer1 += 1; } }); console.log(`Part 1 Answer: ${answer1}`); console.log(`Part 2 Answer: ${answer2}`);Brute forcing it like this was my first thought - like they say: if it’s stupid and it works, it’s not stupid.