day 7 simplify awk solution
inspired by elly's fennel solution
This commit is contained in:
		
							parent
							
								
									e2a67c4b5c
								
							
						
					
					
						commit
						f4be6a5be3
					
				@ -4,21 +4,10 @@ BEGIN { depth = 0 }
 | 
			
		||||
 | 
			
		||||
/^\$/ { cmd = $2 }
 | 
			
		||||
 | 
			
		||||
/^\$ cd/ {
 | 
			
		||||
    if ($3 == "/") {
 | 
			
		||||
        depth = 0
 | 
			
		||||
    } else if ($3 == "..") {
 | 
			
		||||
        depth -= 1
 | 
			
		||||
    } else {
 | 
			
		||||
        cwd[depth++] = $3
 | 
			
		||||
    }
 | 
			
		||||
    #print dir
 | 
			
		||||
    next
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/^\$ ls$/ {
 | 
			
		||||
    next
 | 
			
		||||
}
 | 
			
		||||
/^\$ cd \/$/    { depth = 0; next }
 | 
			
		||||
/^\$ cd \.\.$/  { depth -= 1; next }
 | 
			
		||||
/^\$ cd/        { cwd[depth++] = $3; next }
 | 
			
		||||
/^\$ ls$/       { next }
 | 
			
		||||
 | 
			
		||||
function add(sz) {
 | 
			
		||||
    sizes["/"] += sz
 | 
			
		||||
@ -29,11 +18,9 @@ function add(sz) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
cmd == "ls" {
 | 
			
		||||
    if ($1 != "dir") {
 | 
			
		||||
        #print(dir "/" $2, $1)
 | 
			
		||||
        add($1)
 | 
			
		||||
    }
 | 
			
		||||
cmd == "ls" && $1 != "dir" {
 | 
			
		||||
    #print(dir "/" $2, $1)
 | 
			
		||||
    add($1)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
END {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user