aoc2024

advent of code 2024 version unix - retour accueil

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

2.solve (945B)


      1 #! /bin/sh
      2 
      3 echo "part 1"
      4 
      5 < 2.input awk '{
      6 	split($0,a," ")
      7 	if (a[2]-a[1] > 0) { upordown="up" } else { upordown="down" }
      8 	for(i=1;i<length(a);i++) {
      9 		diff=a[i+1]-a[i]
     10 		if ( diff > 3 || diff < -3 || diff==0 ) {
     11 			print $0" not safe to large of a diff or 0"
     12 			next
     13 		}
     14 		if ( upordown=="up" && a[i+1]-a[i]<0 || upordown=="down" && a[i+1]-a[i]>0 ) {
     15 			print $0" not safe wrong way"
     16 			next
     17 		}
     18 	}
     19 	print $0" safe"
     20 }' | grep 'safe$' | wc -l
     21 
     22 echo "part 2"
     23 
     24 < 2.input awk '{
     25 	damp=0
     26 	split($0,a," ")
     27 	if (a[2]-a[1] > 0) { upordown="up" } else { upordown="down" }
     28 	for(i=1;i<length(a);i++) {
     29 		diff=a[i+1]-a[i]
     30 		if ( diff > 3 || diff < -3 || diff==0 ) {
     31 			if (damp==1) { print $0" not safe to large of a diff or 0";next}
     32 			else damp=1
     33 			next
     34 		}
     35 		if ( upordown=="up" && a[i+1]-a[i]<0 || upordown=="down" && a[i+1]-a[i]>0 ) {
     36 			if (damp==1) { print $0" not safe to large of a diff or 0"; next }
     37 			else damp=1
     38 		}
     39 	}
     40 	print $0" safe"
     41 }'
     42