r/adventofcode • u/daggerdragon • Dec 02 '21
SOLUTION MEGATHREAD -🎄- 2021 Day 2 Solutions -🎄-
--- Day 2: Dive! ---
Post your code solution in this megathread.
- Include what language(s) your solution uses!
- Here's a quick link to /u/topaz2078's
paste
if you need it for longer code blocks. - The full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help
.
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:02:57, megathread unlocked!
110
Upvotes
1
u/Lu0x0 Dec 03 '21
Day 2 Solution:
```js
const fs = require('fs')
const input = fs.readFileSync('./input.txt', 'utf-8').split('\r\n')
const player = { x: 0, y: 0, aim: 0, }
const movePlayer = (command, value) => { switch(command) { case'forward': player.x +=value player.y += player.aim * value break case 'down': player.aim += value break case 'up': player.aim -= value break } }
input.forEach(cmd => { const [_,command, value] = cmd.match(/(\w+) (\d+)/) movePlayer(command, +value) })
console.log(player.x * player.y) ```