Compare commits
5 Commits
9848440f3f
...
29b20e27fc
Author | SHA1 | Date |
---|---|---|
magical | 29b20e27fc | |
magical | f4be6a5be3 | |
magical | e2a67c4b5c | |
magical | 0a590f5d36 | |
magical | d2d54b8a6e |
|
@ -0,0 +1,58 @@
|
|||
# run me with awk -f sol.awk < input
|
||||
|
||||
/\[/ {
|
||||
gsub("^ ", "[ ]")
|
||||
gsub(" {4}", " [ ]")
|
||||
gsub("^\\[|\\] \\[|\\]$", "")
|
||||
lines[nlines++] = $0
|
||||
if (nstacks < length())
|
||||
nstacks = length()
|
||||
}
|
||||
|
||||
/^$/ {
|
||||
for (i = 1; i <= nstacks; i++) {
|
||||
s = ""
|
||||
for (j = nlines-1; j >= 0; j--) {
|
||||
c = substr(lines[j], i, 1)
|
||||
if (c != " ") s = s c
|
||||
}
|
||||
stack[i] = s
|
||||
stack2[i] = s
|
||||
print s "."
|
||||
}
|
||||
print "----"
|
||||
}
|
||||
|
||||
function reverse(s) {
|
||||
r = ""
|
||||
for (i = length(s); i > 0; i--)
|
||||
r = r substr(s, i, 1)
|
||||
return r
|
||||
}
|
||||
|
||||
function move(S, n, fr, to, flip) {
|
||||
#print n, fr, to
|
||||
m = length(S[fr]) - n
|
||||
if (flip) chunk = reverse(substr(S[fr], m+1))
|
||||
else chunk = substr(S[fr], m+1)
|
||||
#print stack[to]
|
||||
#print stack[fr]
|
||||
S[to] = S[to] chunk
|
||||
S[fr] = substr(S[fr], 1, m)
|
||||
#print stack[to]
|
||||
#print stack[fr]
|
||||
}
|
||||
|
||||
/^move .* from .* to .*/ {
|
||||
move(stack, $2, $4, $6, 0)
|
||||
move(stack2, $2, $4, $6, 1)
|
||||
}
|
||||
|
||||
END {
|
||||
for (i = 1; i <= nstacks; i++) {
|
||||
p1 = p1 substr(stack[i], length(stack[i]))
|
||||
p2 = p2 substr(stack2[i], length(stack2[i]))
|
||||
}
|
||||
print p1
|
||||
print p2
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,23 @@
|
|||
$ cd /
|
||||
$ ls
|
||||
dir a
|
||||
14848514 b.txt
|
||||
8504156 c.dat
|
||||
dir d
|
||||
$ cd a
|
||||
$ ls
|
||||
dir e
|
||||
29116 f
|
||||
2557 g
|
||||
62596 h.lst
|
||||
$ cd e
|
||||
$ ls
|
||||
584 i
|
||||
$ cd ..
|
||||
$ cd ..
|
||||
$ cd d
|
||||
$ ls
|
||||
4060174 j
|
||||
8033020 d.log
|
||||
5626152 d.ext
|
||||
7214296 k
|
|
@ -0,0 +1,46 @@
|
|||
# run me with awk -f sol.awk < input
|
||||
|
||||
BEGIN { depth = 0 }
|
||||
|
||||
/^\$/ { cmd = $2 }
|
||||
|
||||
/^\$ cd \/$/ { depth = 0; next }
|
||||
/^\$ cd \.\.$/ { depth -= 1; next }
|
||||
/^\$ cd/ { cwd[depth++] = $3; next }
|
||||
/^\$ ls$/ { next }
|
||||
|
||||
function add(sz) {
|
||||
sizes["/"] += sz
|
||||
path = ""
|
||||
for (i = 0; i < depth; i++) {
|
||||
path = path "/" cwd[i]
|
||||
sizes[path] += sz
|
||||
}
|
||||
}
|
||||
|
||||
cmd == "ls" && $1 != "dir" {
|
||||
#print(dir "/" $2, $1)
|
||||
add($1)
|
||||
}
|
||||
|
||||
END {
|
||||
print sizes["/"]
|
||||
# for (i in sizes) { print i, sizes[i] }
|
||||
|
||||
t1 = 0
|
||||
for (i in sizes) {
|
||||
if (sizes[i] <= 100000) {
|
||||
t1 += sizes[i]
|
||||
}
|
||||
}
|
||||
print t1
|
||||
|
||||
t2 = 0
|
||||
for (i in sizes) {
|
||||
if (70000000 - sizes["/"] + sizes[i] >= 30000000) {
|
||||
if (sizes[i] < t2 || t2 == 0)
|
||||
t2 = sizes[i]
|
||||
}
|
||||
}
|
||||
print t2
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
import sys
|
||||
from collections import defaultdict
|
||||
cmds = open("input").read().split("$")
|
||||
|
||||
cwd = []
|
||||
sizes = defaultdict(lambda: 0)
|
||||
for cmd in cmds:
|
||||
cmd = cmd.strip()
|
||||
if not cmd:
|
||||
continue
|
||||
print(cmd)
|
||||
if cmd.startswith("cd "):
|
||||
dir = cmd.split()[1]
|
||||
if dir == "..":
|
||||
cwd.pop()
|
||||
elif dir == "/":
|
||||
cwd = []
|
||||
else:
|
||||
cwd.append(dir)
|
||||
elif cmd.startswith("ls\n"):
|
||||
files = cmd.splitlines()[1:]
|
||||
for file in files:
|
||||
sz, name = file.split()
|
||||
if sz == 'dir':
|
||||
pass
|
||||
else:
|
||||
print(cwd, name, sizes)
|
||||
for i in range(len(cwd)+1):
|
||||
sizes['/'.join(cwd[:i])] += int(sz)
|
||||
else:
|
||||
print("bad command:" +cmd)
|
||||
|
||||
|
||||
print(sizes)
|
||||
|
||||
print(sum(sz for sz in sizes.values() if sz <= 100000))
|
||||
|
||||
print(min(sz for sz in sizes.values() if 70000000 - sizes[''] + sz >= 30000000))
|
Loading…
Reference in New Issue