aboutsummaryrefslogtreecommitdiff
path: root/2025/06/puzzle-2.sh
blob: cba270f7c9cdd3f5d03b3d6df613c82087f1c4e5 (plain) (blame)
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
#!/bin/sh

{
	printf '('
	for i in $(seq "$(head -n1 input | wc -c)")
	do
		num="$(cut -c $i input | paste -sd '')"
		num="${num%${num##*[![:space:]]}}"

		case "$num" in
		*[+*])
			test -n "$op" && printf ') + ('
			op="${num#${num%?}}"
			num="${num%?}"
			printf '%d %s ' $num "$op"
			;;
		'')
			test "$op" = +
			printf '%d' $?
			;;
		*)
			printf '%d %s ' $num "$op"
			;;
		esac
	done
	printf ')\n'
} | bc