summaryrefslogtreecommitdiff
path: root/1/1.sh
diff options
context:
space:
mode:
Diffstat (limited to '1/1.sh')
-rw-r--r--1/1.sh64
1 files changed, 0 insertions, 64 deletions
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