From 1773627631cc873b6a09098dda1cac14c6ed03fa Mon Sep 17 00:00:00 2001 From: cos Date: Thu, 1 Dec 2022 16:14:20 +0000 Subject: Initial commit of init_year, poke support scripts --- bin/cookies_from_chromium | 32 +++++++++++++++++ bin/cookies_from_chromium.sh | 13 ------- bin/fetch_input | 79 +++++++++++++++++++++++++++++++++++++++++ bin/fetch_input.sh | 79 ----------------------------------------- bin/init_rust | 83 ++++++++++++++++++++++++++++++++++++++++++++ bin/init_rust.sh | 83 -------------------------------------------- bin/init_year | 76 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 270 insertions(+), 175 deletions(-) create mode 100755 bin/cookies_from_chromium delete mode 100755 bin/cookies_from_chromium.sh create mode 100755 bin/fetch_input delete mode 100755 bin/fetch_input.sh create mode 100755 bin/init_rust delete mode 100755 bin/init_rust.sh create mode 100755 bin/init_year diff --git a/bin/cookies_from_chromium b/bin/cookies_from_chromium new file mode 100755 index 0000000..b59e494 --- /dev/null +++ b/bin/cookies_from_chromium @@ -0,0 +1,32 @@ +#!/bin/sh -eu + +_aoc_config="${HOME}/.adventofcode.json" + +for _c in "${HOME}/.config/chrom"*/*'/Cookies' +do + echo "${_c}" + printf '%s\n%s%s\n' '.headers on' \ + 'SELECT name, value, hex(encrypted_value) FROM cookies ' \ + 'WHERE host_key=".adventofcode.com";' | sqlite3 "${_c}" +done + +echo 'This is a reminder of how cookies are encrypted.' \ + 'Manual export is required.' >&2 +echo 'Ctrl-Shift-i > Application > Cookies' >&2 + +read -p 'Session cookie: ' _session_cookie + +read -p "Create ${_aoc_config} (y/N)? " _input + +case "${_input}" in + 'y' | 'Y') + echo 'Ok. Creating the file.' + printf '{ "session-cookie": "%s" }' "${_session_cookie}" \ + > "${_aoc_config}" + ;; + *) + echo 'Ok. Skipping it.' + ;; +esac + +unset _c _input _session_cookie diff --git a/bin/cookies_from_chromium.sh b/bin/cookies_from_chromium.sh deleted file mode 100755 index 208cf14..0000000 --- a/bin/cookies_from_chromium.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh -e - -for C in "${HOME}/.config/chrom"*/*'/Cookies' -do - echo "${C}" - printf '%s\n%s%s\n' '.headers on' \ - 'SELECT name, value, hex(encrypted_value) FROM cookies ' \ - 'WHERE host_key=".adventofcode.com";' | sqlite3 "${C}" -done - -echo 'This is a reminder of how cookies are encrypted.' \ - 'Manual export is required.' >&2 -exit 1 diff --git a/bin/fetch_input b/bin/fetch_input new file mode 100755 index 0000000..ec09328 --- /dev/null +++ b/bin/fetch_input @@ -0,0 +1,79 @@ +#!/bin/sh -eu +# vim: sw=2 et + +COOKIES="${HOME}/.adventofcode.json" + +YEAR=${YEAR:-$(date +%Y)} +DAY=${DAY:-$(TZ=EST date '+%d')} +DEC=$(echo "${DAY}" | sed 's/^0//') + +MODE="wget" + +# https://stackoverflow.com/q/21919156/how-do-i-copy-cookies-from-chrome +SESSION=$(jq < "${COOKIES}" '."session-cookie"') + +check_dependency_availability() { + local bin + + for bin in 'elinks' \ + 'jq' \ + 'wget' + do + if ! which "${bin}" >/dev/null; then + echo "Missing dependency: ${bin}" >&2 + exit 1 + fi + done +} + +concat() { + local part + + for part in "$@"; do + printf '%s' "${part}" + done +} + +authenticated_wget() { + wget \ + --server-response \ + --header "Cookie: session=${SESSION}" \ + "${@}" +} + +check_dependency_availability + +if [ "${MODE}" = 'wget' ]; then + cd "${YEAR}" + cd 'rust' + mkdir "day${DAY}" || : + cd "day${DAY}" + + # https://stackoverflow.com/q/21919156/how-do-i-copy-cookies-from-chrome + SESSION=$(jq < "${COOKIES}" '."session-cookie"' | tr -d '"') + + if [ -e "input" ]; then + echo 'Directory input already exists. Skipping.' + else + mkdir 'input' + authenticated_wget --output-document "input/page_${DEC}.html" \ + "https://adventofcode.com/${YEAR}/day/${DEC}" + SETTINGS=$(authenticated_wget --output-document='-' \ + "https://adventofcode.com/${YEAR}/settings") + USERNAME=$(echo "${SETTINGS}" | + sed -n 's#.*display_name.*checked.*\(.*\).*#\1#p') + echo "Found username: ${USERNAME}" + authenticated_wget --output-document "input/${USERNAME}" \ + "https://adventofcode.com/${YEAR}/day/${DEC}/input" + elinks -dump "./input/page_${DEC}.html" >"input/page_${DEC}.txt" + sed -n 's/^ \([^ ]\)/\1/p' <"input/page_${DEC}.txt" >"input/example.txt" + fi +elif [ "${MODE}" = 'aocdl' ]; then + # One could possibly use the golang implemenation instead of wget: + # https://github.com/GreenLightning/advent-of-code-downloader + # + # It has more features, but it is always a hassle to install extra things. + # + # go get github.com/GreenLightning/advent-of-code-downloader/aocdl + ~/.go/bin/aocdl -output "{{.Year}}/rust/{{.Day}}" +fi diff --git a/bin/fetch_input.sh b/bin/fetch_input.sh deleted file mode 100755 index ec09328..0000000 --- a/bin/fetch_input.sh +++ /dev/null @@ -1,79 +0,0 @@ -#!/bin/sh -eu -# vim: sw=2 et - -COOKIES="${HOME}/.adventofcode.json" - -YEAR=${YEAR:-$(date +%Y)} -DAY=${DAY:-$(TZ=EST date '+%d')} -DEC=$(echo "${DAY}" | sed 's/^0//') - -MODE="wget" - -# https://stackoverflow.com/q/21919156/how-do-i-copy-cookies-from-chrome -SESSION=$(jq < "${COOKIES}" '."session-cookie"') - -check_dependency_availability() { - local bin - - for bin in 'elinks' \ - 'jq' \ - 'wget' - do - if ! which "${bin}" >/dev/null; then - echo "Missing dependency: ${bin}" >&2 - exit 1 - fi - done -} - -concat() { - local part - - for part in "$@"; do - printf '%s' "${part}" - done -} - -authenticated_wget() { - wget \ - --server-response \ - --header "Cookie: session=${SESSION}" \ - "${@}" -} - -check_dependency_availability - -if [ "${MODE}" = 'wget' ]; then - cd "${YEAR}" - cd 'rust' - mkdir "day${DAY}" || : - cd "day${DAY}" - - # https://stackoverflow.com/q/21919156/how-do-i-copy-cookies-from-chrome - SESSION=$(jq < "${COOKIES}" '."session-cookie"' | tr -d '"') - - if [ -e "input" ]; then - echo 'Directory input already exists. Skipping.' - else - mkdir 'input' - authenticated_wget --output-document "input/page_${DEC}.html" \ - "https://adventofcode.com/${YEAR}/day/${DEC}" - SETTINGS=$(authenticated_wget --output-document='-' \ - "https://adventofcode.com/${YEAR}/settings") - USERNAME=$(echo "${SETTINGS}" | - sed -n 's#.*display_name.*checked.*\(.*\).*#\1#p') - echo "Found username: ${USERNAME}" - authenticated_wget --output-document "input/${USERNAME}" \ - "https://adventofcode.com/${YEAR}/day/${DEC}/input" - elinks -dump "./input/page_${DEC}.html" >"input/page_${DEC}.txt" - sed -n 's/^ \([^ ]\)/\1/p' <"input/page_${DEC}.txt" >"input/example.txt" - fi -elif [ "${MODE}" = 'aocdl' ]; then - # One could possibly use the golang implemenation instead of wget: - # https://github.com/GreenLightning/advent-of-code-downloader - # - # It has more features, but it is always a hassle to install extra things. - # - # go get github.com/GreenLightning/advent-of-code-downloader/aocdl - ~/.go/bin/aocdl -output "{{.Year}}/rust/{{.Day}}" -fi diff --git a/bin/init_rust b/bin/init_rust new file mode 100755 index 0000000..1431b87 --- /dev/null +++ b/bin/init_rust @@ -0,0 +1,83 @@ +#!/bin/sh -eu + +YEAR=${YEAR:-$(date +%Y)} +DAY=${DAY:-$(TZ=EST date '+%d')} + +GIT_USER=$(git config --get 'user.name') +GIT_EMAIL=$(git config --get 'user.email') + +CARGO=$( sed "s/^#\(.*${DAY}\)/\1/" <"${YEAR}/rust/Cargo.toml") +echo "${CARGO}" >"${YEAR}/rust/Cargo.toml" + +cd "${YEAR}/rust" +mkdir "day${DAY}" || : +cd "day${DAY}" + +cargo init --bin + +cat > 'Cargo.toml' << END_OF_TOML +[package] +name = "day${DAY}" +version = "0.1.0" +authors = ["${GIT_USER} <${GIT_EMAIL}>"] +edition = "2021" + +[dependencies] +aoc = { path = "../../../common/rust/aoc" } +anyhow = "1.0" +END_OF_TOML + +cat > 'src/main.rs' << END_OF_SRC +use { + anyhow::{ + anyhow, + Context, + Result, + }, + std::{ + env::args, + fs::File, + io::{ + BufRead, + BufReader, + }, + path::Path, + }, +}; + +fn read_input>(filename: T) -> Result> { + let reader = BufReader::new(File::open(filename)?); + + reader.lines().map( + |v| { + let s = v?; + let n = s.parse()?; + Ok(n) + } + ).collect() +} + +fn part1<'a, I: IntoIterator>(_input: I) -> Result { + Ok(0) +} + +fn part2<'a, I: IntoIterator>(_input: I) -> Result { + Ok(0) +} + +fn main() -> Result<()> { + let ( do_part_1, do_part_2 ) = aoc::do_parts(); + + let filename = args().nth(1).ok_or(anyhow!("Missing input filename"))?; + let input = read_input(filename).context("Could not read input")?; + if do_part_1 { + let solution = part1(&input).context("No solution for part 1")?; + println!("Part1, solution found to be: {}", solution); + } + if do_part_2 { + let solution = part2(&input).context("No solution for part 2")?; + println!("Part2, solution found to be: {}", solution); + } + Ok(()) +} +END_OF_SRC diff --git a/bin/init_rust.sh b/bin/init_rust.sh deleted file mode 100755 index fc4e25d..0000000 --- a/bin/init_rust.sh +++ /dev/null @@ -1,83 +0,0 @@ -#!/bin/sh -e - -YEAR=${YEAR:-$(date +%Y)} -DAY=${DAY:-$(TZ=EST date '+%d')} - -GIT_USER=$(git config --get 'user.name') -GIT_EMAIL=$(git config --get 'user.email') - -CARGO=$( sed "s/^#\(.*${DAY}\)/\1/" <"${YEAR}/rust/Cargo.toml") -echo "${CARGO}" >"${YEAR}/rust/Cargo.toml" - -cd "${YEAR}/rust" -mkdir "day${DAY}" || : -cd "day${DAY}" - -cargo init --bin - -cat > 'Cargo.toml' << END_OF_TOML -[package] -name = "day${DAY}" -version = "0.1.0" -authors = ["${GIT_USER} <${GIT_EMAIL}>"] -edition = "2021" - -[dependencies] -aoc = { path = "../../../common/rust/aoc" } -anyhow = "1.0" -END_OF_TOML - -cat > 'src/main.rs' << END_OF_SRC -use { - anyhow::{ - anyhow, - Context, - Result, - }, - std::{ - env::args, - fs::File, - io::{ - BufRead, - BufReader, - }, - path::Path, - }, -}; - -fn read_input>(filename: T) -> Result> { - let reader = BufReader::new(File::open(filename)?); - - reader.lines().map( - |v| { - let s = v?; - let n = s.parse()?; - Ok(n) - } - ).collect() -} - -fn part1<'a, I: IntoIterator>(_input: I) -> Result { - Ok(0) -} - -fn part2<'a, I: IntoIterator>(_input: I) -> Result { - Ok(0) -} - -fn main() -> Result<()> { - let ( do_part_1, do_part_2 ) = aoc::do_parts(); - - let filename = args().nth(1).ok_or(anyhow!("Missing input filename"))?; - let input = read_input(filename).context("Could not read input")?; - if do_part_1 { - let solution = part1(&input).context("No solution for part 1")?; - println!("Part1, solution found to be: {}", solution); - } - if do_part_2 { - let solution = part2(&input).context("No solution for part 2")?; - println!("Part2, solution found to be: {}", solution); - } - Ok(()) -} -END_OF_SRC diff --git a/bin/init_year b/bin/init_year new file mode 100755 index 0000000..494c86f --- /dev/null +++ b/bin/init_year @@ -0,0 +1,76 @@ +#!/bin/sh -eu + +if [ "${1:-}" ]; then + _lang="$1" +fi + +if [ "${2:-}" ]; then + _year="$2" +else + _year=$( date +%Y ) +fi + +### FUNCTION ################################################################## + +rust() { + rust_dir="$1" + + ( + printf '%s\n%s\n' '[workspace]' 'members = [' + for day in $( seq 1 25 ); do + printf '# "day%02d",\n' "${day}" + done + echo ']' + ) > "${rust_dir}/Cargo.toml" + + unset rust_dir +} + +### MAIN ###################################################################### + +_dir="$( pwd )/${_year}" + +if ! [ -d "${_dir}" ]; then + read -p "Create directory '${_dir}'?: " _input + + case "${_input}" in + 'y' | 'Y') + mkdir "${_dir}" + ;; + *) + echo 'Okay, not doing anything then.' + exit 0 + ;; + esac + unset _input +fi + +[ "${_lang:-}" ] || read -p "Implementation language for '${_year}'?: " _lang + +if ! [ -d "${_dir}/${_lang}" ]; then + read -p "Create directory '${_dir}/${_lang}'?: " _input + case "${_input}" in + 'y' | 'Y') + mkdir "${_dir}/${_lang}" + ;; + *) + echo 'Okay, not doing that then.' + exit 0 + ;; + esac + unset _input +fi + +read -p "Run '${_lang}(\"${_dir}/${_lang}\")'?: " _input +case "${_input}" in + 'y' | 'Y') + ${_lang} "${_dir}/${_lang}" + ;; + *) + echo 'Okay, not doing that then.' + exit 0 + ;; +esac +unset _input + +unset _dir _lang _year -- cgit v1.2.3