-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.vim
62 lines (54 loc) · 1.5 KB
/
day04.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
read day04_input
" Part 1
let g:won = v:false
let g:wonlines = {}
function! Win(call)
call setpos('.', g:winpos)
normal! {jV5j"ay
exe "let sum = " .. substitute(substitute(trim(@a), "[ \n]", "+", "g"), "\\V++", "+", "g")
normal! {
let g:won = v:true
if !has_key(g:wonlines, line('.'))
let g:wonlines[line('.')] = v:true
let g:last_win_sum = sum
let g:last_win_call = str2nr(a:call)
endif
endfunction
function! Check(call)
let pos = getpos('.')
normal! r0lr0h
if match(getline('.'), "\\v^(\\_s*00\\_s*){5}") != -1
let g:winpos = pos
call Win(a:call)
return "00"
endif
normal! {j
exe "silent! normal! " .. pos[2] .. "|\<c-v>l4j\"ay"
let col = substitute(@a, "\\n", "", "g")
if match(col, "0000000000") != -1
let g:winpos = pos
call Win(a:call)
return "00"
endif
call setpos('.', pos)
return "00"
endfunction
let calls = split(getline(1), ",")
for call in calls
if len(call) == 1 | let call = " " .. call | endif
exe "3,$s/\\v" .. call .. ">/\\=Check(" .. trim(call) .. ")/g"
if g:won
call Print("part 1: " .. g:last_win_call * g:last_win_sum)
break
endif
endfor
" Part 2
%delete
read day04_input
let calls = split(getline(1), ",")
let g:wonlines = {}
for call in calls
if len(call) == 1 | let call = " " .. call | endif
exe "3,$s/\\v" .. call .. ">/\\=Check(" .. trim(call) .. ")/ge"
endfor
call Print("part 2: " .. g:last_win_call * g:last_win_sum)