Back to blog
Product

Solving Advent of Code Puzzles with Shoreline

Using Shoreline's Oplang and Metrics System to Solve Advent of Code Puzzles
Brian Scheuermann

Introduction

Advent of code is a popular programming challenge with two puzzles a day from December 1st through 25th. People often use it as a way to try a new programming language, use languages they are already familiar with in new and interesting ways, or just as a fun challenge. Since the first day's two puzzles both involve time series data, it was a good opportunity to use Shoreline's Op language and metrics system to write the data and solve the puzzle.

First, we'll use Shoreline's statsd endpoint to write the data used in the puzzle, so that it can be ingested by Shoreline's metric system. Grabbing the data from the link above, their example uses:

<code-embed>199
200
208
210
200
207
240
269
260
263<code-embed>

Now, we'll use a Shoreline action to install <code-block>netcat<code-block> on our shoreline pods for use with <code-block>statsd<code-block>:

<code-embed>op:test-us-alpha>pod | app="shoreline" | `apt-get install -y netcat`<code-embed>

Next, we'll define a custom action to write the data:

<code-embed>op:test-us-alpha>action write_dp(VAL) = `echo -n "statsd_puzzle_metric:$VAL|g|#env:shell,mode:udp" | nc -vu -w1 localhost 9125`
Created action 'write_dp'.<code-embed>

And a custom metric query to read the above data:

<code-embed>op:test-us-alpha>metric puzzle_data = metric_query(metric_names="statsd_puzzle_metric") | mode="udp" | resolution=10s

Created metric 'puzzle_data'.<code-embed>

Now that we have an action write our data, we'll use a short bash script that uses Shoreline's CLI to write the test data:

<code-embed>brian@Brians-MacBook-Pro:alpha-cust$ cat write_data.sh
#!/usr/bin/env bash

DATA="199 200 208 210 200 207 240 269 260 263"

for dp in $DATA; do
  echo "writing $dp..."
  echo "pod | app=\"shoreline\" | write_dp($dp)" | ~/shoreline/go/bin/oplang_cli > /dev/null
  sleep 7
done;

~/shoreline [master % u=]
brian@Brians-MacBook-Pro:alpha-cust$ ./!$
./write_data.sh
writing 199...
writing 200...
writing 208...
writing 210...
writing 200...
writing 207...
writing 240...
writing 269...
writing 260...
writing 263...<code-embed>

After the data has been written, we can run a metric query that uses the <code-block>puzzle_data<code-block> metric defined above to verify that we have the right numbers:

<code-embed>op:test-us-alpha>host | limit=1 | puzzle_data |  base=1638392010000 | offset=90s
ID | TYPE | NAME                | REGION    | AZ         | TIMESTAMPS          | PUZZLE_DATA
1  | HOST | i-00f02bd632585710b | us-west-2 | us-west-2b | 2021/12/01 12:53:30 |    199.000
   |      |                     |           |            | 2021/12/01 12:53:40 |    200.000
   |      |                     |           |            | 2021/12/01 12:53:50 |    208.000
   |      |                     |           |            | 2021/12/01 12:54:00 |    210.000
   |      |                     |           |            | 2021/12/01 12:54:10 |    200.000
   |      |                     |           |            | 2021/12/01 12:54:20 |    207.000
   |      |                     |           |            | 2021/12/01 12:54:30 |    240.000
   |      |                     |           |            | 2021/12/01 12:54:40 |    269.000
   |      |                     |           |            | 2021/12/01 12:54:50 |    260.000
   |      |                     |           |            | 2021/12/01 12:55:00 |    263.000<code-embed>

Now, we're ready to solve the two puzzles.

Part 1 asks for the number of times the sequence increases. So, we can use an <code-block>irate<code-block> to compute the consecutive differences, check which of the differences are greater than <code-block>0<code-block> (giving <code-block>1<code-block> if this is the case and <code-block>0<code-block> if not), and then pipe this into a <code-block>sum<code-block> to get the total number of positive differences (<code-block>7<code-block>):

<code-embed>op:test-us-alpha>host | limit=1 | (puzzle_data | irate(2)) > 0 | sum(9) |  base=1638392010000 | offset=90s
ID | TYPE | NAME                | REGION    | AZ         | TIMESTAMPS          | PUZZLE_DATA
1  | HOST | i-00f02bd632585710b | us-west-2 | us-west-2b | 2021/12/01 12:55:00 |      7.000<code-embed>

Part 2 is similar, but asks us to compute the sliding sum with a window size of <code-block>3<code-block> increases, rather than just consecutive values. So, we'll add a <code-block>sum(3, "SLIDING")<code-block> to our previous solution to get our answer of <code-block>5<code-block>:

<code-embed>op:test-us-alpha>host | limit=1 | (puzzle_data | sum(3, "SLIDING") | irate(2)) > 0 | sum(7) |  base=1638392010000 | offset=90s
ID | TYPE | NAME                | REGION    | AZ         | TIMESTAMPS          | PUZZLE_DATA
1  | HOST | i-00f02bd632585710b | us-west-2 | us-west-2b | 2021/12/01 12:55:00 |      5.000<code-embed>

Ready to give Shoreline a try?

Join a growing community of companies making on-call better
Request demo

Find more Shoreline resources

Looking for more information? Visit our other resource sections