-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_6.rb
69 lines (51 loc) · 1.58 KB
/
day_6.rb
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
def set_up_counter(number_of_columns)
freq_counts = []
number_of_columns.times do
letter_counts = Hash[('a'..'z').to_a.map {|x| [x, 0]}]
letter_counts[:most_frequent] = {highest_count: 0, letters: []}
freq_counts << letter_counts
end
freq_counts
end
def process_input(input)
freq_counts = set_up_counter(input.first.length)
input.each do |row|
row.split('').each_with_index do |letter, i|
freq_counts[i][letter] += 1
if freq_counts[i][letter] > freq_counts[i][:most_frequent][:highest_count]
freq_counts[i][:most_frequent][:highest_count] = freq_counts[i][letter]
freq_counts[i][:most_frequent][:letters] = [letter]
elsif freq_counts[i][letter] == freq_counts[i][:most_frequent][:highest_count]
freq_counts[i][:most_frequent][:letters] << letter
end
end
end
freq_counts
end
def derive_correct_password(input)
freq_counts = process_input(input)
password = []
freq_counts.each {|counter| password << counter[:most_frequent][:letters]}
password.join
end
def read_file(filename)
File.read(filename).split("\n")
end
# Part A
# input = read_file('day_6_input.txt')
# puts derive_correct_password(input)
##################################
# Part B
def password_from_lowest_freq_letters(input)
freq_counts = process_input(input)
password = []
freq_counts.each do |counter|
counter.delete(:most_frequent)
values = counter.values
values.delete(0)
password << counter.key(values.min)
end
password.join
end
# input = read_file('day_6_input.txt')
# puts password_from_lowest_freq_letters(input)