aboutsummaryrefslogtreecommitdiffstats
path: root/day07/solution.nim
blob: e6f4643d92cc6112882164462263e2a85f6eceae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import std/strutils

type
  Command = string
  # using LeFile for name because File already exists
  LeFile = ref object
    Size: int
    Name: string

type
  Directory = ref object
    Name: string
    Files: seq[LeFile]
    Parent: Directory
    Subdirectories: seq[Directory]

# Directory methods
method cd(self: Directory, path: string): Directory =
  if path == "..":
    return self.Parent

  for subdir in self.Subdirectories:
    if subdir.Name == path:
      return subdir

method add(self: Directory, file: LeFile): void =
  self.Files.add(file)

method add(self: Directory, dir: Directory): void =
  self.Subdirectories.add(dir)

let total = 70_000_000 
var part1 = 0
var minRemoved = total

method size(self: Directory, freeThreshold = total): int =
  var size = 0
  for file in self.Files:
    size += file.Size

  for subdir in self.Subdirectories:
    size += subdir.size(freeThreshold)

  if size <= 100_000:
    part1 += size

  if size >= freeThreshold:
    minRemoved = min(minRemoved, size)

  return size

proc run(root: Directory, input: seq[Command]): void = 
  # start pc from 1 to ignore "cd /" command thats coming first
  var pc = 1
  var current = root

  let endpc = input.len()
  while pc < endpc:
    let command = input[pc]
    assert(command[0] == '$')
    
    let tokens = command.splitWhitespace()

    case tokens[1]:
      of "cd":
        current = current.cd(tokens[2])
        pc += 1
      of "ls":
        pc += 1
        while pc < endpc:
          let file = input[pc]
          # end of file list
          if file[0] == '$':
            break
          
          let fileInfo = file.splitWhitespace()
          if fileInfo[0] == "dir":
            current.add(Directory(Name: fileInfo[1], Files: @[], Parent: current, Subdirectories: @[]))
          else:
            current.add(LeFile(Size: fileInfo[0].parseInt(), Name: fileInfo[1]))
          pc += 1
      else:
        assert(false)

  return

let content = readFile("./input.txt").strip().splitLines()

var root = Directory(Name: "/", Files: @[], Parent: nil, Subdirectories: @[])
run(root, content)

let totalUsed = root.size()
let totalFree = total - totalUsed
discard root.size(30_000_000 - totalFree)

echo part1
echo minRemoved