Day 7

Part 1

  • Read in input file
with open("day7/input.txt") as f:
    input = f.read().splitlines()

First 100 lines of input looks like this:

['$ cd /',
 '$ ls',
 '282959 btm',
 'dir fmfnpm',
 'dir gwlwp',
 'dir hchp',
 '275929 hmbbjbf',
 'dir nsphznf',
 'dir phschqg',
 '193293 rhpwvff',
 'dir spfwthmd',
 'dir wchdqb',
 'dir zlpmfh',
 '191479 zlpmfh.gpt',
 '$ cd fmfnpm',
 '$ ls',
 'dir fgtqvq',
 '194704 fwdvgnqp.fsm',
 '48823 fwdwq.tsq',
 '224991 mtjngt',
 '79386 rdsgpfjb.sfn',
 'dir rvnwwfq',
 'dir wrzcjwc',
 'dir zlpmfh',
 '$ cd fgtqvq',
 '$ ls',
 '293783 rjc.ncl',
 '324635 wdjrhw',
 '$ cd ..',
 '$ cd rvnwwfq',
 '$ ls',
 '76914 btm',
 '$ cd ..',
 '$ cd wrzcjwc',
 '$ ls',
 'dir fwdwq',
 '2159 fzb.tjs',
 'dir lddhdslh',
 'dir mjp',
 '284475 vclnlds',
 '196284 zjtftd',
 '$ cd fwdwq',
 '$ ls',
 '120795 jqnl.hzj',
 '$ cd ..',
 '$ cd lddhdslh',
 '$ ls',
 '293030 fzb',
 'dir gzj',
 '$ cd gzj',
 '$ ls',
 'dir qzgsswr',
 '$ cd qzgsswr',
 '$ ls',
 '33681 qzgsswr.wmv',
 '121649 sbjbw.shv',
 '$ cd ..',
 '$ cd ..',
 '$ cd ..',
 '$ cd mjp',
 '$ ls',
 '289491 btm',
 '169221 jqnl.hzj',
 '$ cd ..',
 '$ cd ..',
 '$ cd zlpmfh',
 '$ ls',
 '189296 ldgpvnh',
 '$ cd ..',
 '$ cd ..',
 '$ cd gwlwp',
 '$ ls',
 'dir dwcrnbj',
 'dir fmfnpm',
 'dir fwdwq',
 'dir hzpsts',
 'dir hzrq',
 'dir jzwpjtf',
 'dir lmmpmghg',
 'dir mnw',
 'dir qzgsswr',
 'dir zlpmfh',
 '$ cd dwcrnbj',
 '$ ls',
 '182989 btm',
 '145822 fwdvgnqp.fsm',
 'dir jbtfslcn',
 'dir lgbglc',
 '293584 mfstl.hhp',
 'dir sbffqq',
 'dir zhvn',
 '$ cd jbtfslcn',
 '$ ls',
 '195255 sbjbw.shv',
 '$ cd ..',
 '$ cd lgbglc',
 '$ ls',
 '261423 fmfnpm.rqh',
 '323530 fzb.lmm',
 '314800 hbl.blm']
with open("day7/sample.txt") as f:
    samples = f.read().splitlines()
  • Create a function that processes the commands

  • Test function on the sample data

sample_nodes = process_commands(samples)
sample_lnodes = fc.L(sample_nodes)
sample_tsize = sample_lnodes.attrgot("size").filter(lambda o: o < 100_000).sum()

print(f'the  answer for samples  is {sample_tsize}')
the  answer for samples  is 95437
  • Finally the total of all the directories sized below 100_000 is:
nodes = process_commands(input)
lnodes = fc.L(nodes)
tsize = lnodes.attrgot("size").filter(lambda o: o < 100_000).sum()

print(f'the correct answer for part 1 is {tsize}')
the correct answer for part 1 is 1491614

Part 2

  • Compute the free space (for the sample)
sample_free_space = 70_000_000 - sample_lnodes.attrgot("size")[0]
print(sample_free_space)
21618835
  • Compute the space need to delete (for the sample)
sample_need_to_delete = 30_000_000 - sample_free_space
print(sample_need_to_delete)
8381165
  • Find the smallest size of the directories sized greater than the space needed to be deleted
sample_delete_size = min(sample_lnodes.attrgot("size").filter(lambda o: o >= sample_need_to_delete))
print(sample_delete_size)
24933642
  • For the answer to part 2, run the same calculations
free_space = 70_000_000 - lnodes.attrgot("size")[0]
print(free_space)
23909866
need_to_delete = 30_000_000 - free_space
print(need_to_delete)
6090134
delete_size = min(lnodes.attrgot("size").filter(lambda o: o >= need_to_delete))

print(f'the correct answer for part 2 is {delete_size}')
the correct answer for part 2 is 6400111