Compare commits

...

2 commits

Author SHA1 Message Date
19c189eee8 README: add note about day 3 crystal 2018-12-03 04:36:42 -03:00
1e25f502a6 add crystal solutions for day 3 2018-12-03 04:36:16 -03:00
4 changed files with 143 additions and 6 deletions

View file

@ -4,4 +4,4 @@ Advent of Code 2018, my solutions, any language
- day 1: elixir
- day 2: elixir
- day 3: python
- day 3: python (with a crystal port)

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