<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Cipherlogs</title>
    <description>The latest articles on DEV Community by Cipherlogs (@cipherlogs).</description>
    <link>https://dev.to/cipherlogs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1235505%2F96240dab-0825-4ad2-a7f4-121c6b3ef63c.jpg</url>
      <title>DEV Community: Cipherlogs</title>
      <link>https://dev.to/cipherlogs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cipherlogs"/>
    <language>en</language>
    <item>
      <title>Using only vim to solve AdventOfCode Challenges | Episode 3</title>
      <dc:creator>Cipherlogs</dc:creator>
      <pubDate>Sun, 17 Dec 2023 20:34:13 +0000</pubDate>
      <link>https://dev.to/cipherlogs/using-only-vim-to-solve-adventofcode-challenges-episode-3-232d</link>
      <guid>https://dev.to/cipherlogs/using-only-vim-to-solve-adventofcode-challenges-episode-3-232d</guid>
      <description>&lt;h1&gt;
  
  
  1. Getting Started
&lt;/h1&gt;

&lt;p&gt;To begin, access &lt;a href="https://adventofcode.com/2022/day/3"&gt;today's challenge&lt;/a&gt; and carefully read the instructions below before attempting to solve it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;New here?&lt;/strong&gt; &lt;code&gt;scenario1&lt;/code&gt; and &lt;code&gt;scenario2&lt;/code&gt; might be unfamiliar, these sprints are designed to stretch your limits – you'll need to tackle the problem from both angles, or your solution won't fly. So, if you're content to stay within your comfort zone, this &lt;a href="https://github.com/cipherlogs/s909/"&gt;sprint&lt;/a&gt; isn't for you. Start with the &lt;a href="https://cipherlogs.com/how-to-solve-advent-of-code-using-only-vim-episode-1"&gt;first episode&lt;/a&gt; and give it a go yourself. Spend at least 15 minutes grappling with both scenarios before peeking at the solutions.&lt;/p&gt;

&lt;p&gt;Remember, make sure that you are in sync with your team, &lt;em&gt;if you don't have a team yet submit your application here &lt;a href="https://forms.gle/bwegPDvbgEuWKDF47"&gt;https://forms.gle/bwegPDvbgEuWKDF47&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Good luck!&lt;/strong&gt;  &lt;/p&gt;

&lt;h2&gt;
  
  
  1.1 Rules
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In the first scenario, refrain from creating low-level functions that involve iterating through strings to extract duplicated characters. Instead, leverage built-in Vimscript functions creatively or utilize Unix utilities. Alternatively, if achieving the desired results promptly requires a one-liner in a programming language, that approach is acceptable. However, avoid reinventing the wheel; the emphasis is on efficiency in this speed-focused scenario.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the second scenario, pay attention to the following considerations:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The initial aspect of the challenge involves splitting each line in half. However, the script should be flexible enough to allow for splitting into thirds, quarters, or any other specified interval. Ensure your script is capable of dynamically handling different split configurations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For the first part of the challenge, when splitting each line into segments of size 'n,' retain only those chunks that match the specified size. For instance, if &lt;code&gt;'abcdefgh'&lt;/code&gt; is split into groups of 3, the desired result should be &lt;code&gt;['abc', 'def']&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second part of the challenge focuses on joining lines in groups of &lt;code&gt;3&lt;/code&gt;. Extend the script's capability to accommodate future needs for joining lines in groups of &lt;code&gt;5&lt;/code&gt;, &lt;code&gt;7&lt;/code&gt;, or any other specified interval &lt;code&gt;n&lt;/code&gt;. Ensure your script is adaptable to varying group sizes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1.2 Sample Data
&lt;/h2&gt;

&lt;p&gt;The provided sample data should be used for testing the commands. Once you feel comfortable and confident, you can apply these commands to your actual input data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Expected output&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;part1:&lt;/strong&gt; 157&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;part2:&lt;/strong&gt; 70&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  2. Solutions
&lt;/h1&gt;

&lt;p&gt;This guide assumes you've already watched the accompanying walk-through YouTube video. If you haven't, please do.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/YazmNjy-mCA"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  2.1 The First Scenario
&lt;/h2&gt;

&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Scenario1 Part1:
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Define a function &lt;code&gt;Char2nr&lt;/code&gt; to calculate character priorities.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;code&gt;getline('.')&lt;/code&gt; to get the current line.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Split the line into left and right halves.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;code&gt;grep&lt;/code&gt; to find unique characters in both halves.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Execute the command and replace the line with the output.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Apply the process to all lines.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Convert unique characters to priorities using &lt;code&gt;:%s/.*/\=Char2nr(submatch(0))&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sum up the priorities using &lt;code&gt;%! paste -sd+ | bc&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Scenario1 Part2:
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Merge three lines at a time using &lt;code&gt;:g/^/.,+2j&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Calculate the total score using &lt;code&gt;let total += split(getline('.'))-&amp;gt;reduce(Intersect)-&amp;gt;Char2nr()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;View the final score with &lt;code&gt;:echo total&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  2.1.1 Part One
&lt;/h3&gt;

&lt;p&gt;Let's begin by addressing the obvious.&lt;/p&gt;

&lt;p&gt;As we progress through the challenge, we will eventually need to convert characters to their corresponding priority levels.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Lowercase item types 'a' through 'z' have priorities ranging from 1 through 26.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Uppercase item types 'A' through 'Z' have priorities ranging from 27 through 52.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;" 96 because -char2nr('a') + 1 == 96
" 38 because -char2nr('A') + 26 + 1 == 38

let Char2nr = {c -&amp;gt; char2nr(c) - (tolower(c) ==# c ? 96 : 38)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the &lt;code&gt;==#&lt;/code&gt; operator to perform a case-sensitive comparison, as opposed to the &lt;code&gt;==&lt;/code&gt; operator.&lt;/p&gt;

&lt;p&gt;Now, let's proceed with the following steps:&lt;/p&gt;

&lt;p&gt;Initiate the recording of a macro with &lt;code&gt;qq&lt;/code&gt;, then extract useful information that we'll need later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let str = getline('.') | let mid = len(str)/2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Having acquired the content of the current line and its midpoint, let's split it in half and save the left and right sides:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let [left, right] = [str[:mid+1], str[mid:]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To extract unique characters from both the left and right sides, we can employ &lt;code&gt;grep&lt;/code&gt;. In this case we want to highlight the unique characters so we'll use &lt;code&gt;grep -o&lt;/code&gt;. Constructing the command within VIM requires formatting, for a more cleaner look we'll use &lt;code&gt;printf()&lt;/code&gt; instead of concatenation. Here's the formatted command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let cmd = printf(".!echo '%s' | grep -o '[%s]' | sort -u | head -1", left, right)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;!&lt;/code&gt; signals to Vim that this is a shell command, and the &lt;code&gt;.&lt;/code&gt; indicates that Vim should replace the current line with the output of our constructed command.&lt;/p&gt;

&lt;p&gt;Now, Execute the command using &lt;code&gt;:exe cmd&lt;/code&gt; and move to the next line and conclude the macro with &lt;code&gt;q&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;With the macro defined, apply it to the remaining lines using &lt;code&gt;:% norm @q&lt;/code&gt;. This process transforms all the lines into a single unique character.&lt;/p&gt;

&lt;p&gt;The next step is to convert all unique characters to their corresponding priority and sum them up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:%s/.*/\=Char2nr(submatch(0))/ | %! paste -sd+ | bc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Congratulations, Part One is now solved!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before advancing to the next part, let's review and identify potential enhancements. During the initial attempt, it's common to miss the broader perspective, especially in this scenario where we are emphasizing speed.&lt;/p&gt;

&lt;p&gt;The command we composed to find unique characters in two chunks of strings can be encapsulated in a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let Intersect = {a, b -&amp;gt; printf("echo '%s' | grep -o '[%s]' | sort -u | head -1", a, b)-&amp;gt;system()}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function now returns a string instead of mutating the actual line invoked from. Furthermore, it operates as a reducer, a concept from functional programming. Reducers should be designed to accumulate, akin to recording a macro that can be repeated.&lt;/p&gt;

&lt;p&gt;However, there's a subtlety. If we apply our reducer to chunks of strings (2 by 2), there's a potential issue. The reducer returns strings with new lines (due to &lt;code&gt;grep&lt;/code&gt; and &lt;code&gt;sort&lt;/code&gt;), and merging these results might be inconsistent. Additionally, using &lt;code&gt;head&lt;/code&gt; in this situation is discouraged. &lt;em&gt;More details can be found in the exercises section&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To address this, the function must consistently return either an empty string or a string, regardless of the input. Let's remove any new lines from the output using &lt;code&gt;tr -d '\n'&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let Intersect = {a, b -&amp;gt; printf("echo '%s' | grep -o '[%s]' | sort -u | tr -d '\n'", a, b)-&amp;gt;system()}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Understanding the underlying mechanics is crucial; it provides more value than just learning about specific utilities.&lt;/p&gt;

&lt;p&gt;Now we can proceed to the next part.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.1.2 Part Two
&lt;/h3&gt;

&lt;p&gt;Our current objective is to consolidate three lines at a time, extract unique characters from each merged line, and finally, display the cumulative score for all lines. This might sound like a lot.&lt;/p&gt;

&lt;p&gt;Well, let's see what vim has to say about that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:let total = 0
:g/^/.,+2j | let total += split(getline('.'))-&amp;gt;reduce(Intersect)-&amp;gt;Char2nr()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Congrats, Part Two is solved!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These commands efficiently merge lines in groups of three and calculate the total score. The final score is stored in the 'total' variable. To view the result, simply run &lt;code&gt;:echo total&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.2 The Second Scenario
&lt;/h2&gt;

&lt;p&gt;Let's begin by revisiting familiar ground. You still remember &lt;code&gt;Char2nr&lt;/code&gt; right?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let Char2nr = {c -&amp;gt; char2nr(c) - (tolower(c) ==# c ? 96 : 38)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, onto the Intersect function. While we could reuse the one from the first scenario, for the sake of learning, let's explore an alternative implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let Intersect = {a, b -&amp;gt;split(a, '\zs')-&amp;gt;filter({_, c -&amp;gt; c =~# "[".b."]"})-&amp;gt;join('')}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We use '\zs' to split at the start of each character, transforming, for instance, "abc" into ['a', 'b', 'c'].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The filter step retains only the characters matching our regex pattern, with the &lt;code&gt;c =~ 'regex'&lt;/code&gt;, the use of &lt;code&gt;#&lt;/code&gt; is to ensure case sensitivity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, we reassemble the characters into a single string using &lt;code&gt;join()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With both &lt;code&gt;Intersect&lt;/code&gt; and &lt;code&gt;Char2nr&lt;/code&gt; in place, let's streamline the process further by creating a utility function to determine the priority level of each line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let GetPriority = {str -&amp;gt; split(str)-&amp;gt;reduce(g:Intersect)-&amp;gt;g:Char2nr()}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Going forward, we only need to interact with &lt;code&gt;GetPriority&lt;/code&gt;, simplifying our code.&lt;/p&gt;

&lt;p&gt;Now, let's approach the challenge from a different perspective, considering the additional rules introduced. Despite seeming disparate, joining lines and splitting strings boil down to manipulating delimiters.&lt;/p&gt;

&lt;p&gt;To address this, let's design a function that adds a space delimiter to any list every n elements. For example, calling &lt;code&gt;SpaceEvery(2, ['a', 'b', 'c', 'd'])&lt;/code&gt; should yield &lt;code&gt;['a', 'b', ' ', 'c', 'd']&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let SpaceEvery = {n, xs -&amp;gt;
  \ range(n, len(xs) - n, n)
  \ -&amp;gt;map({i, x -&amp;gt; x + i})
  \ -&amp;gt;reduce({acc, i -&amp;gt; insert(xs, ' ', i)}, copy(xs))
  \}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This versatile function can now be applied to each part of the process, enhancing flexibility and maintainability.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: In case &lt;code&gt;SpaceEvery()&lt;/code&gt; is expecting shorter strings, then it is better to use &lt;code&gt;range(n, len(xs), n)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2.2.1 Part One
&lt;/h3&gt;

&lt;p&gt;The first step is to split a line in half&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let InHalf = {xs -&amp;gt; g:SpaceEvery(len(xs)/2, split(xs, '\zs'))-&amp;gt;join('')}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.2.2 Part Two
&lt;/h3&gt;

&lt;p&gt;the second part is to join lines in groups of 3&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let JoinBy3 = {xs -&amp;gt; g:SpaceEvery(3, xs)-&amp;gt;join(',')-&amp;gt;split()-&amp;gt;map("split(v:val,',')-&amp;gt;join()")}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.2.3 Finale Part
&lt;/h3&gt;

&lt;p&gt;Now that we have created all the necessary utilities, let’s apply them to our input file.&lt;/p&gt;

&lt;p&gt;For the first part, we can say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo
      \ readfile("inputdemo")
      \ -&amp;gt;map("InHalf(v:val)")
      \ -&amp;gt;map("GetPriority(v:val)")
      \ -&amp;gt;reduce({a, b -&amp;gt; a + b})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And for the second part:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo
      \ same as before
      \ -&amp;gt;JoinBy3()
      \ same as before
      \ same as before
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Congrats&lt;/strong&gt;, Not only did we solve the original challenge, but we also overcame the additional constraints and rules.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.2.4 Finale Script
&lt;/h3&gt;

&lt;p&gt;the final script is as follow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let Intersect = {a, b -&amp;gt;
      \ split(a, '\zs')-&amp;gt;filter({_, c -&amp;gt; c =~# "[".b."]"})-&amp;gt;join('')}

let Char2nr = {c -&amp;gt;
      \ char2nr(c) - (tolower(c) ==# c ? 96 : 38)}

let GetPriority = {str -&amp;gt;
      \ split(str)-&amp;gt;reduce(g:Intersect)-&amp;gt;g:Char2nr()}

let SpaceEvery = {n, xs -&amp;gt;
      \ range(n, len(xs) - n, n)
      \ -&amp;gt;map({i, x -&amp;gt; x + i})
      \ -&amp;gt;reduce({acc, i -&amp;gt; insert(xs, ' ', i)}, copy(xs))}

let InHalf = {xs -&amp;gt;
      \ g:SpaceEvery(len(xs)/2, split(xs, '\zs'))-&amp;gt;join('')}

let JoinBy3 = {xs -&amp;gt;
      \ g:SpaceEvery(3, xs)
      \ -&amp;gt;join(',')
      \ -&amp;gt;split()
      \ -&amp;gt;map("split(v:val,',')-&amp;gt;join()")}


echo
      \ readfile("inputdemo")
      \ -&amp;gt;JoinBy3()
      \ -&amp;gt;map("GetPriority(v:val)")
      \ -&amp;gt;reduce({a, b -&amp;gt; a + b})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  4. Bonus
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;For a more detailed explanation of this section, please refer to the walkthrough video.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the second scenario, we decided to reinvent the wheel. This is entirely acceptable because we are in a learning process, and my objective is to equip you with the skills necessary for the challenges that lie ahead. when we start the &lt;a href="https://github.com/cipherlogs/s909"&gt;second season&lt;/a&gt;, we will delve into the development of plugins and tackle more complex tasks. It is my intention that these foundational concepts become second nature to you, akin to drinking water.&lt;/p&gt;

&lt;p&gt;Allow me to illustrate a real-life scenario. When expediency is essential, opting for a quick-and-dirty approach is viable. Instead of employing the &lt;code&gt;readfile()&lt;/code&gt; function, we can load a file into memory using buffers and manipulate it as if it were open(technically it is open).&lt;/p&gt;

&lt;p&gt;Let's explore how we can use commands in a script to address a particular problem. Please note that I will demonstrate this using the original challenge, not the augmented version from the second scenario, as the latter involves additional elements.&lt;/p&gt;

&lt;p&gt;Imagine that your script file, 'myscript.vim', is open. If you input the following lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;badd inputfile | buff inputfile
buff myscript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is equivalent to &lt;code&gt;:e inputfile&lt;/code&gt;, followed by &lt;code&gt;buff myscript&lt;/code&gt;, swiftly transitioning between the two files. Executing this file might seem uneventful, but VIM's efficiency creates an illusion of no movement due to its rapid switching between files.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Now, what's the significance of this?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We can insert commands in between and modify our files using vim commands. Observe the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;badd inputfile | buff inputfile
norm ggdd
buff myscript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this scenario, our script switches to &lt;code&gt;inputfile&lt;/code&gt;, navigates to the first line, deletes it, and then switches back to our &lt;code&gt;myscript&lt;/code&gt; file. Remarkably efficient, isn't it?&lt;/p&gt;

&lt;p&gt;Now, let's apply this technique to solve part 2 of our challenge:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;badd inputfile | buffinputfile
let total = 0
g/^/.,+2j | let total += split(getline('.'))-&amp;gt;reduce(Intersect)-&amp;gt;Char2nr()
norm u
buff myscript
echo total
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;norm u&lt;/code&gt; command ensures that after modifying the buffer, we undo the changes to maintain the original state of the buffer. Our primary focus is on calculating the result without altering the file.&lt;/p&gt;

&lt;p&gt;This method of scriptwriting is invaluable, especially when you need to create scripts in this format or when hardcoding macros prove to be efficient. We'll explore examples later to highlight the efficiency of this approach.&lt;/p&gt;

&lt;h1&gt;
  
  
  5. Exercises
&lt;/h1&gt;

&lt;p&gt;From time to time, I'll share the solutions on Twitter, however, I want you to spend some time with the exercises on your own and with your team.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In the first scenario, instead of employing &lt;code&gt;Char2nr&lt;/code&gt; to calculate priority, achieve the same result using only &lt;code&gt;:%s&lt;/code&gt;. A helpful hint is to utilize the &lt;code&gt;'\C'&lt;/code&gt; flag to specify that the substitution should be case-sensitive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rather than consolidating three lines using the global command &lt;code&gt;:g&lt;/code&gt;, explore an alternative approach using only regular expressions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;When we wanted to make &lt;code&gt;Intersect&lt;/code&gt; acts as a reducer&lt;/p&gt;

&lt;p&gt;3.1. why we weren't capable of using &lt;code&gt;head -1&lt;/code&gt;  &lt;/p&gt;

&lt;p&gt;3.2. If &lt;code&gt;Intersect&lt;/code&gt; finds many occurrences it will return them all as a string, how can we make &lt;code&gt;Intersect&lt;/code&gt; only returns one character while keeping the function useful if used as a reducer?  &lt;/p&gt;

&lt;p&gt;3.3. In our case why is it okay to not use &lt;code&gt;tr -d '\n'&lt;/code&gt;?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the second scenario, if we find multiple unique occurrences we only choose the first one, this time select the one that appears the most.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Instead of &lt;code&gt;SpaceEvery&lt;/code&gt;. Create a function that can put any delimiter for each &lt;code&gt;n&lt;/code&gt; interval.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using buffers only, write the solution for &lt;code&gt;scenario1 part1&lt;/code&gt; and &lt;code&gt;part2&lt;/code&gt;, without opening the input file and make sure that you use the same solution that we came up with. (you can either hardcode macors using &lt;code&gt;let @q=...&lt;/code&gt; or use &lt;code&gt;norm&lt;/code&gt; to record it.)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>vim</category>
      <category>neovim</category>
      <category>linux</category>
      <category>adventofcode</category>
    </item>
    <item>
      <title>Using only vim to solve AdventOfCode Challenges | Episode 2</title>
      <dc:creator>Cipherlogs</dc:creator>
      <pubDate>Sun, 17 Dec 2023 20:31:33 +0000</pubDate>
      <link>https://dev.to/cipherlogs/using-only-vim-to-solve-adventofcode-challenges-episode-2-561b</link>
      <guid>https://dev.to/cipherlogs/using-only-vim-to-solve-adventofcode-challenges-episode-2-561b</guid>
      <description>&lt;p&gt;&lt;strong&gt;Note: It's important to read the first episode to understand the context of how these challenges are solved.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Getting Started
&lt;/h1&gt;

&lt;p&gt;Open &lt;a href="https://adventofcode.com/2022/day/1"&gt;today's challenge&lt;/a&gt; and make sure that you are in sync with your team, if you don't have a team yet submit your application &lt;a href="https://forms.gle/bwegPDvbgEuWKDF47"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take your time with the challenge and solve the puzzle in the two mentioned scenarios. &lt;strong&gt;Good luck.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1.1 Rules
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In the first scenario, if you plan to hardcode all possible outcomes of the game, avoid manual entry. Instead, find an automatic way to handle them. Imagine having one million potential outcomes for the game—your approach should be automated for speed. Utilize your editor for efficiency. &lt;strong&gt;Remember, the emphasis in the first scenario is on speed.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the second scenario, write a Vimscript that accommodates the worst-case scenario. Consider a situation where each symbol might signify something else in the future. Structure your code to distinctly separate the game logic from symbol deciphering.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1.2 Sample Data
&lt;/h2&gt;

&lt;p&gt;The provided sample data should be used for testing the commands. Once you feel comfortable and confident, you can apply these commands to your actual input data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A Y
B X
C Z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Expected output&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;part1: 15&lt;/li&gt;
&lt;li&gt;part2: 12&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  2. Solutions
&lt;/h1&gt;

&lt;p&gt;This guide assumes you've already watched the accompanying walk-through YouTube video. If you haven't, please do.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/ACzoJj-sunE"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  2.1 The First Scenario
&lt;/h2&gt;

&lt;h3&gt;
  
  
  2.1.1 Part One
&lt;/h3&gt;

&lt;p&gt;Our input data consists of symbols representing each player's move, and our task is to calculate the score of each game. The quickest way is to map all possible outcomes and replace each line with the corresponding score. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A Y
B X
C Z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will translate to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8
1
6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This mapping can be done using Vim dictionaries, acting like hashtables or objects. Below is a function &lt;code&gt;Swap&lt;/code&gt; containing a dictionary with all possible game outcomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function! Swap(line)
  let scores = {
    \ 'A X': '4',
    \ 'A Y': '8',
    \ 'A Z': '3',
    \ 'B X': '1',
    \ 'B Y': '5',
    \ 'B Z': '9',
    \ 'C X': '7',
    \ 'C Y': '2',
    \ 'C Z': '6',
  \ }

  return scores[a:line]
endfunction
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While we can use this function immediately, today's challenge rules prohibit hard coding outcomes. Instead, we need a fast and creative way to enter them.&lt;/p&gt;

&lt;p&gt;To generate all possible symbols, use &lt;code&gt;:r! echo \\n{A..C}" "{X..Z}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the walkthrough video, alternatives to Bash were discussed, showcasing the flexibility to choose more powerful tools based on specific needs.&lt;/p&gt;

&lt;p&gt;This Haskell code achieves the same result as the previous Bash code, but with a key advantage: scalability. We can easily make further transformations to the generated output by adding additional functions to the pipeline.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;putStr&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="n"&gt;unlines&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="sc"&gt;'C'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sc"&gt;'X'&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="sc"&gt;'Z'&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So far, this is what we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A X 
A Y 
A Z 
B X 
B Y 
B Z 
C X 
C Y 
C Z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and our end goal is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A X: score 
A Y: score
... 
C Z: score
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First let's create a function on the fly, that will assign the bonus points, if we loose we get no bonus, if we end with a draw we get the hand move point plus 3 and if we win we get the hand move point plus 6.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let BonusOf = {a, b -&amp;gt; a &amp;gt; b ? 0 : (a &amp;lt; b ? 6 : 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to assign the correct score to each combination, let's record a quick macro.&lt;/p&gt;

&lt;p&gt;Start by recording a macro on the first line with the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:let ln = line('.')
:let playerA = ln-1/3 + 1
:let playerB = ln%3 ? ln%3 : 3

gg0C'p&amp;lt;Esc&amp;gt;A: &amp;lt;C-r&amp;gt;=playerB + (playerA&amp;gt;1 &amp;amp;&amp;amp; playerB&amp;gt;1 ? BonusOf(playerA,playerB) : BonusOf(playerA%3,playerB%3))jq
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we covered in the walkthrough video, we're using line numbers to determine the value of each hand. By default, "A, X" is 1, "B, Y" is 2, and so on.&lt;/p&gt;

&lt;p&gt;We also use BonusOf(playerA%3, playerB%3) to round the value of each hand by three. This is because "Rock" is typically 1 and "Paper" is 2. However, we need to adjust this logic for "Scissors" (3), which should not be considered stronger than "Rock." The walkthrough video explains this rounding process in more detail.&lt;/p&gt;

&lt;p&gt;Now, let's apply the macro we recorded on the first line to the rest of the lines. Simply use &lt;code&gt;.,$ norm @q&lt;/code&gt; and you're good to go!&lt;/p&gt;

&lt;p&gt;Then wrap everything in a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function! Swap(line)
  let scores = {
        \ 'A X': 4,
        \ 'A Y': 8,
        \ 'A Z': 3,
        \ 'B X': 1,
        \ 'B Y': 5,
        \ 'B Z': 9,
        \ 'C X': 7,
        \ 'C Y': 2,
        \ 'C Z': 6,
        \ }

  return scores[a:line]
endfunction
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the input file and run the function on each line using &lt;code&gt;:%s/.*/\=Swap(submatch(0))&lt;/code&gt;. To calculate the sum of all lines, use &lt;code&gt;%! paste -sd+ | bc&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.1.2 Part Two
&lt;/h3&gt;

&lt;p&gt;For the second part of the challenge, the approach remains similar. Adjust the way you calculate the score for each round. You'll end up with a function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function! Swap2(line)
  let scores = {
    \ 'A X': 3,
    \ 'A Y': 4,
    \ 'A Z': 8,
    \ 'B X': 1,
    \ 'B Y': 5,
    \ 'B Z': 9,
    \ 'C X': 2,
    \ 'C Y': 6,
    \ 'C Z': 7,
  \ }

  return scores[a:line]
endfunction
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Apply the same steps as in Part One to obtain the answer for the second part efficiently. Don't retype your previous commands, just recall them from the command history. this step shouldn't take more than 5 seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.2 The Second Scenario
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Defining Essential Functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First and foremost, let's address the essential functions required for our game logic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let Bonus = {a, b -&amp;gt; a &amp;gt; b ? 0 : (a &amp;lt; b ? 6 : 3)}
let Score = {a, b -&amp;gt; a&amp;gt;1 &amp;amp;&amp;amp; b&amp;gt;1 ? g:Bonus(a, b) + b : g:Bonus(a%3, b%3) + b}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These functions establish the scoring mechanism for the game.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Handling Hand Moves&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To proceed, we need to assign points to each hand move:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let pointOf = {'rock': 1, 'paper': 2, 'scissors': 3}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additionally, we create a dictionary to map each move to its defeating move:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let next = {'rock': 'scissors', 'paper': 'rock', 'scissors': 'paper'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Processing Input Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Assuming our input data is stored in a file named &lt;code&gt;input&lt;/code&gt;, we start by reading and printing its content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo readfile("input")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is a list of lines in the format &lt;code&gt;['symbolA symbolB', 'symbolA symbolB', ...]&lt;/code&gt;. We transform it to &lt;code&gt;[['symbolA', 'symbolB'], ...]&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo
      \ readfile("inputdemo.vim")
      \ -&amp;gt;map("split(v:val, ' ')")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step now is to translate each symbol to what it mean, whether is it 'rock', 'paper' or 'scissors'. for now, we are still in the testing phase, let's create a dummy function that will do this translation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ToHand(symbol)
  return {_ -&amp;gt; 'rock'}
endfunction
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our &lt;code&gt;ToHand&lt;/code&gt; function takes a symbol and returns a new function. This new function, in turn, takes an argument and ultimately returns the name of the hand. In our example, it will always return "rock."&lt;/p&gt;

&lt;p&gt;You might wonder why we don't just create a function that directly returns the string "rock" instead of adding an extra layer of abstraction. The reason is that the function we return will later be able to accept symbols from either the left or right column. This allows each function to have information about the opponent's move.&lt;/p&gt;

&lt;p&gt;While &lt;code&gt;ToHand&lt;/code&gt; may seem simple now, we'll revisit it and expand its functionality later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo
      \ readfile("inputdemo.vim")
      \ -&amp;gt;map("split(v:val, ' ')")
      \ -&amp;gt;map({_, xs -&amp;gt; [ToHand(xs[0])(xs[1]), xs[1]]})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we map over each element, applying ToHand to symbols in the left column for player one's move, and then applying it to the right column for player two's move.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      \ -&amp;gt;map({_, xs -&amp;gt; [xs[0], ToHand(xs[1])(xs[0])]})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While applying both functions simultaneously might seem tempting, it would make our lambda function unnecessarily complex. Instead, we'll follow the natural flow of the game: player one plays their move, the game state updates, and then player two reacts based on the updated state. This approach keeps our code easy to read.&lt;/p&gt;

&lt;p&gt;Next, we convert hand names to their corresponding points:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      \ -&amp;gt;map({_, xs -&amp;gt; map(xs, "g:pointOf[v:val]")})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We call the Score function to obtain the score for each game round and sum up all the numbers in the list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      \ -&amp;gt;map({_, v -&amp;gt; g:Score(v[0], v[1])})
      \ -&amp;gt;reduce({a, b -&amp;gt; a + b})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This encapsulates the entire game logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: While this script utilizes nested maps, it aligns with the nature of Vimscript. In other languages, we might explore composition or transduction, but for now, we'll keep Vimscript within its natural confines. Any future modifications will be limited to the &lt;code&gt;ToHand&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: I have written this using map on top of map. for this script is fine, in other languages, I might compose or transduce but let's not push vimscript doing things that aren't naturally built for. However, it's acknowledged that in future iterations, Lua may be explored for enhanced functionality, as hinted in the &lt;a href="https://github.com/cipherlogs/s909/blob/main/s2.md"&gt;seond season&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Moving forward, the sole modification we'll make is to our placeholder function &lt;code&gt;ToHand&lt;/code&gt;; all other aspects of the script remain unchanged.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.2.1 Part One
&lt;/h3&gt;

&lt;p&gt;In the initial phase of the puzzle, we assign 'rock' to A and X, 'paper' to B and Y, and 'scissors' to C and Z.&lt;/p&gt;

&lt;p&gt;Now, let's refine our 'ToHand' function to accurately represent each symbol.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ToHand(symbol)
  let symbols = {
        \ 'A': {_ -&amp;gt; 'rock'},
        \ 'B': {_ -&amp;gt; 'paper'},
        \ 'C': {_ -&amp;gt; 'scissors'},
        \ 'X': {_ -&amp;gt; 'rock'},
        \ 'Y': {_ -&amp;gt; 'paper'},
        \ 'Z': {_ -&amp;gt; 'scissors'},
        \}

  return symbols[a:symbol]
endfunction
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This modification ensures that our 'ToHand' function correctly translates each symbol to its corresponding hand gesture.&lt;/p&gt;

&lt;p&gt;And that's it – we're finished!&lt;/p&gt;

&lt;h3&gt;
  
  
  2.2.2 Part Two
&lt;/h3&gt;

&lt;p&gt;In the second part, we define the conditions for three scenarios: losing, drawing, and winning denoted by X, Y, and Z respectively. Achieving these outcomes involves specific modifications to our &lt;code&gt;ToHand&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        \ 'X': {x -&amp;gt; g:next[x]}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For scenario X, which corresponds to a loss, we take the enemy's hand move, represented by arg &lt;code&gt;x&lt;/code&gt;, and call the &lt;code&gt;next&lt;/code&gt; function to determine the subsequent move required for a loss.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        \ 'Y': {x -&amp;gt; x}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the case of Y, signifying a draw, we simply return the same enemy move denoted by x.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        \ 'Z': {x -&amp;gt; g:next[g:next[x]]}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For Z, indicating a win, we perform the inverse of the X scenario.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.2.3 The Final Script
&lt;/h3&gt;

&lt;p&gt;That's all the necessary adjustments to the &lt;code&gt;ToHand&lt;/code&gt; function. The final and comprehensive script is provided below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ToHand(symbol)
  let symbols = {
        \ 'A': {_ -&amp;gt; 'rock'},
        \ 'B': {_ -&amp;gt; 'paper'},
        \ 'C': {_ -&amp;gt; 'scissors'},
        \ 'X': {x -&amp;gt; g:next[x]},
        \ 'Y': {x -&amp;gt; x},
        \ 'Z': {x -&amp;gt; g:next[g:next[x]]},
        \}

  return symbols[a:symbol]
endfunction


" CORE, DO NOT TOUCH
let pointOf = {'rock': 1, 'paper': 2, 'scissors': 3}
let next = {'rock': 'scissors', 'paper': 'rock', 'scissors': 'paper'}
let Bonus = {a, b -&amp;gt; a &amp;gt; b ? 0 : (a &amp;lt; b ? 6 : 3)}
let Score = {a, b -&amp;gt; a&amp;gt;1 &amp;amp;&amp;amp; b&amp;gt;1 ? g:Bonus(a, b) + b : g:Bonus(a%3, b%3) + b}

echo
      \ readfile("inputdemo.vim")
      \ -&amp;gt;map("split(v:val, ' ')")
      \ -&amp;gt;map({_, xs -&amp;gt; [ToHand(xs[0])(xs[1]), xs[1]]})
      \ -&amp;gt;map({_, xs -&amp;gt; [xs[0], ToHand(xs[1])(xs[0])]})
      \ -&amp;gt;map({_, xs -&amp;gt; map(xs, "g:pointOf[v:val]")})
      \ -&amp;gt;map({_, v -&amp;gt; g:Score(v[0], v[1])})
      \ -&amp;gt;reduce({a, b -&amp;gt; a + b})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Exercises
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In your preferred programming language, create a script that can automatically generate all potential game outcomes and their score.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run your code in Vim to generate all the required data automatically.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Below is an example using Javascript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getCombos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;xs&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;ys&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;xs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flatMap &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;ys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pointOf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;move&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;moves&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;X&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Y&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Z&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,};&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;moves&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;move&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;some&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;xs&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;xA&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;xB&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;xs&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;some &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;xA&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;xB&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isDraw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;some &lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;AX&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;BY&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CZ&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isLost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;some &lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;AZ&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;BX&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CY&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;format&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;combos&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;combos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;combo&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;enemyMove&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;combo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myMove&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;combo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;shapeScore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pointOf &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myMove&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nf"&gt;isDraw &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;enemyMove&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myMove&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;shapeScore&lt;/span&gt;
          &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;isLost &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;enemyMove&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myMove&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;shapeScore&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;shapeScore&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`'&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;enemyMove&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;myMove&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;':&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;,`&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;log &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;format &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getCombos &lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;A&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;B&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;C&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;X&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Y&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Z&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>vim</category>
      <category>neovim</category>
      <category>productivity</category>
      <category>linux</category>
    </item>
    <item>
      <title>Using only vim to solve AdventOfCode Challenges | Episode 1</title>
      <dc:creator>Cipherlogs</dc:creator>
      <pubDate>Sun, 17 Dec 2023 20:27:35 +0000</pubDate>
      <link>https://dev.to/cipherlogs/using-only-vim-to-solve-adventofcode-challenges-episode-1-2e99</link>
      <guid>https://dev.to/cipherlogs/using-only-vim-to-solve-adventofcode-challenges-episode-1-2e99</guid>
      <description>&lt;p&gt;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 &lt;a href="https://adventofcode.com/"&gt;Advent Of Code&lt;/a&gt; puzzles, you will truly transform yourself if you follow the two scenarios listed below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Every day, you'll practice two scenarios:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;Create an account with Advent of code, and open &lt;a href="https://adventofcode.com/2022/day/1"&gt;today's challenge&lt;/a&gt;.&lt;br&gt;&lt;br&gt;
Make sure that you are in sync with your team, if you don't have a team yet submit your application &lt;a href="https://forms.gle/bwegPDvbgEuWKDF47"&gt;here&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;take your time with the challenge and solve the puzzle in the two mentioned scenarios. &lt;strong&gt;Good luck.&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;p&gt;This guide assumes you've already watched the accompanying walk-through YouTube video. If you haven't, please do.&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://www.youtube.com/watch?si=iIakQJGp2lCZ-SCB&amp;amp;v=9fUDbq933fE&amp;amp;feature=youtu.be" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--2_hqbQFP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ytimg.com/vi/9fUDbq933fE/maxresdefault.jpg%3Fsqp%3D-oaymwEmCIAKENAF8quKqQMa8AEB-AH-CYAC0AWKAgwIABABGEYgUChyMA8%3D%26rs%3DAOn4CLB_V_Jr5xup7Tu_T059_Il6wjoKvQ" height="450" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://www.youtube.com/watch?si=iIakQJGp2lCZ-SCB&amp;amp;v=9fUDbq933fE&amp;amp;feature=youtu.be" rel="noopener noreferrer" class="c-link"&gt;
          Using only vim/neovim to solve Advent of code | Episode 1 - YouTube
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          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...
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ob9E8PNH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://www.youtube.com/s/desktop/e4d15d2c/img/favicon.ico" width="16" height="16"&gt;
        youtube.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;





&lt;h3&gt;
  
  
  Sample data
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1000
2000
3000

4000

5000
6000

7000
8000
9000

10000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Expected output:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;part1: 24000&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;part2: 45000&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;h3&gt;
  
  
  Scenario #1
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1.1 Part1 First method: using AWK
&lt;/h4&gt;

&lt;p&gt;We can use AWK to solve both part one and two, but let's stay in sync with the walk-through video.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{s+=$1} !NF{m=s&amp;gt;m?s:m;s=0} END {print m}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  1.2 Part1 Second method: Global Command
&lt;/h4&gt;

&lt;p&gt;Like AWK, the global command operates on every line one by one.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Concatenate lines containing only digits in reverse order: &lt;code&gt;:g/\d/-j&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Replace spaces with plus signs in each line: &lt;code&gt;:%s/\s/+/g&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Evaluate each line as an expression: &lt;code&gt;:%s/.*/\=eval(submatch(0))&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select the entire file, perform a descending sort, and retain only the first line: &lt;code&gt;:%!sort -nr | head -1&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;We can replace step3 with just &lt;code&gt;:% !bc&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1.3 Part2 Regex And Global Command
&lt;/h3&gt;

&lt;p&gt;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 &lt;code&gt;head -3&lt;/code&gt; now that we have the top 3 sums we can easily sum up these 3 lines &lt;code&gt;:%! paste -sd+ | bc&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 2
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h1&gt;
  
  
  Exercises
&lt;/h1&gt;

&lt;p&gt;After watching the walk-through video and going through this guide, you will have plenty of knowledge to solve the exercises below.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;use the &lt;code&gt;:g&lt;/code&gt; command to replicate what we did with AWK&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;modify our AWK to solve part2&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In case you care about this content, follow me on twitter &lt;a href="https://twitter.com/intent/follow?screen_name=cipherlogs"&gt;@cipherlogs&lt;/a&gt; and enable notification to be notified when each episode is out.&lt;/p&gt;

</description>
      <category>vim</category>
      <category>neovim</category>
      <category>productivity</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
