2022-12-06 17:17:11 +10:30
|
|
|
{.hint[name]: off.} # snake_case > camelCase
|
|
|
|
import strutils
|
|
|
|
let line = strip read_file "input/6"
|
|
|
|
|
2022-12-06 23:02:25 +10:30
|
|
|
proc string_no_duplicate_chars(s: string): bool =
|
|
|
|
# 1M iterations: nim r -d:danger day6.nim 90.29s user 0.27s system 99% cpu 1:30.99 total
|
|
|
|
for i in 0..<s.len:
|
|
|
|
let c = s[i]
|
|
|
|
for j in (i+1)..<s.len:
|
|
|
|
if c == s[j]:
|
|
|
|
return false
|
|
|
|
return true
|
2022-12-06 17:17:11 +10:30
|
|
|
|
2022-12-06 23:02:25 +10:30
|
|
|
proc string_no_duplicate_chars_1iter(s: string): bool =
|
|
|
|
# 1M iterations: nim r -d:danger day6.nim 206.13s user 0.69s system 99% cpu 3:27.83 total
|
|
|
|
for i in 0..<s.len:
|
|
|
|
let c1 = s[i]
|
|
|
|
for c2 in s[i+1..^1]:
|
|
|
|
if c1==c2:
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
|
|
|
proc string_no_duplicate_chars_2iter(s: string): bool =
|
|
|
|
# 1M iterations: nim r -d:danger day6.nim 259.15s user 0.76s system 99% cpu 4:20.86 total
|
|
|
|
for i, c1 in s[0..^2]:
|
|
|
|
for c2 in s[i+1..^1]:
|
|
|
|
if c1==c2:
|
|
|
|
return false
|
|
|
|
return true
|
2022-12-06 17:17:11 +10:30
|
|
|
|
|
|
|
proc find_first(n: int, skip: int = 0): int =
|
2022-12-06 23:02:25 +10:30
|
|
|
for i in max(n, skip)..<line.len:
|
|
|
|
if string_no_duplicate_chars(line[i-n..<i]):
|
2022-12-06 17:17:11 +10:30
|
|
|
return i
|
|
|
|
return -1
|
|
|
|
|
2022-12-06 23:19:48 +10:30
|
|
|
proc string_no_duplicate_chars_copyless(s: string, i: int, n: int): bool =
|
|
|
|
# 1M iterations: nim r -d:danger day6.nim 22.70s user 0.13s system 97% cpu 23.412 total
|
|
|
|
let lower = i-n+1
|
|
|
|
for j in countdown(i, lower+1):
|
|
|
|
let c = s[j]
|
|
|
|
for k in countdown(j-1, lower):
|
|
|
|
if c == s[k]:
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
|
|
|
proc find_first_copyless(n: int, skip: int = 0): int =
|
|
|
|
for i in max(n, skip)..<line.len:
|
|
|
|
if string_no_duplicate_chars_copyless(line, i, n):
|
|
|
|
return i
|
|
|
|
return -1
|
|
|
|
|
2022-12-06 17:17:11 +10:30
|
|
|
var four = 0
|
|
|
|
var fourteen = 0
|
|
|
|
for i in 1..1000000:
|
2022-12-06 23:19:48 +10:30
|
|
|
four = find_first_copyless 4
|
|
|
|
fourteen = find_first_copyless(14, four)
|
2022-12-06 17:17:11 +10:30
|
|
|
echo four
|
|
|
|
echo fourteen
|