r/adventofcode • • Dec 03 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 03 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It


--- Day 03: Toboggan Trajectory ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, 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 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:04:56, megathread unlocked!

88 Upvotes

1.3k comments sorted by

View all comments

2

u/musicnothing Dec 03 '20 edited Dec 03 '20

JavaScript one-liner solution for part 1:

`input-goes-here`.split(/\n/).reduce((c, v, i) => c + +(v.charAt(i * 3 % v.length) === '#'), 0);

Basically splits the input into an array of strings, and reduces the array to the number of trees. Uses the array index times three (because three right) to find the correct item in the string, plus uses the modulus for when that position is past the end of the string, it will just loop back around. Also uses the unary + operator to cast the boolean to a 1 or 0.

1

u/daggerdragon Dec 03 '20

FYI: you missed the closing backtick so your code line isn't formatting properly.

2

u/musicnothing Dec 03 '20

I actually wanted the backticks to show since it's a template string, looks like I was missing a space at the beginning of the line. Thanks!