Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2494333

Browse files
committed
day06
1 parent a4487f5 commit 2494333

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

2023/day06/.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.1.0

2023/day06/data_1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Time: 38 67 76 73
2+
Distance: 234 1027 1157 1236

2023/day06/data_exemple_1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Time: 7 15 30
2+
Distance: 9 40 200

2023/day06/main.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
string = File.read('data_1.txt')
5+
string = string.strip
6+
7+
def calcul(total_time, waiting_time)
8+
return 0 if total_time <= waiting_time
9+
10+
(total_time - waiting_time) * waiting_time
11+
end
12+
13+
def min_time_to_win(total_time, min_distance)
14+
result = (0..(total_time / 2)).bsearch do |waiting_time|
15+
calcul(total_time, waiting_time) > min_distance
16+
end
17+
(total_time / 2 - result) * 2 + (total_time.odd? ? 2 : 1)
18+
end
19+
20+
# stars1
21+
times = string[/Time: (.*)\n/, 1].split(' ').map(&:to_i)
22+
distances = string[/Distance: (.*)/, 1].split(' ').map(&:to_i)
23+
puts distances.map.with_index { min_time_to_win(times[_2], distances[_2]) }.reduce(:*)
24+
# stars2
25+
time = string[/Time: (.*)\n/, 1].gsub(' ', '').to_i
26+
distance = string[/Distance: (.*)/, 1].gsub(' ', '').to_i
27+
puts min_time_to_win(time, distance)

0 commit comments

Comments
 (0)