Day 19
This commit is contained in:
116
19/01.sh
Executable file
116
19/01.sh
Executable file
@@ -0,0 +1,116 @@
|
||||
#!/bin/bash
|
||||
|
||||
declare -A scanners
|
||||
declare -A beacons
|
||||
scanner_i=-1
|
||||
n_scanners=0
|
||||
scanners_beacons_n=()
|
||||
declare -A placed_scanners
|
||||
while read line; do
|
||||
if [[ $line == "---"*"---" ]]; then
|
||||
scanner_i=$((scanner_i+1))
|
||||
n_scanners=$((n_scanners+1))
|
||||
scanners_beacons_n+=(0)
|
||||
placed_scanners[$scanner_i]=0
|
||||
elif ! [ -z "$line" ]; then
|
||||
scanners[$scanner_i,${scanners_beacons_n[$scanner_i]}]=$line
|
||||
scanners_beacons_n[$scanner_i]=$((scanners_beacons_n[scanner_i]+1))
|
||||
fi
|
||||
done
|
||||
|
||||
for ((i=0;i<scanners_beacons_n[0];i++)); do
|
||||
beacons[${scanners[0,$i]}]=1
|
||||
done
|
||||
placed_scanners[0]=1
|
||||
n_placed_scanners=1
|
||||
|
||||
declare -A coordinates
|
||||
coordinates=( \
|
||||
[0,x]=x [0,y]=y [0,z]=z \
|
||||
[1,x]=x [1,y]=z [1,z]=-y \
|
||||
[2,x]=x [2,y]=-y [2,z]=-z \
|
||||
[3,x]=x [3,y]=-z [3,z]=y \
|
||||
[4,x]=-x [4,y]=y [4,z]=-z \
|
||||
[5,x]=-x [5,y]=-z [5,z]=-y \
|
||||
[6,x]=-x [6,y]=-y [6,z]=z \
|
||||
[7,x]=-x [7,y]=z [7,z]=y \
|
||||
[8,x]=y [8,y]=-x [8,z]=z \
|
||||
[9,x]=y [9,y]=z [9,z]=x \
|
||||
[10,x]=y [10,y]=x [10,z]=-z \
|
||||
[11,x]=y [11,y]=-z [11,z]=-x \
|
||||
[12,x]=-y [12,y]=-x [12,z]=-z \
|
||||
[13,x]=-y [13,y]=-z [13,z]=x \
|
||||
[14,x]=-y [14,y]=x [14,z]=z \
|
||||
[15,x]=-y [15,y]=z [15,z]=-x \
|
||||
[16,x]=z [16,y]=y [16,z]=-x \
|
||||
[17,x]=z [17,y]=-x [17,z]=-y \
|
||||
[18,x]=z [18,y]=-y [18,z]=x \
|
||||
[19,x]=z [19,y]=x [19,z]=y \
|
||||
[20,x]=-z [20,y]=y [20,z]=x \
|
||||
[21,x]=-z [21,y]=x [21,z]=-y \
|
||||
[22,x]=-z [22,y]=-y [22,z]=-x \
|
||||
[23,x]=-z [23,y]=-x [23,z]=y \
|
||||
)
|
||||
|
||||
while [ $n_placed_scanners -lt $n_scanners ]; do
|
||||
n_overlapping_beacons=0
|
||||
for placed_beacon in ${!beacons[@]}; do
|
||||
x_placed_beacon=${placed_beacon%%,*}
|
||||
y_placed_beacon=${placed_beacon%,*}
|
||||
y_placed_beacon=${y_placed_beacon#*,}
|
||||
z_placed_beacon=${placed_beacon##*,}
|
||||
for ((i_scanners=1;i_scanners<n_scanners;i_scanners++)); do
|
||||
if [ ${placed_scanners[$i_scanners]} -eq 0 ]; then
|
||||
for ((i_orientations=0;i_orientations<24;i_orientations++)); do
|
||||
for ((i_matching_beacons=0;i_matching_beacons<scanners_beacons_n[$i_scanners];i_matching_beacons++)); do
|
||||
matching_beacon_coordinate=${scanners[$i_scanners,$i_matching_beacons]}
|
||||
x=${matching_beacon_coordinate%%,*}
|
||||
y=${matching_beacon_coordinate%,*}
|
||||
y=${y#*,}
|
||||
z=${matching_beacon_coordinate##*,}
|
||||
x_offset=$((x_placed_beacon-coordinates[$i_orientations,x]))
|
||||
y_offset=$((y_placed_beacon-coordinates[$i_orientations,y]))
|
||||
z_offset=$((z_placed_beacon-coordinates[$i_orientations,z]))
|
||||
n_overlapping_beacons=0
|
||||
for ((i_beacons=0;i_beacons<scanners_beacons_n[$i_scanners];i_beacons++)); do
|
||||
beacon_coordinate=${scanners[$i_scanners,$i_beacons]}
|
||||
x=${beacon_coordinate%%,*}
|
||||
y=${beacon_coordinate%,*}
|
||||
y=${y#*,}
|
||||
z=${beacon_coordinate##*,}
|
||||
curr_coordinates=$((coordinates[$i_orientations,x]+x_offset)),$((coordinates[$i_orientations,y]+y_offset)),$((coordinates[$i_orientations,z]+z_offset))
|
||||
if ! [ -z ${beacons[$curr_coordinates]} ]; then
|
||||
n_overlapping_beacons=$((n_overlapping_beacons+1))
|
||||
fi
|
||||
done
|
||||
if [ $n_overlapping_beacons -ge 12 ]; then
|
||||
placed_scanners[$i_scanners]=1
|
||||
n_placed_scanners=$((n_placed_scanners+1))
|
||||
for ((i_beacons=0;i_beacons<scanners_beacons_n[$i_scanners];i_beacons++)); do
|
||||
beacon_coordinate=${scanners[$i_scanners,$i_beacons]}
|
||||
x=${beacon_coordinate%%,*}
|
||||
y=${beacon_coordinate%,*}
|
||||
y=${y#*,}
|
||||
z=${beacon_coordinate##*,}
|
||||
curr_coordinates=$((coordinates[$i_orientations,x]+x_offset)),$((coordinates[$i_orientations,y]+y_offset)),$((coordinates[$i_orientations,z]+z_offset))
|
||||
beacons[$curr_coordinates]=1
|
||||
done
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ $n_overlapping_beacons -ge 12 ]; then
|
||||
break
|
||||
fi
|
||||
done
|
||||
fi
|
||||
if [ $n_overlapping_beacons -ge 12 ]; then
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ $n_overlapping_beacons -ge 12 ]; then
|
||||
break
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
echo ${#beacons[@]}
|
||||
Reference in New Issue
Block a user