Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
gcj round 1A
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien Bottazzini committed May 3, 2012
1 parent 94263c3 commit cb5d38a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 69 deletions.
14 changes: 0 additions & 14 deletions Google Code Jam/Google Code Jam 2012/1A/A watashi.rb

This file was deleted.

107 changes: 65 additions & 42 deletions Google Code Jam/Google Code Jam 2012/1A/B.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,70 @@ def debug text
STDERR.puts text
end

def doable_levels ll
levels = ll.select { |l| (l[0][0] <= @stars && l[0][1] == false) || l[1][0] <= @stars && l[1][1] == false}
return levels
end
def doable_levels?
@levels_list.each do |level|
star_1 = level[0]
if star_1[1] == false && star_1[0] <= @stars
return true
end

def find_2star_level_index ll
ll.each_with_index do |l, index|
if l[1][0] <= @stars && l[1][1] == false
return index
star_2 = level[1]
if star_2[1] == false && star_2[0] <= @stars
return true
end
end
return nil

return false
end

def find_1star_level_index ll
ll.each_with_index do |l, index|
if l[0][0] <= @stars && l[0][1] == false
def find_2_star_level_index

@levels_list.each_with_index do |level, index|
star_1 = level[0]
star_2 = level[1]

if star_2[1] == false &&
star_2[0] <= @stars &&
star_1[1] == false &&
star_1[0] <= @stars
return index
end
end

return nil
end

def complete_level index, star_mode
def star_1_level_or_highest_2star_index
indexes = Array.new
@levels_list.each_with_index do |l, i|
star_1 = l[0]
star_2 = l[1]

if star_mode == 1
@stars += 1
@levels_list[index][0][1] = true
else
first_star_status = @levels_list[index][0][1]
if first_star_status == true
@stars += 1
else
@stars += 2
if star_1[1] == true && star_2[1] == false && star_2[0] <= @stars
return i
end

@levels_list.delete_at(index)
if star_1[1] == false && star_1[0] <= @stars
indexes << i
elsif star_2[1] == false && star_2[0] <= @stars
indexes << i
end
end

@runs += 1
end

star_2_val = -1
return_index = -1
indexes.each do |i|
if @levels_list[i][1][0] > star_2_val
star_2_val = @levels_list[i][1][0]
return_index = i
end
end

return return_index
end

1.upto(gets.to_i) do |case_number|
@runs = 0

n = gets.to_i
@levels_list = Array.new
Expand All @@ -56,26 +75,30 @@ def complete_level index, star_mode
end

@stars = 0
possible_levels = doable_levels @levels_list

while possible_levels.size > 0
@runs = 0
while doable_levels?
star2_level_index = find_2_star_level_index

# if find 2 star level do it
level_index = find_2star_level_index(possible_levels)
if level_index
complete_level(level_index, 2)
else # else do 1 star level
level_index = find_1star_level_index(possible_levels)
complete_level(level_index, 1)
if star2_level_index
@levels_list[star2_level_index][0][1] = true
@levels_list[star2_level_index][1][1] = true
@stars = @stars + 2
else
star1_level_index = star_1_level_or_highest_2star_index
@stars = @stars + 1

if @levels_list[star1_level_index][0][1] == false
@levels_list[star1_level_index][0][1] = true
else
@levels_list[star1_level_index][1][1] = true
end
end

possible_levels = doable_levels @levels_list
@runs += 1
end

answer = 'Too Bad'

if doable_levels(@levels_list).size == 0 && @runs != 0
answer = @runs
answer = @runs
if @stars != 2*n
answer = 'Too Bad'
end

puts "Case ##{case_number}: #{answer}"
Expand Down
12 changes: 10 additions & 2 deletions Google Code Jam/Google Code Jam 2012/1A/Bsamples.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@
1 1
4 7
5 6
1
1 1
9
6 9
14 18
3 14
0 0
2 9
7 13
5 7
4 5
7 14
11 changes: 0 additions & 11 deletions Google Code Jam/Google Code Jam 2012/1A/C.rb

This file was deleted.

0 comments on commit cb5d38a

Please sign in to comment.