aoc2024

advent of code 2024 version unix - retour accueil

git clone git://bebou.netlib.re/aoc2024
Log | Files | Refs |

4.solve (1312B)


      1 #! /bin/sh
      2 
      3 echo "part 1"
      4 
      5 exec 3>&1 1> all
      6 
      7 # Horizontal
      8 
      9 cat 4.input
     10 
     11 # Vertical
     12 
     13 ## AWK
     14 
     15 < 4.input awk '
     16 { split($0,a,"");for(i=0;i<length(a);i++) f[NR,i]=a[i] }
     17 END { for(i=0;i<length(a);i++) {
     18 		for(j=0;j<length(a);j++) printf f[j,i]
     19 		printf "\n" } }
     20 '
     21 
     22 ## Shell
     23 
     24 for i in $(seq 1 140);do
     25 	< 4.input cut -c$i | paste -s -d'\0'
     26 done | wc -l > /dev/null
     27 
     28 # Diagonals
     29 
     30 < 4.input awk  '
     31 {split($0,a,""); la=length(a)+1;for(i=0;i<la;i++) f[NR,i]=a[i] }
     32 END {
     33 	for(i=0;i<la;i++) printf f[i,i]
     34 	printf "\n"
     35 	for(i=0;i<la;i++) printf f[la-i,i]
     36 	printf "\n"
     37 	for(i=1;i<la;i++) {
     38 		for(j=0;j<la;j++) printf f[j,j+i]
     39 		printf "\n"
     40 		for(j=0;j<la;j++) printf f[j+i,j]
     41 		printf "\n"
     42 		for(j=0;j<la;j++) printf f[la-j,j+i]
     43 		printf "\n"
     44 		for(j=0;j<la;j++) printf f[la-j-i,j]
     45 		printf "\n"
     46 	}
     47 	printf "\n"
     48 }
     49 '
     50 
     51 exec 1>&3 3>&-
     52 
     53 echo $((  $( < all grep -Eo "XMAS" | grep -v '^$' | wc -l ) \
     54         + $(  < all rev | grep -Eo "XMAS" | grep -v '^$' | wc -l ) ))
     55 
     56 echo "part 2"
     57 
     58 < 4.input awk '
     59 {split($0,a,""); la=length(a)+1;for(i=0;i<la;i++) f[NR-1,i]=a[i+1]}
     60 END {
     61 	la=length(a)+1
     62 	for(j=0;j<la-3;j++) {
     63 		for(i=0;i<la-3;i++) {
     64 			printf f[j,i] f[j,i+1] f[j,i+2]
     65 			printf f[j+1,i] f[j+1,i+1] f[j+1,i+2]
     66 			print  f[j+2,i] f[j+2,i+1] f[j+2,i+2]
     67 		}
     68 	}
     69 }
     70 ' | grep -Eo "(M.S.A.M.S|S.S.A.M.M|M.M.A.S.S|S.M.A.S.M)" | wc -l
     71