Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 17
Good morning! Here's your coding interview problem for today.

This problem was asked by Google.

Suppose we represent our file system by a string in the following manner:

The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents:
```
dir
    subdir1
    subdir2
        file.ext
```
The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.

The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" represents:
```
dir
    subdir1
        file1.ext
        subsubdir1
    subdir2
        subsubdir2
            file2.ext
```
The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext.

We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is "dir/subdir2/subsubdir2/file2.ext", and its length is 32 (not including the double quotes).

Given a string representing the file system in the above format, return the length of the longest absolute path to a file in the abstracted file system. If there is no file in the system, return 0.

Note:

The name of a file contains at least a period and an extension.

The name of a directory or sub-directory will not contain a period.

Pull request description:
Day 17 solution
Resolves #37 

Patch:
diff --git a/README.md b/README.md
index 6992c6d..efdb947 100644
--- a/README.md
+++ b/README.md
@@ -27,3 +27,4 @@ problems from
 * [Day 14](https://github.com/vaskoz/dailycodingproblem-go/issues/31)
 * [Day 15](https://github.com/vaskoz/dailycodingproblem-go/issues/33)
 * [Day 16](https://github.com/vaskoz/dailycodingproblem-go/issues/35)
+* [Day 17](https://github.com/vaskoz/dailycodingproblem-go/issues/37)
diff --git a/day17/problem.go b/day17/problem.go
new file mode 100644
index 0000000..7f31c24
--- /dev/null
+++ b/day17/problem.go
@@ -0,0 +1,39 @@
+package day17
+
+import (
+	"strings"
+)
+
+// FindLongestAbsolutePathLength returns the length of the longest file path.
+func FindLongestAbsolutePathLength(fs string) int {
+	var parts []string
+	var isFile bool
+	var partStart, tabCount, maxLength int
+	for i, r := range fs {
+		if isNewStart(i, r, fs) {
+			partStart = i
+		} else if r == '.' {
+			isFile = true
+		} else if r == '\t' {
+			tabCount++
+		} else if r == '\n' {
+			if pos := tabCount; len(parts) > pos {
+				parts = parts[:pos]
+			}
+			parts = append(parts, fs[partStart:i])
+			if len := len(strings.Join(parts, "/")); isFile && len > maxLength {
+				maxLength = len
+			}
+			isFile = false
+			tabCount = 0
+		}
+	}
+	if isFile {
+		parts = append(parts, fs[partStart:])
+	}
+	return len(strings.Join(parts, "/"))
+}
+
+func isNewStart(i int, r rune, fs string) bool {
+	return i-1 > 0 && fs[i-1] == '\t' && r != '\t' && r != '\n'
+}


Test patch:
diff --git a/day17/problem_test.go b/day17/problem_test.go
new file mode 100644
index 0000000..fd2f682
--- /dev/null
+++ b/day17/problem_test.go
@@ -0,0 +1,28 @@
+package day17
+
+import "testing"
+
+var testcases = []struct {
+	fs       string
+	expected int
+}{
+	{"dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext", 32},
+	{"root\n\tsubdir1\n\t\tfile1.ext", 22},
+}
+
+func TestFindLongestAbsolutePathLength(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := FindLongestAbsolutePathLength(tc.fs); result != tc.expected {
+			t.Errorf("Expected %v but got %v", tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkFindLongestAbsolutePathLength(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			FindLongestAbsolutePathLength(tc.fs)
+		}
+	}
+}