123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package main
- import (
- "bufio"
- "fmt"
- "log"
- "os"
- "sort"
- "strings"
- )
- type item struct {
- name string
- size int
- }
- type dir struct {
- name string
- parent *dir
- dirs []*dir
- files []*item
- }
- func parentHasDir(parent *dir, name string) *dir {
- for i := range parent.dirs {
- if parent.dirs[i].name == name {
- return parent.dirs[i]
- }
- }
- return nil
- }
- func cd(line string, current *dir, root *dir) *dir {
- var name string
- n, err := fmt.Sscanf(line, "$ cd %s", &name)
- if n != 1 || err != nil {
- log.Fatal("Can't parse cd:", err)
- }
- if name == "/" {
- current = root
- } else if name == ".." {
- current = current.parent
- } else {
- parent := current
- current = parentHasDir(parent, name)
- if current == nil {
- newDir := dir{name: name, parent: parent}
- parent.dirs = append(parent.dirs, &newDir)
- current = &newDir
- }
- }
- return current
- }
- func readInput(file *os.File) dir {
- scanner := bufio.NewScanner(file)
- root := dir{name: "/"}
- var current *dir
- read := false
- for scanner.Scan() {
- line := scanner.Text()
- if line == "" {
- continue
- }
- if strings.HasPrefix(line, "$ cd") {
- read = false
- current = cd(line, current, &root)
- } else if strings.HasPrefix(line, "$ ls") {
- read = true
- continue
- }
- if read {
- if strings.HasPrefix(line, "dir ") {
- continue
- } else {
- var newFile item
- n, err := fmt.Sscanf(line, "%d %s", &newFile.size, &newFile.name)
- if n != 2 || err != nil {
- log.Fatal("Can't parse cd:", err)
- }
- current.files = append(current.files, &newFile)
- }
- }
- }
- return root
- }
- func getSizes(root dir, sizes []int) (int, []int) {
- size := 0
- for i := range root.files {
- size += root.files[i].size
- }
- for i := range root.dirs {
- var c int
- c, sizes = getSizes(*root.dirs[i], sizes)
- size += c
- }
- sizes = append(sizes, size)
- return size, sizes
- }
- func part1(sizes []int) int {
- sum := 0
- for i := range sizes {
- if sizes[i] < 100000 {
- sum += sizes[i]
- }
- }
- return sum
- }
- func part2(sizes []int, total int, fsSize int, needed int) int {
- unused := fsSize - total
- needed -= unused
- sort.Ints(sizes)
- for i := range sizes {
- if sizes[i] >= needed {
- return sizes[i]
- }
- }
- return 0
- }
- func main() {
- if len(os.Args) < 2 {
- log.Fatal("You need to specify a file!")
- }
- filePath := os.Args[1]
- file, err := os.Open(filePath)
- if err != nil {
- log.Fatalf("Failed to open %s!\n", filePath)
- }
- root := readInput(file)
- total, sizes := getSizes(root, []int{})
- fmt.Println("Part1:", part1(sizes))
- fmt.Println("Part2:", part2(sizes, total, 70000000, 30000000))
- }
|