-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc.vim
64 lines (52 loc) · 1.95 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
" ----------------------------------- input ------------------------------------
" duplicate empty line, so we hava an empty trailing line
" this lets us use macro @s for both parts
:norm yyP
" read data from input file
:r input.txt
" duplicate input lines, so that we can first do part 1 and then part 2
" this will leave a blank line between the two parts, and the cursor on line 1
:norm VGykP
" ----------------------------------- part 1 -----------------------------------
" \v is very magic mode (no more \ before (, ), {, }, etc.)
" .{-} is a non-greedy match, so the (\d) matches the first digit in the line
" .* is a greedy match, so the (\d) matches the last digit in the line
:let d1 = ':s/\v.{-}(\d).*/\1/'
:let d2 = ':s/\v.*(\d).*/\1+/'
" yyp - duplicate line
" d2 - reduce second line to last digit
" k - move up
" d1 - reduce first line to last digit, incl. trailing + for summing later
" gJ - join lines without spaces
" j - move down to next input line
:let @q = "yyp" . d2 . "\<cr>k" . d1 . "\<cr>gJj"
" execute macro 1000 times (number of lines in input file)
:norm 1000@q
" remove trailing +, join lines, evaluate sum via expression register
:let @s = "k$xvipgJ0c$\<C-r>=\<C-r>\"\<cr>\<esc>"
:norm @s
" ----------------------------------- part 2 -----------------------------------
" move to beginning of part 2 block
:norm 3j
" same trick from part 1, but regexen include the number words now
:let dw1 = ':s/\v.{-}(\d|one|two|three|four|five|six|seven|eight|nine).*/\1/'
:let dw2 = ':s/\v.*(\d|one|two|three|four|five|six|seven|eight|nine).*/\1+/'
:let @q = "yyp" . dw2 . "\<cr>k" . dw1 . "\<cr>gJj"
:norm 1000@q
" replace the words with digits
:%s/one/1/g
:%s/two/2/g
:%s/three/3/g
:%s/four/4/g
:%s/five/5/g
:%s/six/6/g
:%s/seven/7/g
:%s/eight/8/g
:%s/nine/9/g
" move to bottom, execute summing macro
:norm G@s
" ----------------------------------- output -----------------------------------
" delete empty lines
:g/^$/d
" write results to output file
:x! out