-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from vaskoz/day17
Day 17 solution
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |