-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc.vim
48 lines (41 loc) · 1.42 KB
/
aoc.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
" read data from input file
:r input.txt
" define macro q that:
" - selects the current paragraph (vip)
" - joins the lines with :j (J does not work for single line paragraphs)
" - goes down one line (which should be empty)
:let @q = "vip:j\<cr>j"
" execute the macro many times to apply this to all paragraphs
:norm 999@q
" get rid of empty lines
:g/^$/d
" transform lines to sums
:%s/ /+/g
" to to first line, first character
:norm gg0
" define macro v that:
" - yanks the line into the default register (")
" - we are still in insert mode from the c$, so <C-r> = is used to insert from
" the expression register (=)
" - <C-r> " inserts the value of the default register (which is the line with
" the sum)
" - evaluated sum ends up in the line
" - 0j goes to the beginning of the line and down one line to prepare for the
" next iteration
:let @v = "c$\<C-r>=\<C-r>\"\<cr>\<Esc>0j"
" execute the macro many times to apply this to all lines
:norm 999@v
" sort numerically in reverse order
:%!sort -rn
" keep top 3 entries (4,$ is the range from line 4 to last line, d deletes)
:4,$d
" - ggyyp: duplicate the first line (biggest sum, result for part 1)
" - JJ: join lines 2 to 4 (three biggest sums)
:norm ggyypJJ
" again, tranform line to a sum
:s/ /+/g
" we can reuse the macro v from above to evaluate the sum
:norm @v
" sum of biggest three sums is the result for part 2 and ends up in line 2
" write results to output file
:x! out