-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2022_25.sh
executable file
路66 lines (59 loc) 路 1.29 KB
/
AoC2022_25.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
#
# Advent of Code 2022 Day 25
#
year=2022
day=25
declare -A decode=([0]=0 [1]=1 [2]=2 [-]=-1 [=]=-2)
declare -A encode=([0]="0,0" [1]="1,0" [2]="2,0" [3]="=,1" [4]="-,1" [5]="0,1")
part1() {
local -i total
while read -r line || [ -n "$line" ]; do
local -i subtotal=0
for ((i = 0; i < "${#line}"; i++)); do
coefficient="${decode[${line:i:1}]}"
exponent=$((${#line} - 1 - i))
((subtotal += coefficient * (5 ** exponent)))
done
((total += subtotal))
done < "$1"
local ans=""
for (( ; ; )); do
pair="${encode[$((total % 5))]}"
digit="${pair%,*}"
carry="${pair#*,}"
ans="$digit$ans"
total=$((total / 5 + carry))
if [[ "$total" = 0 ]]; then
echo "$ans"
return 0
fi
done
_fatal 1 "Unsolvable"
}
part2() {
echo "0"
return 0
}
tests() {
# shellcheck disable=SC2034
{
sample=(
"1=-0-2"
"12111"
"2=0="
"21"
"2=01"
"111"
"20012"
"112"
"1=-1="
"1-12"
"12"
"1="
"122"
)
}
TEST part1 sample "2=-1=0"
}
source "$(dirname "$0")/aoc_main.sh"