Move to have day be 2 digit always
[advent-of-code-2020.git] / day03 / count_trees_2.sh
diff --git a/day03/count_trees_2.sh b/day03/count_trees_2.sh
new file mode 100644 (file)
index 0000000..fde0ec3
--- /dev/null
@@ -0,0 +1,75 @@
+#!/bin/bash
+
+moves=("1,1" "3,1" "5,1" "7,1" "1,2")
+
+count_trees() {
+    right=$1
+    down=$2
+    cur_down=-1 # we want to skip the very first line
+    pos=0
+    tree_count=0
+    for line in $(<input.txt); do
+        cur_down=$((cur_down+1))
+        if [ $cur_down -eq $down ]; then
+            cur_down=0
+            pos=$((pos + $right))
+            if [ $pos -ge ${#line} ]; then
+                pos=$((pos - ${#line}))
+            fi
+            if [ ${line:$pos:1} = "#" ]; then
+                tree_count=$((tree_count+1))
+            fi
+        fi
+    done
+    echo "$tree_count"
+}
+
+mult_total=1
+for move in ${moves[@]}; do
+    down=${move#*,}
+    right=${move%,*}
+    trees=$(count_trees $right $down)
+    echo "Move: $move has $trees trees"
+    mult_total=$((mult_total * $trees))
+done
+
+echo "Answer: $mult_total"
+
+exit 0
+
+#!/usr/bin/python3
+
+trees_count = []
+on_first_line = True
+line_number = 0
+
+move_types = ((1,1), (3,1), (5,1), (7,1), (1,2))
+
+for move in move_types:
+    line_offset = 0
+    tree_count = 0
+    position = move[0]
+    first_line = True
+    for line in open("input.txt", "r"):
+        line = line.rstrip()
+        print(move[1], line_offset)
+        if line_offset < move[1] or first_line:
+            line_offset += 1
+            if line_offset >= move[1]:
+                first_line = False
+            continue
+        line_offset = 1
+        if line[position] == "#":
+            tree_count += 1
+        position += move[0]
+        if position >= len(line):
+            position = position - len(line)
+    trees_count.append(tree_count)
+    tree_count = 0
+
+mult_result = 1
+for x in trees_count:
+    print(x)
+    mult_result *= x
+
+print("There were", mult_result, "trees")