-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday03.rb
More file actions
109 lines (94 loc) · 3.49 KB
/
day03.rb
File metadata and controls
109 lines (94 loc) · 3.49 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# frozen_string_literal: true
DigitCounts = Struct.new(:one_counts, :zero_counts)
DIAGNOSTIC_LENGTH = File.open('../inputs/2021/day03-input-01.txt', &:readline).chomp().length
diagnostics = File.readlines('../inputs/2021/day03-input-01.txt').map { |reading| reading.to_i(2) }
# rubocop:disable Metrics/MethodLength
def count_digits(input_diagnostics_array)
digit_counts = DigitCounts.new(Array.new(DIAGNOSTIC_LENGTH, 0), Array.new(DIAGNOSTIC_LENGTH, 0))
(0...DIAGNOSTIC_LENGTH).each do |i|
input_diagnostics_array.each do |diagnostic|
if ((diagnostic >> i) & 1) == 1
digit_counts.one_counts[i] += 1
else
digit_counts.zero_counts[i] += 1
end
end
end
digit_counts
end
# rubocop:enable Metrics/MethodLength
##########
# PART 1 #
##########
gamma_str = epsilon_str = ''
digit_counts = count_digits(diagnostics)
(0...DIAGNOSTIC_LENGTH).each do |i|
if digit_counts.one_counts[i] > digit_counts.zero_counts[i]
gamma_str = "1#{gamma_str}"
epsilon_str = "0#{epsilon_str}"
else
gamma_str = "0#{gamma_str}"
epsilon_str = "1#{epsilon_str}"
end
end
puts "the power consumption of the submarine is #{gamma_str.to_i(2) * epsilon_str.to_i(2)}"
##########
# PART 2 #
##########
# counts = digit_counts
# oxygen_candidates = diagnostics
# (0...DIAGNOSTIC_LENGTH).each do |i|
# if counts.one_counts[i] >= counts.zero_counts[i]
# oxygen_candidates = oxygen_candidates.select do |diagnostic|
# diagnostic & (((2**(DIAGNOSTIC_LENGTH - i)) - 1)) >= (2**(DIAGNOSTIC_LENGTH - (i + 1)))
# end
# else
# oxygen_candidates = oxygen_candidates.select do |diagnostic|
# diagnostic & (((2**(DIAGNOSTIC_LENGTH - i)) - 1)) < (2**(DIAGNOSTIC_LENGTH - (i + 1)))
# end
# end
# break if oxygen_candidates.length == 1
# counts = count_digits(oxygen_candidates)
# end
# counts = digit_counts
# scrubber_candidates = diagnostics
# (0...DIAGNOSTIC_LENGTH).each do |i|
# if counts.one_counts[i] < counts.zero_counts[i]
# scrubber_candidates = scrubber_candidates.select do |diagnostic|
# diagnostic & (((2**(DIAGNOSTIC_LENGTH - i)) - 1)) >= (2**(DIAGNOSTIC_LENGTH - (i + 1)))
# end
# else
# scrubber_candidates = scrubber_candidates.select do |diagnostic|
# diagnostic & (((2**(DIAGNOSTIC_LENGTH - i)) - 1)) < (2**(DIAGNOSTIC_LENGTH - (i + 1)))
# end
# end
# break if scrubber_candidates.length == 1
# counts = count_digits(scrubber_candidates)
# end
# puts "oxygen (#{oxygen_candidates[0]}) * oxygen (#{scrubber_candidates[0]}) = #{oxygen_candidates[0] * scrubber_candidates[0]}"
# Let me try brute force in text :-(
diagnostic_strings = File.readlines('../inputs/2021/day03-input-01.txt')
oxygen_strings = scrubber_strings = diagnostic_strings
(0...12).each do |i|
if oxygen_strings.length > 1
ones_count = oxygen_strings.count { |d| d[i] == '1' }
zeros_count = oxygen_strings.count { |d| d[i] == '0' }
if ones_count >= zeros_count
oxygen_strings = oxygen_strings.select { |d| d[i] == '1' }
else
oxygen_strings = oxygen_strings.select { |d| d[i] == '0' }
end
end
if scrubber_strings.length > 1
ones_count = scrubber_strings.count { |d| d[i] == '1' }
zeros_count = scrubber_strings.count { |d| d[i] == '0' }
if ones_count < zeros_count
scrubber_strings = scrubber_strings.select { |d| d[i] == '1' }
else
scrubber_strings = scrubber_strings.select { |d| d[i] == '0' }
end
end
end
puts oxygen_strings
puts scrubber_strings
puts oxygen_strings[0].to_i(2) * scrubber_strings[0].to_i(2)