From bf73a34e73a12c047e0cf129c34b7cd5040839b3 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Sat, 9 Dec 2023 01:02:52 +0000 Subject: [PATCH] Day 8 - bash --- README.md | 7 ++++- day8/eg1.txt | 9 ++++++ day8/solution.py | 33 --------------------- day8/solution.sh | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 34 deletions(-) create mode 100644 day8/eg1.txt delete mode 100644 day8/solution.py create mode 100755 day8/solution.sh diff --git a/README.md b/README.md index 95de138..0dc87ea 100644 --- a/README.md +++ b/README.md @@ -36,4 +36,9 @@ I'll probably try to vary languages a bit to get some variety. ### [Day 7](day7/) **Language**: JavaScript (Node) -**Note**: Yes, I used JSDoc for type-hints. But I think only insane people use JavaScript without JSDoc. \ No newline at end of file +**Note**: Yes, I used JSDoc for type-hints. But I think only insane people use JavaScript without JSDoc. + +### [Day 8](day8/) +**Language**: Bash + +**Note**: Here begin languages I'm not great at. \ No newline at end of file diff --git a/day8/eg1.txt b/day8/eg1.txt new file mode 100644 index 0000000..59e2d47 --- /dev/null +++ b/day8/eg1.txt @@ -0,0 +1,9 @@ +RL + +AAA = (BBB, CCC) +BBB = (DDD, EEE) +CCC = (ZZZ, GGG) +DDD = (DDD, DDD) +EEE = (EEE, EEE) +GGG = (GGG, GGG) +ZZZ = (ZZZ, ZZZ) \ No newline at end of file diff --git a/day8/solution.py b/day8/solution.py deleted file mode 100644 index b5ad11e..0000000 --- a/day8/solution.py +++ /dev/null @@ -1,33 +0,0 @@ -import math - -if __name__ == "__main__": - with open("input.txt") as f: - lines = f.read().split("\n") - instructions = lines[0] - d = {} - for l in lines[2:]: - pair = l.split(" = ") - d[pair[0]] = pair[1][1:-1].split(", ") - - cur = "AAA" - count = 0 - while cur != "ZZZ": - for i in instructions: - count += 1 - cur = d[cur][0 if i == "L" else 1] - if cur == "ZZZ": - break - print(count) - - cur = [k for k in d.keys() if k[-1] == "A"] - counts = [0 for _ in range(len(cur))] - while not all([s[-1] == "Z" for s in cur]): - for i in instructions: - for idx in range(len(cur)): - if cur[idx][-1] == "Z": - continue - counts[idx] += 1 - cur[idx] = d[cur[idx]][0 if i == "L" else 1] - if all([s[-1] == "Z" for s in cur]): - break - print(math.lcm(*counts)) \ No newline at end of file diff --git a/day8/solution.sh b/day8/solution.sh new file mode 100755 index 0000000..c061294 --- /dev/null +++ b/day8/solution.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +declare -A mappings + +gcd() { + local a=$1 + local b=$2 + while [ $b -ne 0 ] + do + local t=$b + b=$(($a % $b)) + a=$t + done + echo ${a#-} +} + +lcm() { + local d=$(gcd $1 $2) + echo $(($1 * $2 / $d)) +} + +findcount() { + cur=$1 + instructions=$2 + pattern=$3 + count=0 + while [[ $cur != $pattern ]] + do + for (( idx = 0; idx < ${#instructions}; idx++ )) + do + ((count += 1)) + instruction=${instructions:$idx:1} + part=0 + if [ $instruction = "R" ] + then + part=4 + fi + mapping=${mappings[$cur]} + cur=${mapping:$part:3} + if [[ $cur == $pattern ]] + then + break + fi + done + done + echo $count +} + +# Parse inputs +file="input.txt" +instructions=$(head -n1 $file) + +while read line || [ -n "$line" ] +do + key=${line%% *} + values=${line##*(} + values=${values%)} + mappings[$key]=${values/,/} +done <<< $(tail -n +3 $file) + +# Part 1 +echo "Part 1: $(findcount "AAA" $instructions "ZZZ")" + +# Part 2 +count=1 +for key in ${!mappings[@]} +do + if [[ $key != *A ]] + then + continue + fi + cur=$key + count=$(lcm $count $(findcount $cur $instructions "*Z")) +done +echo "Part 2: $count" \ No newline at end of file