[2022][ruby][7.x] refactoring

pull/1/head
Alpha Chen 2 years ago
parent fac81cb212
commit 1fa98d82d6
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -1,21 +1,19 @@
input = ARGF.read.lines(chomp: true).slice_when { _2.start_with?(?$) }
input = ARGF.read.lines(chomp: true)
Dir = Struct.new(:parent, :name, :children) do
include Enumerable
def size = children.sum(&:size)
def each(&block)
return enum_for(__method__, &block) unless block
def each
return enum_for(__method__) unless block_given?
block.(self)
yield self
children.each do |child|
case child
when Dir
child.each(&block)
child.each { yield _1 }
when File
block.(child)
yield child
else
fail child.inspect
end
@ -28,9 +26,8 @@ File = Struct.new(:parent, :name, :size)
root = Dir.new(nil, ?/, [])
pwd = root
input.each do |chunk|
cmd, *out = *chunk
case cmd
input.each do |line|
case line
when /\$ cd \//
# no-op
when /\$ cd \.\./
@ -38,19 +35,14 @@ input.each do |chunk|
when /\$ cd (.+)/
pwd = pwd.children.find { _1.name == $1 }
when /\$ ls/
out.each do |line|
case line
when /(\d+) (.+)/
pwd.children << File.new(pwd, $2, $1.to_i)
# no-op
when /dir (.+)/
pwd.children << Dir.new(pwd, $1, [])
when /(\d+) (.+)/
pwd.children << File.new(pwd, $2, $1.to_i)
else
fail line
end
end
else
fail chunk.inspect
end
end
# part 1

Loading…
Cancel
Save