-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.vim
46 lines (39 loc) · 1.07 KB
/
day03.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
read day03_input
function! ColCount(col, char)
redir => c
execute "silent %s/\\%" .. a:col .. "c" .. a:char .. "//ne"
redir END
return str2nr(matchstr(c, '\d\+'))
endfunction
" Part 1
let gamma = ""
let epsilon = ""
for i in range(1, 12)
let zerocount = ColCount(i, "0")
let onecount = ColCount(i, "1")
let gamma .= (zerocount < onecount) ? "1" : "0"
let epsilon .= (zerocount > onecount) ? "1" : "0"
endfor
call Print("part 1: " .. str2nr(gamma, 2) * str2nr(epsilon, 2))
" Part 2
let i = 1
while line('$') > 1
let zerocount = ColCount(i, "0")
let onecount = ColCount(i, "1")
let char = (zerocount <= onecount) ? "1" : "0"
execute "%v/\\%" .. i .. "c" .. char .. "/d"
let i += 1
endwhile
let o = getline(1)
%delete
read day03_input
let i = 1
while line('$') > 1
let zerocount = ColCount(i, "0")
let onecount = ColCount(i, "1")
let char = (zerocount <= onecount) ? "0" : "1"
execute "%v/\\%" .. i .. "c" .. char .. "/d"
let i += 1
endwhile
let co2 = getline(1)
call Print("part 2: " .. str2nr(o, 2) * str2nr(co2, 2))