-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03.az.sh
executable file
·99 lines (86 loc) · 2.77 KB
/
03.az.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh
# Spam facts about the problem, until we have enough facts to solve it.
#
# The Allez Cuisine theme for today was spam. This solution writes a very, very,
# long email, putting in all sorts of content just to spam the keyword matchers.
# But interestingly, when doing this, it manages to also find the solution.
#
# I also wrote a blog post about this approach:
# https://mrmr.io/prolog-is-a-vibe
test -z "$1" && echo "usage: $0 <path-to-input>" && exit 1
head -1 "$1" > /dev/null || exit 1
echo "Hello Dear May," | tee /tmp/ac3.facts
read_log () { cp /tmp/ac3.facts /tmp/ac3.facts.old; cat /tmp/ac3.facts.old; }
nested_read_log () { cat /tmp/ac3.facts.old; }
log () { tee -a /tmp/ac3.facts; }
cat "$1" | awk '
{
split($0, cs, "")
for(i=1; i<=length($0); i++) {
c = cs[i]
if (c ~ /[0-9]/) {
n = n c
if (start==0) { start = i }
} else {
if (start) {
print "Number", n, "on row", NR, "and column", start
n = ""
start = 0
}
if (c != ".") {
print "Symbol", c, "on row", NR, "and column", i
}
}
}
}
' | log
read_log | awk '/Number/ { print $2, $5, $8 }' |
while read n r c
do
nsym=$(cat "$1" | awk -vn=$n -vr=$r -vc=$c '
NR == r-1 || NR == r || NR == r + 1 {
print substr($0,c-1,length(n)+2)
}
' | tr -d '0-9.\n' | wc -c | tr -d ' ')
echo "Number $n on row $r and column $c has $nsym symbols around it" \
| log
if test "$nsym" -ne 0
then
echo "Part $n on row $r and column $c" \
| log
fi
done
read_log | awk '/Symbol */ { print $5, $8 }' |
while read r c
do
echo '> Symbol *' "on row $r and column $c" | log
nested_read_log | grep -E "on row (`expr $r - 1`|$r|`expr $r + 1`) " |
awk -vr=$r -vc=$c '/Part */ {
n = $2; ns = $8; ne = ns + length(n) - 1;
print ">>", $0, "spans from", ns, "to", ne
print ">>", "To touch", c, "should be between", (ns - 1), "and", (ne + 1)
if (c >= (ns - 1) && c <= (ne + 1)) {
print "Gear on row", r, "and column", c, "touches part", n
}
}' | log
done
read_log | awk '/touches part/ { print $4, $7 }' | sort | uniq -c |
awk '
$1 == 2 { print "Gear on row", $2, "and column", $3, "touches two parts" }
' | log
read_log | awk '/touches two parts/ { print $4, $7 }' |
while read r c
do
nested_read_log | grep "Gear on row $r and column $c touches part" |
awk -vr=$r -vc=$c '
BEGIN { p = 1 }
{ p = p * $10 }
END { print "Gear on row", r, "and column", "has ratio", p }
' | log
done
read_log | awk '/Part/ { s += $2 }
END { print "The sum of all part numbers is", s }' | log
read_log | awk '/has ratio/ { s += $9 }
END { print "The sum of all gear ratios is", s }' | log
rm /tmp/ac3.facts.old # One must keep flowing, like water.
echo "Yours Truly," | log