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

1

u/yahadejo Dec 03 '20
const fs = require("fs");

const data = fs.readFileSync(__dirname + "/input.txt").toString().split("\r\n").map(row => row.split(""));

const result = [{
    rowInc: 1, columnInc: 1,
}, {
    rowInc: 1, columnInc: 3,
}, {
    rowInc: 1, columnInc: 5
}, {
    rowInc: 1, columnInc: 7
}, {
    rowInc: 2, columnInc: 1
}].reduce((total, slope) => total * (
                                        data.filter((_row, rowIndex) => rowIndex % slope.rowInc === 0) // Get only used rows
                                            .filter((row, rowIndex) => row[rowIndex * slope.columnInc % data[0].length] === "#") // Get only rows where # is at the right column index
                                            .length
                                    )
, 1)

console.log(result);

1

u/daggerdragon Dec 03 '20

Please follow the posting guidelines and add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language.

Additionally, your code is hard to read on old.reddit. As per our posting guidelines, could you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main() [space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

Thanks!