-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunNewest
executable file
·153 lines (133 loc) · 3.12 KB
/
runNewest
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env bash
YEAR=2024
DAY=1
PART=1
DATA=debug
COPY=false
VIS=false
MEASURE=false
PROJECT_ROOT="$PWD"
while [ ! -f "$PROJECT_ROOT/README.md" ]; do
PROJECT_ROOT=$(dirname "$PROJECT_ROOT")
if [ "$PROJECT_ROOT" == "/" ]; then
echo "README.md not found. Are you sure you're in the correct directory?"
exit 1
fi
done
NEWEST_PATH=$(find $PROJECT_ROOT/20* -type f -name 'part*.ts' | sort -r | head -n 1 | sed "s|^$PROJECT_ROOT/||" | sed "s/day//; s/part//; s/\.ts//")
IFS='/' read -r YEAR DAY PART <<< "$NEWEST_PATH"
DAY="${DAY#0}"
usage() {
echo "Usage: $0 [-y YEAR] [-d DAY] [-p PART] [--solve] [--debug] [--submit]"
echo " -y, --year Year (default: 2024) (min: 2015)"
echo " -d, --day Day (default: 1) (range: 1-25)"
echo " -p, --part Part (default: 1) (options: 1 2)"
echo " -D --data Use real input data or just debug (default: debug) (options: debug solve)"
echo " -c --copy Copy the answer (default: false)"
echo " -v --vis Show cool little visualizations (default: false)"
echo " -m --measure Show cool little visualizations (default: false)"
exit 1
}
validate_year() {
if [ "$YEAR" -lt 2015 ]; then
echo "Error: YEAR must be 2015 or later."
exit 1
fi
}
validate_day() {
if [ "$DAY" -lt 1 ] || [ "$DAY" -gt 25 ]; then
echo "Error: DAY must be between 1 and 25."
exit 1
fi
}
validate_part() {
if [ "$PART" -ne 1 ] && [ "$PART" -ne 2 ]; then
echo "Error: PART must be 1 or 2."
exit 1
fi
}
validate_data() {
if [[ "$DATA" != "debug" && "$DATA" != "solve" ]]; then
echo "Error: DATA must be either 'debug' or 'solve'."
exit 1
fi
}
while [[ $# -gt 0 ]]; do
case $1 in
-y|--year)
YEAR="$2"
shift 2
;;
-d|--day)
DAY="$2"
shift 2
;;
-p|--part)
PART="$2"
shift 2
;;
-D|--data)
DATA="$2"
shift 2
;;
-c|--copy)
COPY=true
shift
;;
-v|--vis)
VIS=true
shift
;;
-m|--measure)
MEASURE=true
shift
;;
*)
usage
;;
esac
done
validate_year
validate_day
validate_part
validate_data
INPUT_PATH="$PROJECT_ROOT/input/$YEAR/day$(printf '%02d' $DAY)"
if [ "$DATA" == "solve" ]; then
INPUT_PATH="$INPUT_PATH.input"
if [ ! -f "$INPUT_PATH" ]; then
mkdir -p $(dirname "$INPUT_PATH" )
aoc download -d "$DAY" -y "$YEAR" -I -i "$INPUT_PATH"
fi
elif [ "$DATA" = "debug" ]; then
if [ -f "$INPUT_PATH.part$PART" ]; then
INPUT_PATH="$INPUT_PATH.part$PART"
elif [ -f "$INPUT_PATH.debug" ]; then
INPUT_PATH="$INPUT_PATH.debug"
else
echo "Please create one of those files:"
echo "$INPUT_PATH.part$PART"
echo "$INPUT_PATH.debug"
exit 1
fi
else
echo "This case should not happen"
exit 1
fi
FILE_PATH="$PROJECT_ROOT/$YEAR/day$(printf '%02d' $DAY)/part$PART.ts"
if [[ ! -f "$FILE_PATH" ]]; then
echo "File $FILE_PATH does not exist!"
exit 1
fi
export PART=$PART
export DATA=$DATA
export VIS=$VIS
start_time=$(date +%s%N)
SOLUTION=$(bun run "$FILE_PATH" "$INPUT_PATH" | tee /dev/tty)
if [ "$MEASURE" == true ]; then
end_time=$(date +%s%N)
elapsed_time=$(( (end_time - start_time) / 1000000 ))
echo "${elapsed_time}ms"
fi
if [ "$COPY" == true ]; then
echo "$SOLUTION" | sed -r 's/\x1B\[[0-9;]*m//g' | wl-copy
fi