@@ -6,14 +6,15 @@ def self.go
6
6
end
7
7
8
8
def initialize ( input = nil )
9
- @input = input || real_input
9
+ @input = ( input || real_input ) . strip
10
10
end
11
11
12
12
def part1
13
13
start_of_packet_marker ( @input )
14
14
end
15
15
16
16
def part2
17
+ start_of_message_marker ( @input )
17
18
end
18
19
19
20
# @example
@@ -32,12 +33,35 @@ def part2
32
33
# day.start_of_packet_marker('zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw') => 11
33
34
#
34
35
def start_of_packet_marker ( buffer )
35
- chars = buffer . tr ( "\n " , "" ) . chars
36
- scan = 3
36
+ scan_for_length ( buffer , 4 )
37
+ end
38
+
39
+ # @example
40
+ # day.start_of_message_marker('mjqjpqmgbljsphdztnvjfqwrcgsmlb') => 19
41
+ #
42
+ # @example
43
+ # day.start_of_message_marker('bvwbjplbgvbhsrlpgdmjqwftvncz') => 23
44
+ #
45
+ # @example
46
+ # day.start_of_message_marker('nppdvjthqldpwncqszvftbrmjlhg') => 23
47
+ #
48
+ # @example
49
+ # day.start_of_message_marker('nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg') => 29
50
+ #
51
+ # @example
52
+ # day.start_of_message_marker('zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw') => 26
53
+ #
54
+ def start_of_message_marker ( buffer )
55
+ scan_for_length ( buffer , 14 )
56
+ end
57
+
58
+ def scan_for_length ( buffer , packet_length )
59
+ chars = buffer . chars
60
+ scan = lookback = packet_length - 1
37
61
found = false
38
62
while !found || scan < chars . length do
39
- check = chars [ scan -3 ..scan ]
40
- if check . uniq . length == 4
63
+ check = chars [ scan -lookback ..scan ]
64
+ if check . uniq . length == packet_length
41
65
found = true
42
66
break
43
67
end
0 commit comments