File tree 12 files changed +75
-73
lines changed
12 files changed +75
-73
lines changed Original file line number Diff line number Diff line change 1
1
#! /usr/bin/env bash
2
- input=${1:- 1.txt}
3
- A=($( sort -n " $input " ) );
4
- for a in " ${A[@]} " ; do grep -q -- ^$(( 2020 - a)) $ " $input " && break ; done
5
- echo " 1A: $(( 2020 - a)) + $a = $(( (2020 - a)* a)) "
6
- b=:; for a in " ${A[@]} " ; do for i in " ${A[@]} " ; do grep -q -- ^$(( 2020 - a- i)) $ " $input " && b=break; $b ; done ; $b ; done
7
- echo " 1B: $(( 2020 - a- i)) + $a + $i = $(( (2020 - a- i)* a* i)) "
2
+ A=($( sort -n " ${1:- 1.txt} " ) )
3
+ for i in " ${A[@]} " ; do B[i]=1; done
4
+ for a in " ${A[@]} " ; do (( b= 2020 - a, B[b] > 0 )) && break ; done
5
+ echo " 1A: $b + $a = $(( b* a)) "
6
+ for i in " ${! A[@]} " ; do for b in " ${A[@]: i+1} " ; do
7
+ (( a= A[i], c= 2020 - a- b, c > 0 && B[c] > 0 )) && break 2;
8
+ done ; done
9
+ echo " 1B: $c + $a + $b = $(( c* a* b)) "
Original file line number Diff line number Diff line change 1
1
#! /usr/bin/env bash
2
- ans=0
3
- IFS=$' -:\ \n '
4
- while read -r m M b c; do x=${c// [^$b]} ; (( ${# x} >= m && ${# x} <= M && ++ ans)) ; done < " ${1:- 2.txt} "
2
+ ans=0 ans2=0
3
+ IFS=$' -:\n '
4
+ while read -r m M b c; do
5
+ x=${c// [^$b]} ; y=${c: m-1: 1} ; z=${c: M-1: 1} ;
6
+ (( ${# x} >= m && ${# x} <= M && ++ ans ))
7
+ [[ $y != " $z " ]] && [[ $b == " $y " || $b == " $z " ]] && (( ++ ans2 ))
8
+ done < " ${1:- 2.txt} "
5
9
echo " 2A: $ans "
6
- ans=0
7
- while read -r m M b c; do x=${c: m-1: 1} ; y=${c: M-1: 1} ; [[ $x != " $y " ]] && [[ $b == " $x " || $b == " $y " ]] && (( ++ ans)) ; done < " ${1:- 2.txt} "
8
- echo " 2B: $ans "
10
+ echo " 2B: $ans2 "
Original file line number Diff line number Diff line change 1
1
#! /usr/bin/env bash
2
- A=($( < " ${1:- 3.txt} " ) ); l=${# A[0]} ; k=0; j=3; total=1; x=" "
3
- for i in " ${A[@]} " ; do x+=${i: k: 1} ; (( k= (k+ j)% l)) ; done ; x=${x// \. }
4
- echo " 3A: ${# x} "
5
- idx2=$( seq 0 2 $(( ${# A[@]} - 1 )) )
6
- for j in 1 3 5 7; do x=" " ; k=0; for i in " ${A[@]} " ; do x+=${i: k: 1} ; (( k= (k+ j)% l)) ; done ; x=${x// \. } ; total+=" *${# x} " ; done
7
- x=" " ; k=0; for i in $idx2 ; do x+=${A[i]: k: 1} ; (( k= (k+ 2 )% l)) ; done ; x=${x// \. } ; total+=" *${# x} "
8
- echo " 3B: ${total: 2} = $(( total)) "
2
+ A=($( < " ${1:- 3.txt} " ) ); l=${# A[0]} ; N=${# A[@]} ; k=0; j=3; x=" "
3
+ for i in " ${A[@]} " ; do x+=${i: k: 1} ; (( k= (k+ j)% l)) ; done ; x=${x// \. } ; total=${# x}
4
+ echo " 3A: $total "
5
+ for j in 1 5 7; do x=" " ; k=0; for i in " ${A[@]} " ; do x+=${i: k: 1} ; (( k= (k+ j)% l)) ; done ; x=${x// \. } ; total+=" *${# x} " ; done
6
+ x=" " ; k=0; for (( i= 0 ;i< N;i+= 2 )) ; do x+=${A[i]: k: 1} ; (( k= (k+ 1 )% l)) ; done ; x=${x// \. } ; total+=" *${# x} "
7
+ echo " 3B: ${total} = $(( total)) "
Original file line number Diff line number Diff line change @@ -20,8 +20,8 @@ solve8 || echo "8A: $acc"
20
20
21
21
A=(" ${B[@]} " );
22
22
for k in " ${! A[@]} " ; do
23
- if [ ${A[k]: 0 : 3 } = nop ] ; then A[k]=${A[k]/ nop / jmp } ;
24
- elif [ ${A[k]: 0 : 3 } = jmp ] ; then A[k]=${A[k]/ jmp / nop } ;
23
+ if [[ ${A[k]} = nop* ]] ; then A[k]=jmp ${A[k]: 3 } ;
24
+ elif [[ ${A[k]} = jmp* ]] ; then A[k]=nop ${A[k]: 3 } ;
25
25
else continue ; fi
26
26
solve8 && break
27
27
A=(" ${B[@]} " )
Original file line number Diff line number Diff line change 2
2
A=($( < " ${1:- 9.txt} " ) )
3
3
idx=(${! A[@]} )
4
4
for i in " ${idx[@]: 25} " ; do
5
- b=:
6
5
for j in " ${idx[@]: i-25: 25-1} " ; do
7
- [[ ${ A[j]} -ge ${ A[i]} ]] && continue
6
+ (( A[j] >= A[i])) && continue
8
7
for k in " ${idx[@]: j+1: i-j-1} " ; do
9
- (( A[j]+ A[k] == A[i])) && b= break; $b
8
+ (( A[j]+ A[k] == A[i])) && break 2
10
9
done
11
- $b
12
10
done
13
- [ " $b " = " : " ] && echo " 9A: line $i = ${ A[i]} " && break
11
+ (( A[j] + A[k] != A[i])) && break
14
12
done
13
+ echo " 9A: line $i = ${A[i]} "
15
14
16
15
k=0; j=0; sum=${A[j++]}
17
16
while [ " $sum " != " ${A[i]} " ]; do
18
- if [[ $ sum -lt ${ A[i]} ]] ; then (( sum+= A[j++ ]))
19
- elif [[ $ sum -gt ${ A[i]} ]] ; then (( sum-= A[k++ ]))
17
+ if (( sum < A[i])) ; then (( sum+= A[j++ ]))
18
+ elif (( sum > A[i])) ; then (( sum-= A[k++ ]))
20
19
fi
21
- [[ $j -ge $i ]] && echo NOT FOUND && break
20
+ (( j >= i )) && echo NOT FOUND && break
22
21
done
23
- B=($( printf " %s\n" " ${A[* ]: k: j-k} " | sort -n) )
22
+ B=($( printf " %s\n" " ${A[@ ]: k: j-k} " | sort -n) )
24
23
echo " 9B: $k ..$j = $(( B[0 ]+ B[- 1 ])) "
Original file line number Diff line number Diff line change @@ -12,12 +12,11 @@ printf -v sum "+%s" "${mem[@]}"
12
12
echo " 14A: $(( sum)) "
13
13
14
14
r () {
15
- local x=${2/ X/ 0}
16
- if [ " $x " = " $2 " ]; then
17
- mem2[$1 | (2#$x )]=$3
18
- else
19
- r " $1 " " $x " " $3 "
15
+ if [[ $2 == * X* ]]; then
16
+ r " $1 " " ${2/ X/ 0} " " $3 "
20
17
r " $1 " " ${2/ X/ 1} " " $3 "
18
+ else
19
+ mem2[$1 | (2#$2 )]=$3
21
20
fi
22
21
}
23
22
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ A=($(grep "," "$input"))
6
6
sum=" "
7
7
for i in " ${! A[@]} " ; do
8
8
for j in ${A[i]// ,/ } ; do
9
- (( j < min || j > max )) && sum+=+$j && A[i]=" "
9
+ (( j < min || j > max )) && sum+=+$j A[i]=" "
10
10
done
11
11
done
12
12
echo " 16A: $(( sum)) "
@@ -20,7 +20,7 @@ while read -r name min1 max1 _ min2 max2; do # Read min/max values and field nam
20
20
Cm1[i]=$min1 ; CM1[i]=$max1
21
21
Cm2[i]=$min2 ; CM2[i]=$max2
22
22
C[i++]=$name
23
- done < <( sed -n -e " / .*:/ s/ /_/;/or/p" " $input " )
23
+ done < <( sed -n -e " / .*:/s/ /_/;/or/p" " $input " )
24
24
IFS=$' \n '
25
25
idx=${! C[*]}
26
26
for line in " ${B[@]} " ; do
@@ -38,9 +38,9 @@ total=1
38
38
while [[ " $oldC " != " ${Cf[*]} " ]]; do
39
39
oldC=" ${Cf[*]} "
40
40
for i in $idx ; do
41
- if [ ${# Cf[i]} = 1 ] ; then
41
+ if [[ ${# Cf[i]} == 1 ]] ; then
42
42
x=${Cf[i]}
43
- [ " ${C[i]: 0 : 3} " = dep ] && total+=" *${my_ticket[36#$i ]} "
43
+ [[ ${C[i]} = dep* ]] && total+=" *${my_ticket[36#$x ]} "
44
44
Cf=(" ${Cf[@]// $x } " )
45
45
fi
46
46
done
Original file line number Diff line number Diff line change 6
6
local i sum=0 op=' +'
7
7
# shellcheck disable=SC2048
8
8
for i in $* ; do
9
- if [[ $i = ' + ' || $i = ' * ' ]]; then
9
+ if [[ $i == [+ * ] ]]; then
10
10
op=$i
11
11
else
12
12
(( sum$op = i))
Original file line number Diff line number Diff line change @@ -9,29 +9,30 @@ for i in "${A[@]}"; do
9
9
done
10
10
11
11
IFS=$' \n '
12
- REGEX=()
12
+ REGEX=() regex=" "
13
+ # shellcheck disable=SC2034,SC2048
13
14
r (){
14
- local x=" "
15
- # shellcheck disable=SC2048
16
- for i in $* ; do
17
- if [ -z " ${i/ [ab|]} " ]; then
18
- x+=$i
19
- else
20
- [[ -z " ${REGEX[$i]} " ]] && REGEX[$i ]=" ($( r " ${RULE[$i]} " ) )"
21
- x+=" ${REGEX[$i]} "
22
- # x+="($(r "${RULE[$i]}"))"
23
- fi
15
+ local -n rx=$1 ; local x=" " i
16
+ for i in ${*: 2} ; do
17
+ if [ -z " ${i/ [ab|]} " ]; then
18
+ x+=$i
19
+ else
20
+ [[ -z " ${REGEX[i]} " ]] && r " REGEX[$i ]" " ${RULE[i]} "
21
+ x+=${REGEX[i]}
22
+ fi
24
23
done
25
- echo " $x "
24
+ [[ ${x} == * \| * ]] && x=" ($x )" # Only brackets if needed
25
+ rx=$x
26
26
}
27
- regex= $( r 0 )
28
- echo " 19A: $( grep -E -c " ^${regex} $" " $input " ) "
27
+ r regex 0
28
+ printf " 19A: " ; grep -E -c " ^${regex} $" " $input "
29
29
30
30
# RULE[8]="42 8"
31
31
# RULE[11]="42 31 | 42 11 31"
32
- r42=" $( r 42) "
33
- r31=" $( r 31) "
34
- r8=" ${r42} {1,5}"
35
- r11=" ${r42}${r31} "
36
- for n in {2..5}; do r11+=" |${r42} {$n }${r31} {$n }" ; done
37
- echo " 19B: $( grep -E -c " ^($r8 )($r11 )$" " $input " ) "
32
+ REGEX[8]=" (${REGEX[42]} {1,5})"
33
+ REGEX[11]=" ${REGEX[42]}${REGEX[31]} "
34
+ for n in {2..5}; do REGEX[11]+=" |${REGEX[42]} {$n }${REGEX[31]} {$n }" ; done
35
+ REGEX[11]=" (${REGEX[11]} )"
36
+ REGEX[0]=" " regex=" "
37
+ r regex 0
38
+ printf " 19B: " ; grep -E -c " ^${regex} $" " $input "
Original file line number Diff line number Diff line change 20
20
IFS=$' \n '
21
21
ans=$( grep -o " .*(" " $input " | grep -o " [a-z]*" | grep -v -c -F " ${B[*]} " )
22
22
echo " 21A: $ans "
23
- IFS=,
23
+ IFS=$' , '
24
24
echo " 21B: ${B[*]} "
Original file line number Diff line number Diff line change 2
2
A=$( < " ${1:- 23.txt} " )
3
3
i=0
4
4
for _ in {0..99}; do
5
- c=${A: i: 1}
6
- out=${A: i+1: 3}
7
- A2=${A/ $out }
8
- [ ${# out} -lt 3 ] && A2=${A2: 3-${# out} } && out+=${A: 0: 3-${# out} }
9
- d=${A: i: 1} ; (( -- d)) || d=9; while [ " ${out/ $d } " != " ${out} " ]; do (( -- d)) || d=9; done
10
- A=${A2/ $d / $d ${out} }
11
- # echo $A : $c : $A2-$d:$out
12
- while [ " ${A: i: 1} " != " $c " ]; do (( ++ i== 9 )) && i=0; done
13
- (( ++ i== 9 )) && i=0
5
+ c=${A: i: 1}
6
+ out=${A: i+1: 3}
7
+ A2=${A/ $out }
8
+ [ ${# out} -lt 3 ] && A2=${A2: 3-${# out} } && out+=${A: 0: 3-${# out} }
9
+ d=${A: i: 1} ; (( -- d)) || d=9; while [ " ${out/ $d } " != " ${out} " ]; do (( -- d)) || d=9; done
10
+ A=${A2/ $d / $d ${out} }
11
+ # echo $A : $c : $A2-$d:$out
12
+ while [ " ${A: i: 1} " != " $c " ]; do (( ++ i== 9 )) && i=0; done
13
+ (( ++ i== 9 )) && i=0
14
14
done
15
15
echo " 23A: ${A/* 1}${A/ 1* } "
16
16
A=$( < " ${1:- 23.txt} " ) a=0 b=0 c=0
Original file line number Diff line number Diff line change @@ -11,8 +11,8 @@ Setting env variable PUREBASH to any value (or giving a second argument) will us
11
11
Description of what I'm doing. Contains spoilers....
12
12
13
13
### 01.sh
14
- 1 . Simple grep . Loop through all the numbers looking for 2020-i .
15
- 2 . Double loop through all the numbers looking for 2020-i-j .
14
+ 1 . Use an array as a hash table . Loop through all the numbers looking for 2020-a .
15
+ 2 . Double loop through all the numbers looking for 2020-a-b .
16
16
17
17
### 02.sh
18
18
Simple read and string manipulation. Use IFS to split up the min/max and remove the ':'.
@@ -97,7 +97,7 @@ Description of what I'm doing. Contains spoilers....
97
97
2 . Just replace "\* " with ")\* (" to change the precedence. Add "(" and ")" to the ends to close off the brackets.
98
98
99
99
### 19.sh
100
- 1 . Recursive regex building. Use a cache to speed up previously built regex.
100
+ 1 . Recursive regex building. Use a cache to only build a regex once for each rule .
101
101
2 . Cheat a bit and use regex{n} to denote repeated matches.
102
102
103
103
### 20.sh
You can’t perform that action at this time.
0 commit comments