add crystal solutions for day 3

This commit is contained in:
Luna 2018-12-03 04:36:16 -03:00
parent f8d8b7fd78
commit 1e25f502a6
3 changed files with 142 additions and 5 deletions

67
day3/day3_1.cr Normal file
View File

@ -0,0 +1,67 @@
lines = [] of String
class Claim
def initialize(@cid : String, @p1 : UInt64, @p2 : UInt64,
@width : UInt64, @height : UInt64)
end
end
cloth = [] of Array(Array(Claim))
claims = [] of Claim
# initialize cloth
(0...1000).each do |i|
cloth_line = [] of Array(Claim)
(0...1000).each do |j|
# cloth_line << Claim.new("-1", 0, 0, 0, 0)
cloth_line << [] of Claim
end
cloth << cloth_line
end
while true
line = gets '\n'
if line.nil?
break
end
lines << line.strip().gsub(" ", "")
end
lines.each do |line|
cid_p, claim_data = line.split('@')
p1p2, wh = claim_data.split(':')
p1, p2 = p1p2.split(',')
width, height = wh.split('x')
cid = cid_p.gsub("#", "")
claims << Claim.new(cid, p1.to_u64, p2.to_u64, width.to_u64, height.to_u64)
end
claims.each do |claim|
(claim.@p1...claim.@p1 + claim.@width).each do |x|
(claim.@p2...claim.@p2 + claim.@height).each do |y|
cloth[x][y] << claim
end
end
end
# check the cloth
overlaps = 0
(0...1000).each do |i|
(0...1000).each do |j|
claims = cloth[i][j]
if claims.size > 1
overlaps += 1
end
end
end
puts overlaps

View File

@ -32,20 +32,15 @@ def main():
c = Claim(cid, int(p1), int(p2), int(w), int(h))
claims.append(c)
pprint.pprint(claims)
print('running claims')
for claim in claims:
print(claim)
for i in range(claim.p1, claim.p1 + claim.width):
for j in range(claim.p2, claim.p2 + claim.height):
print('claiming', i, j, claim.cid)
if cloth[i][j] is None:
cloth[i][j] = []
cloth[i][j].append(claim)
print('result: ', cloth[i][j])
overlap = 0

75
day3/day3_2.cr Normal file
View File

@ -0,0 +1,75 @@
lines = [] of String
class Claim
def initialize(@cid : String, @p1 : UInt64, @p2 : UInt64,
@width : UInt64, @height : UInt64)
end
end
cloth = [] of Array(Array(Claim))
claims = [] of Claim
# initialize cloth
(0...1000).each do |i|
cloth_line = [] of Array(Claim)
(0...1000).each do |j|
# cloth_line << Claim.new("-1", 0, 0, 0, 0)
cloth_line << [] of Claim
end
cloth << cloth_line
end
while true
line = gets '\n'
if line.nil?
break
end
lines << line.strip().gsub(" ", "")
end
lines.each do |line|
cid_p, claim_data = line.split('@')
p1p2, wh = claim_data.split(':')
p1, p2 = p1p2.split(',')
width, height = wh.split('x')
cid = cid_p.gsub("#", "")
claims << Claim.new(cid, p1.to_u64, p2.to_u64, width.to_u64, height.to_u64)
end
claims.each do |claim|
(claim.@p1...claim.@p1 + claim.@width).each do |x|
(claim.@p2...claim.@p2 + claim.@height).each do |y|
cloth[x][y] << claim
end
end
end
# check the cloth
overlaps = Set.new [] of Claim
claims_set = claims.to_set
(0...1000).each do |i|
(0...1000).each do |j|
claims = cloth[i][j]
if claims.size > 1
claims.each do |claim|
overlaps << claim
end
end
end
end
puts claims_set.size
puts overlaps.size
non_overlap = claims_set - overlaps
puts non_overlap