From 973ca9090337d17207e341b70119963022e2ddc9 Mon Sep 17 00:00:00 2001 From: Christian Segundo Date: Mon, 2 Dec 2024 09:30:01 +0100 Subject: add day 2 --- 1/1.sh | 64 ---------------------------------------------------------------- 1 file changed, 64 deletions(-) delete mode 100644 1/1.sh (limited to '1/1.sh') diff --git a/1/1.sh b/1/1.sh deleted file mode 100644 index a9ca200..0000000 --- a/1/1.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env bash - -function smallest { - local -n arr=$1 - smallest=${arr[0]} - for i in "${!arr[@]}"; do - [[ -n ${arr[i]} ]] || continue - [[ ${arr[i]} -lt $smallest ]] && smallest=${arr[i]} - done - echo "$smallest" -} - -function delete_a { - local value=$1 deleted=false new_arr=() - local -n arr=$2 - for i in "${arr[@]}"; do - [[ $deleted == true ]] && new_arr+=($i) && continue - [[ $i -ne $value ]] && new_arr+=($i) && continue - deleted=true - done - echo "${new_arr[@]}" -} - -function occurences { - local value=$1 count=0 - local -n arr=$2 - for i in "${arr[@]}"; do - [[ $i -eq $value ]] && count=$((count + 1)) - done - echo "$count" -} - -one=() -two=() -total_distance=0 -total_count=0 - -while read -r line; do - one+=("${line%% *}") - two+=("${line##* }") -done <./input.txt - -for i in $(seq 0 $((${#one[@]} - 1))); do - n="${one[i]}" - total_count=$((total_count + n * $(occurences "$n" two))) -done - -for i in $(seq 1 ${#one[@]}); do - smallest_one=$(smallest one) - smallest_two=$(smallest two) - one=($(delete_a $smallest_one one)) - two=($(delete_a $smallest_two two)) - distance=$((smallest_one - smallest_two)) - if [[ $distance -lt 0 ]]; then - distance=$((distance * -1)) - fi - total_distance=$((total_distance + distance)) -done - -echo "puzzle 1: $total_distance" -echo "puzzle 2: $total_count" - -# puzzle 1: 2285373 -# puzzle 2: 21142653 -- cgit v1.2.3