Advent of Code Solutions
advent-of-code
aoc
1#!/bin/sh
2set -e
3
4usage() {
5 printf '%b\n' "usage: ${0##*/} <year> <day> [options]" \
6 "options:" \
7 "\t<year> - which year to execute" \
8 "\t<day> - which day to execute" \
9 "\t-p - which part to execute (dont specify anything for both)" \
10 "\t-r - use the real input instead of the test input"
11}
12
13[ $# -lt 2 ] && {
14 usage
15 exit 1
16}
17
18year=$1
19day=$2
20shift 2
21
22part=""
23real=false
24for flag in "$@"
25do
26 # Make sure arguments start with '-' and are atleast 2 characters long
27 case $flag in
28 - ) continue ;;
29 -- ) break ;;
30 -* ) ;;
31 * ) continue;;
32 esac
33
34 # Split arguments to be 1 character long and determine options to use
35 args=${flag#-}
36
37 while [ "$args" ]
38 do
39 a=${args%"${args#?}"}
40
41 case $a in
42 p ) aargs=$*
43 part=${aargs##*"${flag}"}; part=${part#\ }; part=${part%%\ *}
44 [ "${part}" ] || {
45 printf '%s\n' "${0##*/}: -p: missing part" 1>&2
46 exit 1
47 }
48
49 [ "${part}" = "1" ] || [ "${part}" = "2" ] || {
50 printf '%s\n' "${0##*/}: -p: should be either 1 or 2" 1>&2
51 exit 1
52 } ;;
53 r ) real=true ;;
54 * ) printf '%s\n' "${0##*/}: -$a: invalid argument" 1>&2
55 usage 1>&2; exit 1 ;;
56 esac
57
58 args=${args#?}
59 done
60done
61unset args arg aargs
62
63cd "${0%/*}"
64
65cd "$year" || exit 1
66ext="$(cat ./ext)"
67
68if [ $real = true ]
69then "./$day/solution${ext}" "$part" < "$day/input-real"
70else
71 if [ -d "$day/part1-tests" ] && { [ "$part" = 1 ] || [ ! "$part" ]; }
72 then
73 (
74 cd "$day/part1-tests" || exit 1
75 printf '%s' "Part 1 Tests: "
76
77 for test_case in input*
78 do
79 num=${test_case#input}
80 expected=$(sed -n "${num}{p;q}" < ./expected)
81 result=$("../solution$ext" 1 < "$test_case")
82 result=${result#Part\ 1:\ }
83
84 # shellcheck disable=SC2015
85 [ "$expected" = "$result" ] && printf '%s' "PASS " || {
86 printf '\n%s\n' "FAIL: Expected $expected, Got $result ($test_case)"
87 exit 1
88 }
89 done
90 printf '\n'
91 )
92 fi
93
94 if [ -d "$day/part2-tests" ] && { [ "$part" = 2 ] || [ ! "$part" ]; }
95 then
96 (
97 cd "$day/part2-tests" || exit 1
98 printf '%s' "Part 2 Tests: "
99
100 for test_case in input*
101 do
102 num=${test_case#input}
103 expected=$(sed -n "${num}{p;q}" < ./expected)
104 result=$("../solution$ext" 2 < "$test_case")
105 result=${result#Part\ 2:\ }
106
107 # shellcheck disable=SC2015
108 [ "$expected" = "$result" ] && printf '%s' "PASS " || {
109 printf '\n%s\n' "FAIL: Expected $expected, Got $result ($test_case)"
110 exit 1
111 }
112 done
113 printf '\n'
114 )
115 fi
116
117fi