DEV Community

Cover image for Using only vim to solve AdventOfCode Challenges | Episode 1
Cipherlogs
Cipherlogs

Posted on • Originally published at cipherlogs.com

Using only vim to solve AdventOfCode Challenges | Episode 1

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

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.

Using only vim/neovim to solve Advent of code | Episode 1 - YouTube

Every day, you will face real challenges with two interconnected parts, demanding a unique approach.You will join programmers from around the world in solvin...

favicon youtube.com

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
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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

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

In case you care about this content, follow me on twitter @cipherlogs and enable notification to be notified when each episode is out.

Top comments (0)