-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2022 day 6.bas
89 lines (77 loc) · 1.66 KB
/
2022 day 6.bas
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
!advent of code 2022 day 6
!x11-basic on android
day$="6"
fn$="Download/day"+day$+"input.txt"
sample1$="mjqjpqmgbljsphdztnvjfqwrcgsmlb"
sample1_res=7 !offset to last character of marker
sample3$="nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"
sample3_res=10
window_size=4
o = @walk_sample_string(sample3$)
if o = sample3_res
print "success with sample!"
else
print "failure with sample"
print "offset got was: ",o
print "should be: ",sample3_res
endif
cls
fh=freefile()
open "I",#fh,fn$
window$=input$(#fh,window_size)
print window$;
if not @check_string(window$)
marker_end=@walk_file(fh)
else
marker_end=4
endif
print
print "end of first marker at: ";marker_end
print "marker was: ";window$
!part 2 vvvvvvv
seek #fh,0
window_size=14
window$=input$(#fh,window_size)
print window$;
if not @check_string(window$)
marker_end=@walk_file(fh)
else
marker_end=4
endif
print
print "end of first marker at: ";marker_end
print "marker was: ";window$
end
function walk_file(handle)
while not eof(#handle)
nc$=input$(#handle,1)
print nc$;
window$=right$(window$,window_size-1)+nc$
if @check_string(window$)
return loc(#1)
endif
wend
print "end of file reached"
end
endfunction
function walk_sample_string(string_in$)
local start,end
end=len(string_in$)-window_size
for start=1 to end
window$=mid$(string_in$,start,window_size)
if @check_string(window$)
return start + window_size-1
else if start = end
return -1
endif
next
endfunction
function check_string(string_in$)
local i
for i = 1 to len(string_in$)
if tally(string_in$,mid$(string_in$,i,1)) > 1
return FALSE
endif
next
return TRUE
endfunction