Skip to content

Commit 3c402c4

Browse files
author
Robb Kidd
committed
2022 day 6 part 1
1 parent 9c90039 commit 3c402c4

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

2022/ruby/day06.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Day06
2+
def self.go
3+
day = new
4+
puts "Part 1: #{day.part1}"
5+
puts "Part 2: #{day.part2}"
6+
end
7+
8+
def initialize(input=nil)
9+
@input = input || real_input
10+
end
11+
12+
def part1
13+
start_of_packet_marker(@input)
14+
end
15+
16+
def part2
17+
end
18+
19+
# @example
20+
# day.start_of_packet_marker('mjqjpqmgbljsphdztnvjfqwrcgsmlb') => 7
21+
#
22+
# @example
23+
# day.start_of_packet_marker('bvwbjplbgvbhsrlpgdmjqwftvncz') => 5
24+
#
25+
# @example
26+
# day.start_of_packet_marker('nppdvjthqldpwncqszvftbrmjlhg') => 6
27+
#
28+
# @example
29+
# day.start_of_packet_marker('nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg') => 10
30+
#
31+
# @example
32+
# day.start_of_packet_marker('zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw') => 11
33+
#
34+
def start_of_packet_marker(buffer)
35+
chars = buffer.tr("\n","").chars
36+
scan = 3
37+
found = false
38+
while !found || scan < chars.length do
39+
check = chars[scan-3..scan]
40+
if check.uniq.length == 4
41+
found = true
42+
break
43+
end
44+
scan += 1
45+
end
46+
return scan + 1
47+
end
48+
49+
def real_input
50+
File.read('../inputs/day06-input.txt')
51+
end
52+
53+
EXAMPLE_INPUT = <<~INPUT
54+
mjqjpqmgbljsphdztnvjfqwrcgsmlb
55+
INPUT
56+
end

0 commit comments

Comments
 (0)