-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.rb
43 lines (31 loc) · 899 Bytes
/
day6.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
#!/usr/bin/env ruby
# Day 1 2016
# See http://adventofcode.com/2016/day/6
puts "Advent of Code 2016 day 6"
#Part 1 - unjam the signal
char_hashes = []
File.open('day6.data').each do |line|
continue if(line.nil?)
i = 0
line.each_char do |c|
if(char_hashes[i].nil?)
#initialize new character counter
char_hashes[i] = {}
end
if(!char_hashes[i].key?(c))
char_hashes[i][c] = 1
else
char_hashes[i][c] = char_hashes[i][c] + 1
end
i += 1
end
end
#sort by count (desc), then alphabetical (asc), then take the first 5 keys and make a checksum string
top_chars = []
least_chars = []
char_hashes.each_with_index do |h, i|
top_chars[i] = h.sort_by { |letter, count| -count}.to_h.keys.first
least_chars[i] = h.sort_by { |letter, count| count}.to_h.keys.first
end
puts "part1 #{top_chars.join('')}"
puts "part2 #{least_chars.join('')}"