<jupyter_start><jupyter_text>Day 3: Toboggan Trajectoryhttps://adventofcode.com/2020/day/3Given:* a map of trees in an area; the map repeats indefinitely left-to-right,* a toboggan that starts in the top-left corner of the map, and* moves down and to the right following a rational slopeCount the number of trees you'd encounter going `right 3, down 1`. For instance,given the map:<jupyter_code>test_map = """
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#
""".strip()<jupyter_output><empty_output><jupyter_text>you'd encounter `7` trees.<jupyter_code>import doctest

def count_trees(map, right=0, down=0):
  """
  >>> count_trees(test_map, right=3, down=1)
  7
  """
  map = map.splitlines()
  x = 0
  trees = 0
  for y in range(down, len(map), down):
    x += right
    x %= len(map[y])
    trees += map[y][x] == '#'
  return trees

doctest.testmod()<jupyter_output><empty_output><jupyter_text>Determine the number of trees you would encounter if, for each of the followingslopes, you start at the top-left corner and traverse the map all the way to thebottom:* Right 1, down 1.* Right 3, down 1. (This is the slope you already checked.)* Right 5, down 1.* Right 7, down 1.* Right 1, down 2.In the above example, these slopes would find `2`, `7`, `3`, `4`, and `2`tree(s) respectively; multiplied together, these produce the answer `336`.What do you get if you multiply together the number of trees encountered on eachof the listed slopes?<jupyter_code>from functools import reduce
from operator import mul

def part_two(map):
  """
  >>> part_two(test_map)
  336
  """
  return reduce(
      mul, (count_trees(map, right, down) for right, down
            in ((1,1), (3,1), (5,1), (7,1), (1,2))))

doctest.testmod()<jupyter_output><empty_output><jupyter_text>Running on real input1. Use the file uploader to upload a file2. Re-run the last cell to use the input<jupyter_code>from IPython.display import display
import ipywidgets as widgets

uploader = widgets.FileUpload(accept='.txt', multiple=False)
display(uploader)
map = list(uploader.value.values())[0]['content'].decode('utf-8')
display(f'[Part 1]: {count_trees(map, right=3, down=1)}')
display(f'[Part 2]: {part_two(map)}')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.176163549369291, 'fraction_numerical': 0.023923444976076555, 'mean_word_length': 4.043859649122807, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '352959', 'license': 'Apache-2.0', 'n_tokens_mistral': 848, 'n_tokens_neox': 795, 'n_words': 247, 'path': '2020/toboggan_trajectory.ipynb', 'repo_name': 'willangley/advent-of-code', 'size': '2299'}