summaryrefslogtreecommitdiff
path: root/aoc
blob: cde706aff5ddc987388884b87c95746e9c96b40f (plain)
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
154
155
156
157
158
159
160
#!/bin/zsh

# usage: source ./aoc

AOCTZ="America/New_York"
AOCSOLS=$([[ $AOCSOLS == "" ]] && echo "$HOME/git/aoc" || echo $AOCSOLS)
AOCCOOKIE=$([[ $AOCOOKIE == "" ]] && echo "/tmp/aoc_cookie.txt" || echo $AOCCOOKIE)
AOCINPUT=$([[ $AOCINPUT == "" ]] && echo "problem.txt" || echo $AOCINPUT)

if [[ "$OSTYPE" == "darwin"* ]]; then
  pastecmd="pbpaste"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
  pastecmd="wl-paste"
else
  echo "Unsupported OS"
fi

exec_test() {
  bun test "example.test.ts"
}

exec_part() {
  local logfile="logs/out_$1.txt"
  bun run "part_$1.ts" | tee $logfile
  cat $logfile
}

get_aoc_problem_path() {
  local year=$1
  local day=$(printf %02d $2)

  echo "aoc_$year/day-$day"
}

get_problem_input() {
  local cookie=$1
  local year=$2
  local day=$3
  local out=$4
  local url="https://adventofcode.com/$year/day/$day/input"
  echo "Querying $url and saving to $out..."
  curl --silent --cookie "session=$cookie" $url > $out
}

submit_problem() {
  local cookie=$1
  local year=$2
  local day=$3
  local level=$4
  local answer=$(jq -rn --arg x $5 '$x|@uri')
  local url="https://adventofcode.com/$year/day/$day/answer"

  echo "Submitting..."
  local submission=$(curl --silent --cookie "session=$cookie" -X POST $url \
       -H "Content-Type: application/x-www-form-urlencoded" \
       -d "level=$level&answer=$answer")
  echo $submission | pcregrep -o1 -M '<article><p>([\s\S]*?)<\/p><\/article>'
}

get_aoc_cookie() {
  if [[ ! -f "$AOCCOOKIE" ]]; then
    echo "Please copy your Advent of Code cookie to the clipboard, then press Enter..."
    read -r

    echo $($pastecmd) > "$AOCCOOKIE"
    echo "Cookie saved to $AOCCOOKIE"
  fi
  echo $(cat $AOCCOOKIE | tail -n 1)
}

aoc() {
  local argc=$#

  if [[ $argc -eq "0" ]]; then
    echo "/==============================\\"
    echo "| simponic's bun ts aoc helper |"
    echo "\\==============================/"
    echo "+ aoc cookie: get cookie and store to '$AOCCOOKIE'"
    echo "+ aoc init <? year day>: initialize 'template/' to problem solution and"
    echo "  pull input to '$AOCINPUT' (today by default)"
    echo "+ aoc test <? year day>: run 'exec_test' in aoc problem (today by default)"
    echo "+ aoc exec <1 | 2> <? year day>: get the output of an aoc problem without submission"
    echo "+ aoc submit <1 | 2> <? year day>: submit part one or part two to aoc problem (today by default)"
  fi

  cd $AOCSOLS

  local prevcwd=$PWD 
  local year=$(TZ="$AOCTZ" date +"%Y")
  local day=$(TZ="$AOCTZ" date +"%d")
  local curr=$(get_aoc_problem_path $year $day)

  if [[ $1 == "init" ]]; then
    if [[ 3 == $argc ]]; then
      year=$2
      day=$3
      curr=$(get_aoc_problem_path $year $day)
    fi

    if [[ ! -d $curr ]]; then
      mkdir -p $curr

      cp -r template/* $curr
      cd $curr
      get_problem_input $(get_aoc_cookie) $year $(echo $day | sed 's/^0*//') $AOCINPUT

      echo "Initialized $curr"
    else
      echo "Nothing to initialize"
    fi
  fi

  if [[ $1 == "test" ]]; then
    if [[ 3 == $argc ]]; then
      year=$2
      day=$3
      curr=$(get_aoc_problem_path $year $day)
    fi
    if [[ ! -d $curr ]]; then
      aoc init $year $day
    fi

    cd $curr
    exec_test
  fi

  if [[ $1 == "exec" ]]; then
    if [[ 4 == $argc ]]; then
      year=$3
      day=$4
      curr=$(get_aoc_problem_path $year $day)
    fi
    cd $curr

    local level=$2
    exec_part $level
  fi

  if [[ $1 == "submit" ]]; then
    if [[ 4 == $argc ]]; then
      year=$3
      day=$4
      curr=$(get_aoc_problem_path $year $day)
    fi
    cd $curr

    local level=$2
    local output=$(exec_part $level | tail -n 1)

    echo "..====.."
    echo "Submitting Answer=($output)"
    submit_problem $(get_aoc_cookie) $year $(echo $day | sed 's/^0*//') $level $output
  fi

  if [[ $1 == "cookie" ]]; then
    get_aoc_cookie
  fi

  cd $prevcwd
}