From 8503335031af9724d815fb3c7cad70b6acf8f820 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Thu, 3 Dec 2020 19:53:39 +0100 Subject: [PATCH] day3: add python solution --- README.md | 1 + day3/day3.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100755 day3/day3.py diff --git a/README.md b/README.md index de8f57e..97d9f15 100644 --- a/README.md +++ b/README.md @@ -8,3 +8,4 @@ https://adventofcode.com/2020/ |---|--------|---------|------| | 1 | `**` | `**` | | | 2 | `**` | | `**` | +| 3 | `**` | | | diff --git a/day3/day3.py b/day3/day3.py new file mode 100755 index 0000000..1aaa4b9 --- /dev/null +++ b/day3/day3.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python + +def count_trees(right, down, lines): + n = 0 + for i, line in enumerate(lines[::down]): + c = line[(i*right) % len(line)] + if c == '#': + n += 1 + return n + +if __name__ == '__main__': + with open('input.txt', 'r') as f: + lines = [line.strip() for line in f.readlines()] + + print(count_trees(3, 1, lines)) + + hits_multiplied = 1 + for slope in ((1,1), (3,1), (5,1), (7,1), (1,2)): + hits_multiplied *= count_trees(*slope, lines) + print(hits_multiplied)