Skip to content

Commit f7efe8d

Browse files
committed
this is dumb
1 parent e7b689e commit f7efe8d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

2024/day4/p1-alternate.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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

0 commit comments

Comments
 (0)