[2023][ruby][3.*] remove hack

main
Alpha Chen 12 months ago
parent fc5821b277
commit 8c72adad6d
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -1,3 +1,8 @@
Part = Data.define(:number) do
def eql?(other)
self.object_id == other.object_id
end
end
CurrentNumber = Data.define(:coords, :acc) CurrentNumber = Data.define(:coords, :acc)
cur_num = nil cur_num = nil
@ -15,7 +20,8 @@ ARGF.readlines(chomp: true).each.with_index do |row, y|
end end
else else
if cur_num if cur_num
nums.merge!(cur_num.coords.to_h { [_1, cur_num.acc.to_i] }) part = Part.new(cur_num.acc.to_i)
nums.merge!(cur_num.coords.to_h { [_1, part] })
cur_num = nil cur_num = nil
end end
@ -26,7 +32,8 @@ ARGF.readlines(chomp: true).each.with_index do |row, y|
end end
if cur_num if cur_num
nums.merge!(cur_num.coords.to_h { [_1, cur_num.acc.to_i] }) part = Part.new(cur_num.acc.to_i)
nums.merge!(cur_num.coords.to_h { [_1, part] })
cur_num = nil cur_num = nil
end end
end end
@ -39,9 +46,9 @@ p syms
dy.product(dx) dy.product(dx)
.map {|dy,dx| [y+dy,x+dx] } .map {|dy,dx| [y+dy,x+dx] }
.filter_map { nums.fetch(_1, nil) } .filter_map { nums.fetch(_1, nil) }
.uniq # lol, hack .uniq
} }
.sum .sum(&:number)
p syms p syms
.select { _2 == ?* } .select { _2 == ?* }
@ -51,7 +58,7 @@ p syms
parts = dy.product(dx) parts = dy.product(dx)
.map {|dy,dx| [y+dy,x+dx] } .map {|dy,dx| [y+dy,x+dx] }
.filter_map { nums.fetch(_1, nil) } .filter_map { nums.fetch(_1, nil) }
.uniq # lol, hack .uniq
if parts.length == 2 if parts.length == 2
parts parts
@ -59,4 +66,4 @@ p syms
nil nil
end end
} }
.sum { _1.inject(:*) } .sum { _1.map(&:number).inject(:*) }

Loading…
Cancel
Save