]> git.sommitrealweird.co.uk Git - advent-of-code-2020.git/blob - day03/count_trees.sh
Day 8 2020
[advent-of-code-2020.git] / day03 / count_trees.sh
1 #!/bin/bash
2
3 start_letter=0
4 line_number=0
5 tree_count=0
6
7 for line in $(<input.txt); do
8     line_number=$((line_number+1))
9     if [ $line_number -eq 1 ]; then
10         continue
11     fi
12     start_letter=$((start_letter+3))
13     if [ $start_letter -ge ${#line} ]; then
14         start_letter=$((start_letter - ${#line}))
15     fi
16     if [ "${line:$start_letter:1}" = "#" ]; then
17         tree_count=$((tree_count+1))
18     fi
19 done
20
21 echo "Hit $tree_count trees"
22
23 exit 0
24
25 #!/usr/bin/python3
26
27 trees_count = 0
28 position = 3 # forth square in, we moved 3 right from where we were, 0 + 3 -> 3
29 on_first_line = True
30 line_number = 0
31
32 for line in open("input.txt", "r"):
33     line_number += 1
34     print("line: ", line_number)
35     line = line.rstrip()
36     print(line)
37     if on_first_line:
38         on_first_line = False
39         continue
40     print(line[position], position)
41     if line[position] == "#":
42         trees_count += 1
43         print("hit tree")
44     else:
45         print("missed tree")
46     # now add 3 to the position
47     position += 3
48     if position >= len(line):
49         position = position - len(line)
50
51 print("There were", trees_count, "trees")