🏡 index : ~doyle/aoc.git

author Jordan Doyle <jordan@doyle.la> 2023-12-30 18:06:27.0 +00:00:00
committer Jordan Doyle <jordan@doyle.la> 2023-12-30 18:17:42.0 +00:00:00
commit
ca0effa3af21ddb37e730b42aa9aac406134e3f5 [patch]
tree
f075b7a33fa83616e6bcb0760dc8668fcd08d45b
parent
af904e578a1567d3ed197edced894706d7e64a0d
download
ca0effa3af21ddb37e730b42aa9aac406134e3f5.tar.gz

Add day 25



Diff

 2023/24.hs  |  2 +-
 2023/25.sh  | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 README      |  3 +++
 treefmt.nix |  3 +++
 4 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/2023/24.hs b/2023/24.hs
index e369c42..3bae82b 100755
--- a/2023/24.hs
+++ b/2023/24.hs
@@ -4,7 +4,7 @@ import Aoc (readAndParseStdin)
import qualified Data.Map as Map
import Data.Maybe (fromJust, fromMaybe)
import Numeric.LinearAlgebra
import Text.Parsec (char, digit, many1, spaces, (<|>), sepBy, string)
import Text.Parsec (char, digit, many1, sepBy, spaces, string, (<|>))
import Text.Parsec.String (Parser)

main = do
diff --git a/2023/25.sh b/2023/25.sh
new file mode 100755
index 0000000..2c48f40
--- /dev/null
+++ b/2023/25.sh
@@ -0,0 +1,52 @@
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p bash -p graphviz -p imgcat -p gnused -p gawk -p coreutils -p bc

set -euo pipefail

function log() {
  echo -e "\033[34m$1\033[0m" >&2
}

function logc() {
  echo -e "+ \033[35m$*\033[0m" >&2
  "$@"
}

log "Generating graph visualisation using graphviz"
# shellcheck disable=SC2016
GRAPH=$(logc awk -F': ' '{split($2, nodes, " "); for (i in nodes) print $1" -- "nodes[i]}' input)
echo "graph a { $GRAPH }" | logc neato -Tpng | logc imgcat

while true; do
  read -r -p "Which edge would you like to cut? (eg. AA -- BB) " ans

  if [ "$ans" = "" ]; then
    break
  elif echo "$GRAPH" | grep -q "$ans"; then
    GRAPH="${GRAPH//$ans/}"
    echo "graph a { $GRAPH }" | logc neato -Tpng | logc imgcat
  else
    echo "No match for $ans"
  fi
done

log "Calculating graph structure"
SPLIT=$(echo "graph a { $GRAPH }" | ccomps -x || true)

if [ "$(echo "$SPLIT" | grep -o "graph" | wc -l)" -ne 2 ]; then
  echo "There's not two distinctive subgraphs, try again"
  exit 1
fi

# shellcheck disable=SC2016
PART1=$(echo "$SPLIT" \
  | logc awk '/^graph/ {subgraph=$2} /^[[:space:]]+/ {gsub(/;/, "", $1); gsub(/;/, "", $3); print subgraph, $1; print subgraph, $3}' \
  | logc sort \
  | logc uniq \
  | logc cut -d ' ' -f 1 \
  | logc uniq -c \
  | tr '\n' ' ' \
  | logc sed -E 's/^[[:space:]]+([0-9]+)[[:space:]]+[^ ]+[[:space:]]+([0-9]+).*/\1*\2\n/g' \
  | bc)

echo "$PART1"
diff --git a/README b/README
index 30b31ac..cf8d352 100644
--- a/README
+++ b/README
@@ -36,4 +36,7 @@
|       20 | Haskell  |
|       21 | Rust     |
|       22 | Haskell  |
|       23 | Haskell  |
|       24 | Haskell  |
|       25 | Bash     |
+---------------------+
diff --git a/treefmt.nix b/treefmt.nix
index 288585d..a6bf182 100644
--- a/treefmt.nix
+++ b/treefmt.nix
@@ -10,5 +10,8 @@
    rustfmt.enable = true;
    fprettify.enable = true;
    jsonnetfmt.enable = true;
    shellcheck.enable = true;
  };

  settings.formatter.shellcheck.options = [ "-s" "bash" ];
}