5 filename="${1:-input.txt}"
10 declare -a all_outputs
12 while read -u 3 -d "|" inputs; do
13 all_inputs+=("$inputs")
15 all_outputs+=("$outputs")
18 one_count=0 # 2 on signals
19 seven_count=0 # 3 on signals
20 four_count=0 # 4 on signals
21 eight_count=0 # 7 on signals
23 for output in "${all_outputs[@]}"; do
24 read -a seperates <<<$output
25 for s in "${seperates[@]}"; do
43 echo "1 count: $one_count"
44 echo "4 count: $four_count"
45 echo "7 count: $seven_count"
46 echo "8 count: $eight_count"
48 echo "Total: $((one_count+$four_count+$seven_count+$eight_count))"
50 declare -A seg_vals=( [a]=1 [b]=2 [c]=4 [d]=8 [e]=16 [f]=32 [g]=64 )
51 declare -A char_map=( [119]=0 [36]=1 [93]=2 [109]=3 [46]=4 [107]=5 [123]=6 [37]=7 [127]=8 [111]=9 )
56 local -A segcounts=( [a]=0 [b]=0 [c]=0 [d]=0 [e]=0 [f]=0 [g]=0 )
57 local -A segmap=( [a]= [b]= [c]= [d]= [e]= [f]= [g]= )
59 read -a segs <<<"${input}"
60 for seg in "${segs[@]}"; do
61 for (( a=0; a<${#seg}; a++ )); do
62 ((segcounts[${seg:$a:1}]+=1))
66 # we've now got some counts, so we can do *some* of the mappings
67 for seg_name in "${!segcounts[@]}"; do
68 case ${segcounts[$seg_name]} in
87 # now go through segs again, this time checking how long the string is, because from that
88 # we can find 7, which will give us a.
90 for seg in "${segs[@]}"; do
93 # this is number 1 - it has a c but no a
94 for (( a=0; a<${#seg}; a++ )); do
95 if [ ${segmap[${seg:$a:1}]} == "ac" ]; then
100 # because we've worked out where a is, we now go find the one that thinks it's a or c
101 for seg_name in "${!segmap[@]}"; do
102 if [ "${segmap[$seg_name]}" == "ac" ]; then
110 for (( a=0; a<${#seg}; a++ )); do
111 if [ ${segmap[${seg:$a:1}]} == "dg" ]; then
112 segmap[${seg:$a:1}]=d
116 for seg_name in "${!segmap[@]}"; do
117 if [ "${segmap[$seg_name]}" == "dg" ]; then
128 # finally, we can get each of the digits in the output, so go through the output
129 read -a charsets <<<"${output}"
130 for chars in "${charsets[@]}"; do
132 for (( a=0; a<${#chars}; a++ )); do
133 ((char_value+=${seg_vals[${segmap[${chars:$a:1}]}]}))
135 if [ "${char_map[$char_value]+abc}" ]; then
136 output_digits+=${char_map[$char_value]}
138 declare -p segmap >&2
139 echo "Chars: $chars" >&2
144 # remove any leading 0s the cheating way
145 echo $((10#$output_digits))
148 # Now lets start playing with the first input and try working out the outputs
152 for (( a=0; a<${#all_inputs[@]}; a++ )); do
153 ((total_output_count+=$(get_digits "${all_inputs[$a]}" "${all_outputs[$a]}")))
156 echo "Sum of all output digits: $total_output_count"