File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ #! /bin/bash
2
+
3
+ function transpose() {
4
+ awk ' {
5
+ split($0, chars, "")
6
+ for(j=1; j<=length(chars); j++) {
7
+ idx=(j)
8
+ a[idx]=a[idx]chars[j]
9
+ }
10
+ }
11
+ END{ for (i=1; i<=NR; i++) print a[i] }
12
+ '
13
+ }
14
+
15
+ FILE=$1
16
+
17
+ SIZE=$( wc -l $FILE | cut -d' ' -f1)
18
+ A=$(( SIZE+ 1 ))
19
+ B=$(( SIZE- 1 ))
20
+
21
+ {
22
+ # horizontals
23
+ cat $FILE | grep -o " XMAS" | wc -l
24
+ cat $FILE | rev | grep -o " XMAS" | wc -l
25
+
26
+ # verticals
27
+ cat $FILE | transpose | grep -o " XMAS" | wc -l
28
+ cat $FILE | transpose | rev | grep -o " XMAS" | wc -l
29
+
30
+ # diagonals
31
+ tac $FILE | tr ' \n' ' .' | grep -oP " X(?=.{$A }M.{$A }A.{$A }S)" | wc -l
32
+ cat $FILE | tr ' \n' ' .' | grep -oP " X(?=.{$A }M.{$A }A.{$A }S)" | wc -l
33
+ tac $FILE | tr ' \n' ' .' | grep -oP " X(?=.{$B }M.{$B }A.{$B }S)" | wc -l
34
+ cat $FILE | tr ' \n' ' .' | grep -oP " X(?=.{$B }M.{$B }A.{$B }S)" | wc -l
35
+ } | paste -sd+ | bc
You can’t perform that action at this time.
0 commit comments