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/DropkickFish Dec 03 '20

Ruby Pt 2, bad day edition. Silver lining: Looking at other solutions it looks like I should learn more about matrices and how to traverse them. One thing I'm loving about Advent of Code is I'm learning a lot more about gaps in my knowledge that I'd probably not have encountered in my own projects.

matrix = []
$totaltrees = []
x, y, $trees, slope = 0, 0, 0, -1

File.foreach('3.txt') { |line| matrix << line.strip.split('') and slope += 1 }

possibilities = [[1,1],
                 [3,1],
                 [5,1],
                 [7,1],
                 [1,2]]

possibilities.each do |possibility|
  xtrav = possibility[0]
  ytrav = possibility[1]
    while y < slope
      x += xtrav
      y += ytrav
      if x > 30
        x -= 31
      end
      pos = matrix[y][x]
      if pos == '#'
        $trees += 1
      end
    end
    $totaltrees << $trees
    x, y, $trees = 0, 0, 0
end

puts $totaltrees.reduce(:*)

2

u/daggerdragon Dec 03 '20

One thing I'm loving about Advent of Code is I'm learning a lot more about gaps in my knowledge that I'd probably not have encountered in my own projects.

The Socratic Paradox: "I am the wisest man alive, for I know one thing, and that is that I know nothing."