-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3_2.rb
61 lines (49 loc) · 1.23 KB
/
day3_2.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
file = File.open('day3.txt')
input = file.readlines.map do |line|
line.chomp.chars
end
file = File.open('day3.txt')
input_copy = file.readlines.map do |line|
line.chomp.chars
end
digit_number=0
while input_copy.length>1 do
transposed_input_copy = input_copy.transpose
digits=transposed_input_copy[digit_number]
count=digits.inject(0) {|sum, digit| sum+digit.to_i}
if count>=input_copy.length.to_f/2
digit_of_interest="1"
else
digit_of_interest="0"
end
(0...input_copy.length).each do |index|
if input_copy[index][digit_number] != digit_of_interest
input_copy[index]=nil
end
end
digit_number += 1
input_copy=input_copy.compact
end
oxygen_digits=input_copy[0]
digit_number=0
while input.length>1 do
transposed_input = input.transpose
digits=transposed_input[digit_number]
count=digits.inject(0) {|sum, digit| sum+digit.to_i}
if count>=input.length.to_f/2
digit_of_interest="0"
else
digit_of_interest="1"
end
(0...input.length).each do |index|
if input[index][digit_number] != digit_of_interest
input[index]=nil
end
end
digit_number += 1
input=input.compact
end
co2_digits=input[0]
oxygen=oxygen_digits.join.to_i(2)
co2=co2_digits.join.to_i(2)
puts(oxygen*co2)