Advent Of Vim, Episode 1

Advent Of Vim, Episode 1

Solving AdventOfCode challenges in 10 seconds using VIM and Neovim!

·

3 min read

This journey will transform you and challenge your creative and resourceful thinking. You will explore new possibilities with VIM, going beyond what you thought it could do. And as you advance through the Advent Of Code puzzles, you will truly transform yourself if you follow the two scenarios listed below.

Every day, you'll practice two scenarios:

  1. Speed: Imagine this: you are busy editing some text in VIM, when suddenly a challenge pops up on the same file you are working on. You have to solve it quickly with motions and commands to resume your original task. speed is key here.

  2. Future-Proof: Suppose the challenge is not a one-time event, but a frequent occurrence. You need to solve it in a way that’s adaptable and scalable by writing a Vimscript that handles today’s problems and can cope with future changes.

Getting Started

Create an account with Advent of code, and open today's challenge.
Make sure that you are in sync with your team, if you don't have a team yet submit
your application here https://forms.gle/bwegPDvbgEuWKDF47

take your time with the challenge and solve the puzzle in the two mentioned scenarios. Good luck.

Solutions

This guide assumes you've already watched the accompanying walk-through YouTube video. If you haven't, please do.

Sample data

The provided sample data should be used for testing the commands while you are reading. Once you feel comfortable and confident, you can apply these commands to your actual input data.

Input:

1000
2000
3000

4000

5000
6000

7000
8000
9000

10000

Expected output:

  • part1: 24000

  • part2: 45000

Scenario #1

1.1 Part1 First method: using AWK

We can use AWK to solve both part one and two, but let's stay in sync with the walk-through video.

{s+=$1} !NF{m=s>m?s:m;s=0} END {print m}

1.2 Part1 Second method: Global Command

Like AWK, the global command operates on every line one by one.

  1. Concatenate lines containing only digits in reverse order: :g/\d/-j

  2. Replace spaces with plus signs in each line: :%s/\s/+/g

  3. Evaluate each line as an expression: :%s/.*/\=eval(submatch(0))

  4. Select the entire file, perform a descending sort, and retain only the first line: :%!sort -nr | head -1

We can do step 1 and 2 in just :%s/\v\n($)@!/+ or :%s/\v(\d)\n(\d)/\1+\2

We can replace step3 with just :% !bc

1.3 Part2 Regex And Global Command

There's not much to change from the previous solution. All you need to do is instruct head to keep three lines instead of just one. Use head -3 now that we have the top 3 sums we can easily sum up these 3 lines :%! paste -sd+ | bc

Scenario 2

In the second scenario, we need to think of the worst possible cases for our input data and solve the problem accordingly. This problem is so simple that we do not need to write any vimscript or script today.

Exercises

In case you care about this content, follow me on Twitter. I sometimes tweet about hints and solutions for these exercises.

Twitter is too noisy, make sure to enable notifications to be notified otherwise you'll miss out.

After watching the walk-through video and going through this guide, you will have plenty of knowledge to solve the exercises below.

  1. use the :g command to replicate what we did with AWK

  2. modify our AWK to solve part2