Day nine
This commit is contained in:
parent
68238609db
commit
892538ce64
3 changed files with 127 additions and 0 deletions
51
9-1.rb
Executable file
51
9-1.rb
Executable file
|
@ -0,0 +1,51 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
problem = 9
|
||||
input = File.read("#{problem}.input")
|
||||
#input = DATA.read
|
||||
.chars
|
||||
.map(&:to_i)
|
||||
|
||||
expanded_disk = []
|
||||
|
||||
id = 0
|
||||
type = :file
|
||||
input.each do
|
||||
if type == :free
|
||||
_1.times { expanded_disk << "." }
|
||||
|
||||
type = :file
|
||||
else
|
||||
_1.times { expanded_disk << id }
|
||||
id += 1
|
||||
type = :free
|
||||
end
|
||||
end
|
||||
|
||||
defragmented_disk = Array.new(expanded_disk.size)
|
||||
|
||||
l, r = 0, expanded_disk.size - 1
|
||||
while l <= r
|
||||
if expanded_disk[l] != "."
|
||||
defragmented_disk[l] = expanded_disk[l]
|
||||
l += 1
|
||||
elsif expanded_disk[r] == "."
|
||||
defragmented_disk[r] = expanded_disk[r]
|
||||
r -= 1
|
||||
else
|
||||
defragmented_disk[l] = expanded_disk[r]
|
||||
defragmented_disk[r] = "."
|
||||
l += 1
|
||||
r -= 1
|
||||
end
|
||||
end
|
||||
|
||||
checksum = defragmented_disk
|
||||
.map.with_index { _1.to_i * _2.to_i }
|
||||
.sum
|
||||
|
||||
p checksum
|
||||
|
||||
|
||||
__END__
|
||||
2333133121414131402
|
75
9-2.rb
Executable file
75
9-2.rb
Executable file
|
@ -0,0 +1,75 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require "debug"
|
||||
|
||||
problem = 9
|
||||
input = File.read("#{problem}.input")
|
||||
#input = DATA.read
|
||||
.chars
|
||||
.map(&:to_i)
|
||||
|
||||
expanded_disk = []
|
||||
|
||||
id = 0
|
||||
type = :file
|
||||
input.each do
|
||||
if type == :free
|
||||
_1.times { expanded_disk << "." }
|
||||
|
||||
type = :file
|
||||
else
|
||||
_1.times { expanded_disk << id }
|
||||
id += 1
|
||||
type = :free
|
||||
end
|
||||
end
|
||||
|
||||
defragmented_disk = expanded_disk.dup
|
||||
|
||||
start = Time.now
|
||||
r = expanded_disk.size - 1
|
||||
while r > 0
|
||||
if expanded_disk[r] == "."
|
||||
r -= 1
|
||||
else
|
||||
rr = r
|
||||
rr -= 1 while expanded_disk[rr] == expanded_disk[r]
|
||||
space = r - rr
|
||||
|
||||
l = expanded_disk.index(".")
|
||||
while l < expanded_disk.size
|
||||
if expanded_disk[l] != "."
|
||||
l += 1
|
||||
next
|
||||
end
|
||||
|
||||
ll = l
|
||||
ll += 1 while defragmented_disk[ll] == "."
|
||||
free = ll - l
|
||||
|
||||
if l > r
|
||||
break
|
||||
elsif free >= space
|
||||
(l..(l + space - 1)).each { defragmented_disk[_1] = expanded_disk[r] }
|
||||
((rr + 1)..r).each { defragmented_disk[_1] = "." }
|
||||
break
|
||||
else
|
||||
l = ll + 1
|
||||
end
|
||||
end
|
||||
|
||||
r = rr
|
||||
end
|
||||
end
|
||||
finish = Time.now
|
||||
puts "Finished in #{finish - start}"
|
||||
|
||||
checksum = defragmented_disk
|
||||
.map.with_index { _1.to_i * _2.to_i }
|
||||
.sum
|
||||
|
||||
p checksum
|
||||
|
||||
|
||||
__END__
|
||||
2333133121414131402
|
1
9.input
Normal file
1
9.input
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue