From 00e8704ac3cdf70ebfa1bb8e463b1e32d749c14e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 2 Dec 2020 21:14:34 -0800 Subject: [PATCH] Add day3 solution. --- day3.cr | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 day3.cr diff --git a/day3.cr b/day3.cr new file mode 100644 index 0000000..f1c6fa2 --- /dev/null +++ b/day3.cr @@ -0,0 +1,29 @@ +INPUT = File.read("day3.txt").lines.map(&.chomp) + +def run(input, slopes) + prod = 1_i64 + slopes.each do |slope| + trees = 0 + pos = 0 + line = 0 + right, down = slope + while line < input.size + trees += 1 if input[line][pos] == '#' + pos = (pos + right) % input[line].size + line += down + end + prod *= trees + end + prod +end + +def part1 + puts run(INPUT, [{3,1}]) +end + +def part2 + puts run(INPUT, [{1,1}, {3,1}, {5,1}, {7,1}, {1,2}]) +end + +part1 +part2