-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.sh
206 lines (187 loc) · 4.49 KB
/
14.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!bin/bash
lines=()
while read -r line; do
lines+=("$line")
done
#R=7
#C=11
R=103
C=101
T=100 #5
# part 2
p2=-1
#: '
robs=()
for line in "${lines[@]}"; do
read c r dc dr < <(echo "$line" | awk -F'[=, ]' '{print $2,$3,$5,$6}')
robs+=("$r,$c,$dr,$dc")
done
#'
END=8000
w=0
x=0
y=0
z=0
it=1
maxline=0
for (( i = 0; i < END; i++ )); do
if (( it % 1000 == 0 )); then
echo "@ $it" >&2
fi
for (( j = 0; j < ${#robs[@]}; j++ )); do
IFS=',' read -r r c dr dc <<< "${robs[j]}"
(( r = (r + dr + R) % R ))
(( c = (c + dc + C) % C ))
robs[j]="$r,$c,$dr,$dc"
done
# inspect w/ maxline
changed=0
unset lens
declare -A lens
for (( j = 0; j < ${#robs[@]}; j++ )); do
IFS=',' read -r r _ _ _ <<< "${robs[j]}"
(( lens[$r]++ ))
done
for key in "${!lens[@]}"; do
val="${lens[$key]}"
if (( val > maxline )); then
(( maxline = val ))
(( changed = 1 ))
fi
done
if (( changed == 1 )); then
if (( it == 7858 )); then
# print the sapin: grid
G=()
for (( r = 0; r < R; r++ )); do
row=""
for (( c = 0; c < C; c++ )); do
row+="."
done
G+=("$row")
done
# shape of sapin
for rob in "${robs[@]}"; do
IFS="," read -r r c _ _ <<< "$rob"
G[r]="${G[r]:0:c}/"${G[r]:(( c + 1 ))}
done
# print a reduced section
sr=$(( R / 7 * 3 ))
er=$(( R / 5 * 4 ))
sc=$(( C / 4 ))
ec=$(( C / 4 * 3 ))
for (( r = sr; r < er; r++ )); do
echo "${G[r]:sc:ec-sc}"
done
fi
(( p2 = it ))
echo "----/ $it" # maxline-led change
fi
# inspect w/ counts inside each quadrant
changed=0
unset D
declare -A D
for rob in "${robs[@]}"; do
IFS="," read -r r c _ _ <<< "$rob"
(( D["$r,$c"]++ ))
done
unset count
declare -A count
for rc in "${!D[@]}"; do
r=${rc%,*}
c=${rc#*,}
if (( r < R / 2 && c < C / 2 - 1 )); then
(( count[0]++ ))
fi
if (( r < R / 2 && c > C / 2 - 1 )); then
(( count[1]++ ))
fi
if (( r > R / 2 && c < C / 2 - 1 )); then
(( count[2]++ ))
fi
if (( r > R / 2 && c > C / 2 - 1 )); then
(( count[3]++ ))
fi
done
if (( w < count[0] )); then
(( w = count[0] ))
changed=1
fi
if (( x < count[1] )); then
(( x = count[1] ))
changed=1
fi
if (( y < count[2] )); then
(( y = count[2] ))
changed=1
fi
if (( z < count[3] )); then
(( z = count[3] ))
changed=1
fi
if (( changed == 1 )); then
if (( it == 7858 )); then
# print the sapin: grid
G=()
for (( r = 0; r < R; r++ )); do
row=""
for (( c = 0; c < C; c++ )); do
row+="."
done
G+=("$row")
done
# shape of sapin
for rob in "${robs[@]}"; do
IFS="," read -r r c _ _ <<< "$rob"
G[r]="${G[r]:0:c}/"${G[r]:(( c + 1 ))}
done
# print a reduced section
sr=$(( R / 7 * 3 ))
er=$(( R / 5 * 4 ))
sc=$(( C / 4 ))
ec=$(( C / 4 * 3 ))
for (( r = sr; r < er; r++ )); do
echo "${G[r]:sc:ec-sc}"
done
fi
(( p2 = it ))
echo "quad/ $it" # maxline-led change
fi
(( it++ ))
done
# part 1
unset D
declare -A D
unset count
declare -A count
for line in "${lines[@]}"; do
read c r dc dr < <(echo "$line" | awk -F'[=, ]' '{print $2,$3,$5,$6}')
for (( i = 0; i < T; i++ )); do
(( r = (r + dr + R) % R ))
(( c = (c + dc + C) % C ))
done
(( D["$r,$c"]++ ))
done
for rc in "${!D[@]}"; do
r=${rc%,*}
c=${rc#*,}
val=${D["$rc"]}
if (( r < R / 2 && c < C / 2 )); then
(( count[0] += val ))
fi
if (( r < R / 2 && c > C / 2 )); then
(( count[1] += val ))
fi
if (( r > R / 2 && c < C / 2 )); then
(( count[2] += val ))
fi
if (( r > R / 2 && c > C / 2 )); then
(( count[3] += val ))
fi
done
p1=1
for (( i = 0; i < 4 ; i++ )); do
(( p1 *= count[$i] ))
done
echo "part 1: $p1"
echo "part 1: $p1"