File tree Expand file tree Collapse file tree 4 files changed +98
-0
lines changed Expand file tree Collapse file tree 4 files changed +98
-0
lines changed Original file line number Diff line number Diff line change
1
+ #! /usr/bin/env bash
2
+ input=${1:- 5.txt}
3
+ swap (){
4
+ local -n _x=$1 _y=$2
5
+ local tmp=$_x ; _x=$_y ; _y=$tmp
6
+ }
7
+ IFS=$' ,\n ' ; ANS=0; ANS2=0; declare -Ai C=() D=()
8
+ while read -r x y _ X Y; do
9
+ if (( x == X )) ; then
10
+ (( y > Y )) && swap y Y
11
+ while (( y<= Y)) ; do C[$x .$y ]+=1; (( y++ )) ; done
12
+ elif (( y == Y )) ; then
13
+ (( x > X )) && swap x X
14
+ while (( x<= X)) ; do C[$x .$y ]+=1; (( x++ )) ; done
15
+ elif (( (X- x) == (Y- y) || (X- x) == - (Y- y) )) ; then
16
+ (( x > X )) && swap x X && swap y Y
17
+ if (( y > Y )) ; then i=-1; else i=1; fi
18
+ while (( x<= X)) ; do D[$x .$y ]+=1; (( x++, y+= i)) ; done
19
+ fi
20
+ done < " $input "
21
+ ANS=(${C[@]// 1} ) # Lazy count. Fails if there are 11 crossings
22
+ echo " 5A: ${# ANS[@]} "
23
+ for i in " ${! C[@]} " ; do D[$i ]+=${C[$i]} ; done
24
+ ANS2=(${D[@]// 1} )
25
+ echo " 5B: ${# ANS2[@]} "
Original file line number Diff line number Diff line change
1
+ #! /usr/bin/env bash
2
+ A=$( < " ${1:- 6.txt} " )
3
+ A=${A// ,}
4
+ solve () {
5
+ for (( ; n < $1 ; ++ n)) ; do
6
+ # births=${A//[^0]}; births=${births//0/8}
7
+ births=$( tr -cd 0 <<< " $A" | tr 0 8)
8
+ B=$( echo -n " $A " | tr 876543210 765432106 )
9
+ A[i]=" ${B[i]}${births} "
10
+ done
11
+ }
12
+ n=0
13
+ solve 80
14
+ echo " 6A: ${# A} "
15
+ # solve 256
16
+ # echo "6B: ${#A}"
Original file line number Diff line number Diff line change
1
+ #! /usr/bin/env bash
2
+ A=($( tr , ' \n' < " ${1:- 7.txt} " | sort -n ) )
3
+ MIN=(99999999 0) MIN2=(9999999999999 0)
4
+ N=${# A[@]}
5
+ for (( i= A[N/ 4 ]; i< A[- N/ 4 ]; i++ )) ; do
6
+ n=' ' ; for k in ${A[@]} ; do n+=+$(( k- i)) ; done ;
7
+ sum=$(( ${n// -} )) # lazy abs()
8
+ (( MIN> sum)) && MIN=($sum $i )
9
+ done
10
+ echo " 7A: ${MIN[0]} "
11
+
12
+ for (( i= A[N/ 4 ]; i< A[- N/ 4 ]; i++ )) ; do
13
+ declare -i sum2=0;
14
+ for k in ${A[@]} ; do
15
+ dist=$(( k- i)) ; dist=${dist/ -}
16
+ sum2+=$(( dist* (dist+ 1 )/ 2 ))
17
+ done ;
18
+ (( MIN2 > sum2 )) && MIN2=($sum2 $i )
19
+ done
20
+ echo " 7B: ${MIN2[0]} "
Original file line number Diff line number Diff line change
1
+ # adventofcode.sh
2
+ Advent of Code 2021, done in bash. Because I can't stop
3
+ https://adventofcode.com/2021/
4
+
5
+ Input can be given on the command line.
6
+ Defaults to * number* .txt in the same folder (no leading 0).
7
+ Added my input files in input/ folder.
8
+ Setting env variable PUREBASH to any value (or giving a second argument) will use slow bash instead of using faster awk when needed.
9
+
10
+ Description of what I'm doing. Contains spoilers....
11
+
12
+ ### 01.sh
13
+ 1 . Dumb looping through all variables.
14
+ 2 . Use a sliding window. 2/3 of the values in the windows are the same, but this is fine.
15
+
16
+ ### 02.sh
17
+ Simple switch to handle all cases. Both parts handled at once.
18
+
19
+ ### 03.sh
20
+ 1 . Slightly tricky. Use a function to find the most/least common number in each position.
21
+ 2 . Same function, but change the input for every char.
22
+
23
+ ### 04.sh
24
+ 1 . Use an array to store all lines/rows of a bingo card. Delete numbers from all lines until a line is empty.
25
+ 2 . Remove a card when a line is empty. Keep going until all cards are gone.
26
+
27
+ ### 05.sh
28
+ 1 . Mark all points on the grid in a hash table. Return a count of points that are != 1.
29
+ 2 . Mark diagonals on a second hash table. Add the first hash table to it and return a count of points that are != 1.
30
+
31
+ ### 06.sh
32
+ 1 . String manipulation. Bad O(), but finishes part 1 in half a second.
33
+ 2 . Too slow...
34
+
35
+ ### 07.sh
36
+ 1 . Brute force search.
37
+ 2 . More brute force.
You can’t perform that action at this time.
0 commit comments