Add day 6 solutions
This commit is contained in:
parent
07d8457cd8
commit
0aeb0349d7
17
day6.chpl
Normal file
17
day6.chpl
Normal file
|
@ -0,0 +1,17 @@
|
|||
use IO;
|
||||
|
||||
config const numChars = 4;
|
||||
|
||||
var theBytes: bytes;
|
||||
stdin.read(theBytes);
|
||||
|
||||
var indices: [0..<26] int = -numChars;
|
||||
|
||||
for (char, idx) in zip(theBytes.these() - b"a"[0], 0..) do {
|
||||
indices[char] = idx;
|
||||
|
||||
if + reduce (indices > idx - numChars) == numChars {
|
||||
writeln(idx + 1);
|
||||
break;
|
||||
}
|
||||
}
|
23
day6.cr
Normal file
23
day6.cr
Normal file
|
@ -0,0 +1,23 @@
|
|||
require "advent"
|
||||
INPUT = input(2022, 6).lines[0].chars
|
||||
|
||||
def part1(input)
|
||||
offset = 0
|
||||
loop do
|
||||
chars = input[offset..offset+3]
|
||||
return offset + 4 if chars.uniq.size == 4
|
||||
offset += 1
|
||||
end
|
||||
end
|
||||
|
||||
def part2(input)
|
||||
offset = 0
|
||||
loop do
|
||||
chars = input[offset..offset+13]
|
||||
return offset + 14 if chars.uniq.size == 14
|
||||
offset += 1
|
||||
end
|
||||
end
|
||||
|
||||
puts part1(INPUT.clone)
|
||||
puts part2(INPUT.clone)
|
Loading…
Reference in New Issue
Block a user