-
Notifications
You must be signed in to change notification settings - Fork 0
/
24.sh
executable file
·42 lines (41 loc) · 936 Bytes
/
24.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
#! /usr/bin/env bash
A=($(sed -e "s/\([ew]\)\([ew]*\)/\1\2\2/g;s/^\([ew]\)/\1\1/" "${1:-24.txt}" ))
declare -A B
for i in "${A[@]}"; do
s=${i//[^s]}
n=${i//[^n]}
e=${i//[^e]}
w=${i//[^w]}
hash="$((${#e}-${#w})).$((${#n}-${#s}))"
if [[ -n "${B[${hash}]}" ]]; then
unset "B[${hash}]"
else
B[${hash}]=1
fi
done
echo "24A: ${#B[@]}"
solve24() {
local -A C=()
for i in "${!B[@]}"; do
x=${i/.*}
y=${i/*.}
C[$i]+=''
C[$((x-1)).$((y-1))]+=1
C[$((x+1)).$((y-1))]+=1
C[$((x-2)).$y]+=1
C[$((x+2)).$y]+=1
C[$((x-1)).$((y+1))]+=1
C[$((x+1)).$((y+1))]+=1
done
for i in "${!C[@]}"; do
if [[ -n "${B[$i]}" ]]; then
[[ ${#C[$i]} == [12] ]] || unset "B[$i]"
else
[[ ${#C[$i]} == 2 ]] && B[$i]=1
fi
done
}
for _ in {1..100}; do
solve24
done
echo "24B: ${#B[@]}"