Skip to content

Commit 4d14984

Browse files
committed
2021: Days 13 and 14
1 parent ec029a8 commit 4d14984

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

2021/13.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
# Oneliner for part 1. First fold hardcoded.
3+
#echo "13A: $(while read x y; do ((x >655)) && x=$((2*655-x)); echo $x,$y; done < <(grep , 13.txt| tr , ' ') | sort -u | wc -l)"
4+
5+
fold_x() {
6+
local n=$1 C=(${DOTS[@]}) i
7+
for i in "${!C[@]}"; do
8+
x=${C[i]//,*}
9+
((x>n)) && C[i]=$(((2*n-x))),${C[i]/*,}
10+
done
11+
DOTS=($(printf "%s\n" "${C[@]}"| sort -n | uniq))
12+
}
13+
fold_y() {
14+
local n=$1 C=(${DOTS[@]}) i
15+
for i in "${!C[@]}"; do
16+
y=${C[i]//*,}
17+
((y>n)) && C[i]=${C[i]/,*},$(((2*n-y)))
18+
done
19+
DOTS=($(printf "%s\n" "${C[@]}"| sort -n | uniq))
20+
}
21+
22+
DOTS=($(grep , "${1:-13.txt}"))
23+
FOLDS=($(grep -o '.=.*' "${1:-13.txt}"))
24+
fold_"${FOLDS[0]/=*}" "${FOLDS[0]:2}"
25+
echo "13A: ${#DOTS[@]}"
26+
for line in "${FOLDS[@]:1}"; do
27+
xy=${line/=*}
28+
"fold_$xy" "${line:2}"
29+
done
30+
TEXT=() spaces=" "
31+
for i in "${!DOTS[@]}"; do
32+
x=${DOTS[i]//,*} y=${DOTS[i]//*,} len=${#TEXT[y]}
33+
TEXT[y]+="${spaces:0:x-len}#"
34+
done
35+
echo "13B:"
36+
printf "%s\n" "${TEXT[@]}"

2021/14.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
input=${1:-14.txt}
3+
declare -A SET=()
4+
declare -iA COUNT=()
5+
POL=$(head -1 "${input}")
6+
while read -r a _ b;do
7+
SET[$a]="${a:0:1}$b $b${a:1}";
8+
done < <( grep ' -> ' "${input}")
9+
for ((i=0; i<${#POL}-1;i++)); do
10+
j=${POL:i:2}
11+
COUNT[$j]+=1;
12+
done
13+
14+
solve() {
15+
for ((; round < $1; round++)); do
16+
local -iA NEW_COUNT=()
17+
for j in "${!COUNT[@]}"; do
18+
for k in ${SET[$j]}; do
19+
NEW_COUNT[$k]+=${COUNT[$j]}
20+
done
21+
done
22+
COUNT=()
23+
for j in "${!NEW_COUNT[@]}"; do COUNT[$j]=${NEW_COUNT[$j]}; done
24+
done
25+
}
26+
diff() {
27+
local -n ans=$1
28+
local -Ai ALPHA=([${POL:0:1}]=1 [${POL: -1}]=1) # ends are only counted once
29+
for k in "${!COUNT[@]}"; do
30+
ALPHA[${k:0:1}]+=$((COUNT["$k"]))
31+
ALPHA[${k:1}]+=$((COUNT["$k"]))
32+
done
33+
FREQ=($(printf "%s\n" "${ALPHA[@]}" | sort -n))
34+
#shellcheck disable=SC2034
35+
ans=$((FREQ[-1]/2-FREQ[0]/2))
36+
}
37+
38+
round=0
39+
for i in 10 40;do
40+
solve $i
41+
diff "ANS[n++]"
42+
done
43+
echo "14A: ${ANS[0]}"
44+
echo "14B: ${ANS[1]}"

2021/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,11 @@ Description of what I'm doing. Contains spoilers....
5757
### 12.sh
5858
1. Brute force. Simplified version of the recursive functions of 2015 (9 and 13). Collect a hashmap with the destinations for each cave. Remove "start" from the destinations to simplify coding.
5959
2. Add a dumb check to allow the first lowercase cave twice. Takes way too long to run.
60+
61+
### 13.sh
62+
1. Functions fold_x/fold_y for easier handling. Fold and remove duplicates for first line.
63+
2. Repeat for rest of the lines. Collect space-delimited dots in an array of strings. Print the array.
64+
65+
### 14.sh
66+
Collect the conversions in an array. Count the number of pairs in the initial polymer. For each polymer, convert the input to 2 outputs.
67+
For the final count, every char is part of 2 pairs except the ends, so add 1 to the ends and then divide by 2 for the final number.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy