|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +def parse(data): |
| 5 | + return [int(x) for x in data] |
| 6 | + |
| 7 | +split_data = parse |
| 8 | +completed = True |
| 9 | +raw_data = None # Not To be touched |
| 10 | + |
| 11 | +def part1(data): |
| 12 | + fileSystem = [None] * sum(data) |
| 13 | + |
| 14 | + # Populate the fileSystem |
| 15 | + i = 0 |
| 16 | + for id, block in enumerate(data): |
| 17 | + if id % 2 == 0: # Every odd one is block |
| 18 | + fileSystem[i:i+block] = [id // 2]*block # Simple ID calculation |
| 19 | + i += block |
| 20 | + |
| 21 | + # Printing the fileSystem |
| 22 | + # print(''.join(str(x) if x is not None else '.' for x in fileSystem)) |
| 23 | + |
| 24 | + # Run the compacting proccess |
| 25 | + left = 0 |
| 26 | + right = len(fileSystem) - 1 |
| 27 | + |
| 28 | + while left < right: |
| 29 | + if fileSystem[left] is None and fileSystem[right] is None: |
| 30 | + right -= 1 |
| 31 | + elif fileSystem[left] is None and fileSystem[right] is not None: |
| 32 | + fileSystem[left] = fileSystem[right] |
| 33 | + fileSystem[right] = None |
| 34 | + # These two lines are unncecary but still help in optimization |
| 35 | + right -= 1 |
| 36 | + left += 1 |
| 37 | + elif fileSystem[left] is not None: |
| 38 | + left += 1 |
| 39 | + # print(''.join(str(x) if x is not None else '.' for x in fileSystem)) |
| 40 | + |
| 41 | + return sum(index * id for index, id in enumerate(fileSystem[:left])) |
| 42 | + |
| 43 | +def part2(data): |
| 44 | + # Instead of using a ram array. We should instead just keep an array of file blocks |
| 45 | + |
| 46 | + files = [] |
| 47 | + for id, block in enumerate(data): |
| 48 | + files.append({ |
| 49 | + 'type': 'file' if id % 2 == 0 else 'free', |
| 50 | + 'size': block, |
| 51 | + 'id': id // 2 if id % 2 == 0 else '.' # For free blocks, we simply ignore this "id" |
| 52 | + }) |
| 53 | + |
| 54 | + # print(len(files)) |
| 55 | + |
| 56 | + for fi in range(len(files) - 1, -1, -1): |
| 57 | + file = files[fi] |
| 58 | + if file['type'] == 'free': continue |
| 59 | + |
| 60 | + for bi in range(len(files)): |
| 61 | + block = files[bi] |
| 62 | + if block['type'] == 'file' and block['id'] == file['id']: break # Only check the left side |
| 63 | + if block['type'] == 'file': continue # We don't care about files |
| 64 | + if block['size'] < file['size']: continue # This free space is too small |
| 65 | + |
| 66 | + # We are looking at a block which can fit the file... |
| 67 | + block['size'] -= file['size'] |
| 68 | + files.insert(bi, files.pop(fi)) # Reposition |
| 69 | + files.insert(fi, { |
| 70 | + 'type': 'free', |
| 71 | + 'size': file['size'], |
| 72 | + 'id': '.' |
| 73 | + }) # Adding a free space in it's place |
| 74 | + break |
| 75 | + |
| 76 | + cum = 0 |
| 77 | + index = 0 |
| 78 | + for f in files: |
| 79 | + if f['type'] == 'file': |
| 80 | + for i in range(f['size']): |
| 81 | + cum += (i+index) * f['id'] |
| 82 | + index += f['size'] |
| 83 | + |
| 84 | + return cum |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
0 commit comments