Skip to content

Commit

Permalink
Add notebook examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhamilton committed Nov 20, 2024
1 parent 92c58bb commit a1a8063
Show file tree
Hide file tree
Showing 20 changed files with 5,106 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Day 1: Not Quite Lisp

[Question:](https://adventofcode.com/2015/day/1)

Santa was hoping for a white Christmas, but his weather machine's "snow" function is powered by stars, and he's fresh out! To save Christmas, he needs you to collect fifty stars by December 25th.

Collect stars by helping Santa solve puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

Here's an easy puzzle to warm you up.

Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time.

An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor.

The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors.

For example:

(()) and ()() both result in floor 0.
((( and (()(()( both result in floor 3.
))((((( also results in floor 3.
()) and ))( both result in floor -1 (the first basement level).
))) and )())()) both result in floor -3.

To what floor do the instructions take Santa?

```sql showcodeonly
last s:sums -1 1"("=first read0 `:input/01.txt
/232
1+s?-1
/1783
```

The -1 1 maps the () to increments and decrements, we can plot this as a line to show the sum effect of each step. The last step is the answer.

```sql type='line' server='kdbserver'
([] step:`$string til count t; t; s:sums -1 1"("=t:first read0 `:input/01.txt)
```

```sql server='kdbserver'
([] (1+s?-1; last s:sums -1 1"("=first read0 `:input/01.txt))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Day 2: I Was Told There Would Be No Math

[Question:](https://adventofcode.com/2015/day/2)

The elves are running low on wrapping paper, and so they need to submit an order for more. They have a list of the dimensions (length l, width w, and height h) of each present, and only want to order exactly as much as they need.

Fortunately, every present is a box (a perfect right rectangular prism), which makes calculating the required wrapping paper for each gift a little easier: find the surface area of the box, which is 2*l*w + 2*w*h + 2*h*l. The elves also need a little extra paper for each present: the area of the smallest side.

For example:

A present with dimensions 2x3x4 requires 2*6 + 2*12 + 2*8 = 52
square feet of wrapping paper plus 6 square feet of slack, for a total of 58 square feet.
A present with dimensions 1x1x10 requires 2*1 + 2*10 + 2*10 = 42
square feet of wrapping paper plus 1 square foot of slack, for a total of 43 square feet.

All numbers in the elves' list are in feet. How many total square feet of wrapping paper should they order?

```sql type='grid' server='kdbserver'
flip `w`h`l!flip asc each "J"$ "x" vs'read0 `:input/02.txt
```

```sql showcodeonly
t:flip `w`h`l!flip asc each "J"$ "x" vs'read0 `:input/02.txt;
select total:sum (2*w*l) + (3*w*h) + 2*h*l,sum (w*h*l) + (2*w+h) from t
```

```sql server='kdbserver'
t:flip `w`h`l!flip asc each "J"$ "x" vs'read0 `:input/02.txt;
select total:sum (2*w*l) + (3*w*h) + 2*h*l,sum (w*h*l) + (2*w+h) from t
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Day 3: Perfectly Spherical Houses in a Vacuum

[Question](https://adventofcode.com/2015/day/3) Santa is delivering presents to an infinite two-dimensional grid of houses.

He begins by delivering a present to the house at his starting location, and then an elf at the North Pole calls him via radio and tells him where to move next. Moves are always exactly one house to the north (^), south (v), east (>), or west (<). After each move, he delivers another present to the house at his new location.

However, the elf back at the north pole has had a little too much eggnog, and so his directions are a little off, and Santa ends up visiting some houses more than once. How many houses receive at least one present?

For example:

> delivers presents to 2 houses: one at the starting location, and one to the east.
^>v< delivers presents to 4 houses in a square, including twice to the house at his starting/ending location.
^v^v^v^v^v delivers a bunch of presents to some very lucky children at only 2 houses.

```sql showcodeonly
count distinct sums d:(0 1;0 -1;-1 0;1 0)"^v<>"?first read0 `:input/03.txt
/2572
count distinct raze sums 0N 2#d
/2631
```

## Santas Walk
This scatter plot shows santas steps.

```sql type='scatter' server='kdbserver'
a:sums d:(0 1;0 -1;-1 0;1 0)"^v<>"?first read0 `:input/03.txt;
t:( [] x:a[;0]; y:a[;1]);
distinct t
```
Or with coloring to show repeats:
```sql type='scatter' server='kdbserver'
tt:select c:count i by x,y from t;
f:{[v] (`x,`$"y",string v)xcol select x,y from tt where c=v};
((uj/) f each 1+til 10)
```


```sql server='kdbserver'
a:count distinct sums d:(0 1;0 -1;-1 0;1 0)"^v<>"?first read0 `:input/03.txt;
/2572
b:count distinct raze sums 0N 2#d;
/2631
([] (a;b))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Day 9: All in a Single Night

[Question](https://adventofcode.com/2015/day/9) Every year, Santa manages to deliver all of his presents in a single night.

This year, however, he has some new locations to visit; his elves have provided him the distances between every pair of locations. He can start and end at any two (different) locations he wants, but he must visit each location exactly once. What is the shortest distance he can travel to achieve this?

For example, given the following distances:

London to Dublin = 464
London to Belfast = 518
Dublin to Belfast = 141

The possible routes are therefore:

Dublin -> London -> Belfast = 982
London -> Dublin -> Belfast = 605
London -> Belfast -> Dublin = 659
Dublin -> Belfast -> London = 659
Belfast -> Dublin -> London = 605
Belfast -> London -> Dublin = 982

The shortest of these is London -> Dublin -> Belfast = 605, and so the answer is 605 in this example.

What is the distance of the shortest route?

## Loading the Raw File

```sql type='sankey' server='kdbserver'
flip`f`t`d!("s s i";" ")0:`:input/09.txt
```

```sql type='grid' server='kdbserver'
flip`f`t`d!("s s i";" ")0:`:input/09.txt
```

```sql showcodeonly
r:flip`f`t`d!("s s i";" ")0:`:input/09.txt;
/combinations
c:{raze y,/:'x except/:y}[ix;]/[count[ix]-1;] ix:distinct (raze/)exec (f;t) from r;
/calculate all routes
min routes:{ sum {[x;y] first exec d from r where f in (x;y), t in (x;y) }.'-2#'prev\[count[x]-2;x] } each c
/251
max routes
/898
```

```sql server='kdbserver'
r:flip`f`t`d!("s s i";" ")0:`:input/09.txt;
c:{raze y,/:'x except/:y}[ix;]/[count[ix]-1;] ix:distinct (raze/)exec (f;t) from r;
([] enlist min routes:{ sum {[x;y] first exec d from r where f in (x;y), t in (x;y) }.'-2#'prev\[count[x]-2;x] } each c)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Day 10: Elves Look, Elves Say

[Question](https://adventofcode.com/2015/day/10)

Today, the Elves are playing a game called look-and-say. They take turns making sequences by reading aloud the previous sequence and using that reading as the next sequence. For example, 211 is read as "one two, two ones", which becomes 1221 (1 2, 2 1s).

Look-and-say sequences are generated iteratively, using the previous value as input for the next step. For each step, take the previous value, and replace each run of digits (like 111) with the number of digits (3) followed by the digit itself (1).

For example:

1 becomes 11 (1 copy of digit 1).
11 becomes 21 (2 copies of digit 1).
21 becomes 1211 (one 2 followed by one 1).
1211 becomes 111221 (one 1, one 2, and two 1s).
111221 becomes 312211 (three 1s, two 2s, and one 1).

Starting with the digits in your puzzle input, apply this process 40 times. What is the length of the result?

## Load File

```sql server='kdbserver'
([] enlist "J"$ first read0 `:input/10.txt)
```

## Step By Step

```sql server='kdbserver'
res:{raze (1_deltas i,count x),'x i:where differ x}\[50;]10 vs "J"$ first read0 `:input/10.txt;
10 sublist ([] step:til 51; res:res)
```

## Solution

```sql showcodeonly
res:{raze (1_deltas i,count x),'x i:where differ x}\[50;]10 vs "J"$ first read0 `:input/10.txt
count res 40
/252594
count res 50
/3579328
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Day 14: Reindeer Olympics

[Question](https://adventofcode.com/2015/day/14)
This year is the Reindeer Olympics! Reindeer can fly at high speeds, but must rest occasionally to recover their energy. Santa would like to know which of his reindeer is fastest, and so he has them race.

Reindeer can only either be flying (always at their top speed) or resting (not moving at all), and always spend whole seconds in either state.

For example, suppose you have the following Reindeer:

Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.
Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.

After one second, Comet has gone 14 km, while Dancer has gone 16 km. After ten seconds, Comet has gone 140 km, while Dancer has gone 160 km. On the eleventh second, Comet begins resting (staying at 140 km), and Dancer continues on for a total distance of 176 km. On the 12th second, both reindeer are resting. They continue to rest until the 138th second, when Comet flies for another ten seconds. On the 174th second, Dancer flies for another 11 seconds.

In this example, after the 1000th second, both reindeer are resting, and Comet is in the lead at 1120 km (poor Dancer has only gotten 1056 km by that point). So, in this situation, Comet would win (if the race ended at 1000 seconds).

Given the descriptions of each reindeer (in your puzzle input), after exactly 2503 seconds, what distance has the winning reindeer traveled?

## Loading the Raw Data

```sql type='bar' server='kdbserver'
flip `n`s`t`r!("s j j j";" ")0: `:input/14.txt
```

## Time-Series of each reindeer

```sql type='timeseries' server='kdbserver'
reindeert:flip `n`s`t`r!("s j j j";" ")0: `:input/14.txt;
`time xdesc update time:08:00t+til count i from flip exec sums 2503#((t#s),r#0) by n from reindeert
```

```sql showcodeonly
t:flip `n`s`t`r!("s j j j";" ")0: `:input/14.txt
max last f:flip value exec sums 2503#((t#s),r#0) by n from t
/2696
max sum each flip f=max flip f
/1084
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

![Advent of Code Tree](http://timestored.me/sqlnotebook/examples/img/advent-of-code-2015.png)

Advent of Code is a free, annual [online event](https://adventofcode.com/2024/about) that challenges participants to solve Christmas-themed programming puzzles.
The puzzles are released daily from December 1st to 25th, and participants can use any programming language they like to solve them.

These solutions were originally created by [Mark Street](https://www.linkedin.com/in/mkst)

The more complete source code is available at https://github.com/mkst/aoc.
Loading

0 comments on commit a1a8063

Please sign in to comment.