Add some one liners to other_ways for day5
[advent-of-code-2020.git] / day3 / count_trees_2.sh
1 #!/bin/bash
2
3 moves=("1,1" "3,1" "5,1" "7,1" "1,2")
4
5 count_trees() {
6     right=$1
7     down=$2
8     cur_down=-1 # we want to skip the very first line
9     pos=0
10     tree_count=0
11     for line in $(<input.txt); do
12         cur_down=$((cur_down+1))
13         if [ $cur_down -eq $down ]; then
14             cur_down=0
15             pos=$((pos + $right))
16             if [ $pos -ge ${#line} ]; then
17                 pos=$((pos - ${#line}))
18             fi
19             if [ ${line:$pos:1} = "#" ]; then
20                 tree_count=$((tree_count+1))
21             fi
22         fi
23     done
24     echo "$tree_count"
25 }
26
27 mult_total=1
28 for move in ${moves[@]}; do
29     down=${move#*,}
30     right=${move%,*}
31     trees=$(count_trees $right $down)
32     echo "Move: $move has $trees trees"
33     mult_total=$((mult_total * $trees))
34 done
35
36 echo "Answer: $mult_total"
37
38 exit 0
39
40 #!/usr/bin/python3
41
42 trees_count = []
43 on_first_line = True
44 line_number = 0
45
46 move_types = ((1,1), (3,1), (5,1), (7,1), (1,2))
47
48 for move in move_types:
49     line_offset = 0
50     tree_count = 0
51     position = move[0]
52     first_line = True
53     for line in open("input.txt", "r"):
54         line = line.rstrip()
55         print(move[1], line_offset)
56         if line_offset < move[1] or first_line:
57             line_offset += 1
58             if line_offset >= move[1]:
59                 first_line = False
60             continue
61         line_offset = 1
62         if line[position] == "#":
63             tree_count += 1
64         position += move[0]
65         if position >= len(line):
66             position = position - len(line)
67     trees_count.append(tree_count)
68     tree_count = 0
69
70 mult_result = 1
71 for x in trees_count:
72     print(x)
73     mult_result *= x
74
75 print("There were", mult_result, "trees")