From 1ae258d79ba91cb6215a62a011fca7936c06d9df Mon Sep 17 00:00:00 2001 From: Brett Parker Date: Fri, 4 Dec 2020 18:29:23 +0000 Subject: [PATCH] Add bash variants --- day2/get_valid_count.sh | 44 +++++++++ day2/get_valid_count_2.sh | 24 +++++ day3/count_trees.sh | 51 +++++++++++ day3/count_trees_2.sh | 75 ++++++++++++++++ day4/count_valid_passports.sh | 44 +++++++++ day4/count_valid_passports_2.sh | 155 ++++++++++++++++++++++++++++++++ 6 files changed, 393 insertions(+) create mode 100644 day2/get_valid_count.sh create mode 100644 day2/get_valid_count_2.sh create mode 100644 day3/count_trees.sh create mode 100644 day3/count_trees_2.sh create mode 100644 day4/count_valid_passports.sh create mode 100644 day4/count_valid_passports_2.sh diff --git a/day2/get_valid_count.sh b/day2/get_valid_count.sh new file mode 100644 index 0000000..d6ca89e --- /dev/null +++ b/day2/get_valid_count.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +check_file() { + cat input.txt | while read line; do + password=${line#*: } + params=${line%: *} + min=${params%-*} + params=${params#*-} + max=${params% *} + char=${params#* } + count=0 + for ((i=0; i<${#password}; i++)); do + if [ "${password:$i:1}" = "$char" ]; then + count=$((count+1)) + fi + done + if [ $count -ge $min ] && [ $count -le $max ]; then + echo "Got valid line!" + fi + done +} + +echo "Got $(check_file | wc -l) valid lines" + +exit 0 +#!/usr/bin/python3 + +import regex + +total_lines=0 +valid_lines=0 +for line in open("input.txt", "r"): + total_lines += 1 + (min_count, max_count, letter, password) = regex.match('([0-9]+)-([0-9]+) ([a-z]): ([a-z]*)', line).group(1,2,3,4) + min_count=int(min_count) + max_count=int(max_count) + count = 0 + for x in password: + if x == letter: + count += 1 + if count >= min_count and count <= max_count: + valid_lines += 1 + +print(valid_lines, "valid lines of", total_lines) diff --git a/day2/get_valid_count_2.sh b/day2/get_valid_count_2.sh new file mode 100644 index 0000000..7226ae4 --- /dev/null +++ b/day2/get_valid_count_2.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +check_file() { + cat input.txt | while read line; do + password=${line#*: } + params=${line%: *} + min=${params%-*} + params=${params#*-} + max=${params% *} + char=${params#* } + count=0 + min=$((min-1)) + max=$((max-1)) + if [ "${password:$min:1}" = "$char" ] || [ "${password:$max:1}" = "$char" ]; then + if [ "${password:$min:1}" != "${password:$max:1}" ]; then + echo "Got valid line! $line" + fi + fi + done +} + +echo "Got $(check_file | wc -l) valid lines" + +exit 0 diff --git a/day3/count_trees.sh b/day3/count_trees.sh new file mode 100644 index 0000000..53925a3 --- /dev/null +++ b/day3/count_trees.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +start_letter=0 +line_number=0 +tree_count=0 + +for line in $( 3 +on_first_line = True +line_number = 0 + +for line in open("input.txt", "r"): + line_number += 1 + print("line: ", line_number) + line = line.rstrip() + print(line) + if on_first_line: + on_first_line = False + continue + print(line[position], position) + if line[position] == "#": + trees_count += 1 + print("hit tree") + else: + print("missed tree") + # now add 3 to the position + position += 3 + if position >= len(line): + position = position - len(line) + +print("There were", trees_count, "trees") diff --git a/day3/count_trees_2.sh b/day3/count_trees_2.sh new file mode 100644 index 0000000..fde0ec3 --- /dev/null +++ b/day3/count_trees_2.sh @@ -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 $(= 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") diff --git a/day4/count_valid_passports.sh b/day4/count_valid_passports.sh new file mode 100644 index 0000000..17c7ffc --- /dev/null +++ b/day4/count_valid_passports.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +declare -a data +cur_data="" +seperator="" +valid_count=0 + +required_fields=(byr iyr eyr hgt hcl ecl pid) +exec 3