generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitDay.sh
executable file
·67 lines (55 loc) · 1.58 KB
/
initDay.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
# Check if number of arguments is exactly 1
if [ $# -ne 1 ]; then
echo "pass only day number as argument"
exit 1
fi
# Load AOC_COOKIE
source .env
# Check if AOC_COOKIE is set
if [ -z "$AOC_COOKIE" ]; then
echo "AOC_COOKIE is not set"
exit 1
fi
# Define folder and filenames
day=$((10#$1)) # number of day without leading zeroes
prevDay=$((day - 1))
oldName=$(printf "%02d" "${prevDay}")
newName=$(printf "%02d" "${day}")
ktFolder="src/solutions23/"
inputFolder="src/solutions23/input"
# Make new file if it doesn't exist.
# param $1 - content
# param $2 - filename
function mkFile() {
if [ -f "${2}" ]; then
echo "File already exists: ${2}"
else
echo "${1}" > "${2}"
git add "${2}"
echo "Generated: ${2}"
fi
}
# Copy kotlin file from previous day
# use mkFile with first argument the content of the old file and
# the second argument the new file name.
mkFile "$(cat "$ktFolder/Day$oldName.kt")" "$ktFolder/Day$newName.kt"
# Substitute occurences of oldName with newName, to update references to old input files
sed -i "s/$oldName/$newName/g" "$ktFolder/Day$newName.kt"
# Create new empty file for test input
mkFile "" "$inputFolder/Day${newName}_test.txt"
# Download puzzle input file
file="$inputFolder/Day$newName.txt"
url="https://adventofcode.com/2023/day/$day/input"
if [ -f "${file}" ]; then
echo "File already exists: ${file}"
else
if wget --header="Cookie: session=${AOC_COOKIE}" -O "${file}" "${url}"; then
git add "${file}"
echo "Downloaded: ${file}"
exit 0
else
echo "Download failed"
exit 1
fi
fi