Author: Nicholas Prado <nmprado@nzen.ws>
Date: Wed Dec 8 06:22:52 UTC 2021
Parent: e3b519917a2c1ce63c31b677d6db37ea09f2be76
Log message:
feat 21 02.1 golang and template As if I don't have enough to do, this is an attempt with golang.
1: diff --git a/src/go/Template.go b/src/go/Template.go 2: new file mode 100644 3: index 0000000..0e9ec2e 4: --- /dev/null 5: +++ b/src/go/Template.go 6: @@ -0,0 +1,67 @@ 7: +package main 8: + 9: +import ( 10: + "bufio" 11: + "fmt" 12: + "log" 13: + "os" 14: +) 15: + 16: +func main() { 17: + if len( os.Args ) < 2 { 18: + log.Fatal( "Error: need filepath argument" ) 19: + } 20: + filePath := os.Args[ 1 ] 21: + fileLines := readLines( filePath ) 22: + 23: + xxx( fileLines ) 24: +} 25: + 26: +func readLines( filepath string ) []string { 27: + file, err := os.Open(filepath) 28: + if err != nil { 29: + log.Fatal(err) 30: + } 31: + 32: + defer file.Close() 33: + 34: + var lines []string 35: + 36: + scanner := bufio.NewScanner(file) 37: + for scanner.Scan() { 38: + lines = append(lines, scanner.Text()) 39: + } 40: + 41: + if scanner.Err() != nil { 42: + log.Fatal(scanner.Err()) 43: + } 44: + return lines 45: +} 46: + 47: +func xxx( fileLines []string ) { 48: + for _, line := range fileLines { 49: + if len( line ) < 1 { 50: + continue 51: + } 52: + fmt.Println( line ) 53: + } 54: + 55: + fmt.Println( "\tresult" ) 56: +} 57: + 58: + 59: + 60: + 61: + 62: + 63: + 64: + 65: + 66: + 67: + 68: + 69: + 70: + 71: + 72: + 73: + 74: diff --git a/src/go/y2021/Exercise0701.go b/src/go/y2021/Exercise0701.go 75: new file mode 100644 76: index 0000000..874684e 77: --- /dev/null 78: +++ b/src/go/y2021/Exercise0701.go 79: @@ -0,0 +1,87 @@ 80: +package main 81: + 82: +import ( 83: + "bufio" 84: + "fmt" 85: + "log" 86: + "os" 87: + "strconv" 88: + "strings" 89: +) 90: + 91: +func main() { 92: + if len( os.Args ) < 2 { 93: + log.Fatal( "Error: need filepath argument" ) 94: + } 95: + filePath := os.Args[ 1 ] 96: + fileLines := readLines( filePath ) 97: + 98: + calculateDepth( fileLines ) 99: +} 100: + 101: +func readLines( filepath string ) []string { 102: + file, err := os.Open(filepath) 103: + if err != nil { 104: + log.Fatal(err) 105: + } 106: + 107: + defer file.Close() 108: + 109: + var lines []string 110: + 111: + scanner := bufio.NewScanner(file) 112: + for scanner.Scan() { 113: + lines = append(lines, scanner.Text()) 114: + } 115: + 116: + if scanner.Err() != nil { 117: + log.Fatal(scanner.Err()) 118: + } 119: + return lines 120: +} 121: + 122: +func calculateDepth( fileLines []string ) { 123: + horizontalPosition := 0 124: + depth := 0 125: + dirInd := 0 126: + valInd := dirInd +1 127: + for _, line := range fileLines { 128: + if len( line ) < 1 { 129: + continue 130: + } 131: + 132: + direction_magnitude := strings.Split( line, " " ) 133: + magnitude, err := strconv.Atoi( direction_magnitude[ valInd ] ) 134: + if err != nil { 135: + log.Fatal( "not an int ", direction_magnitude[ valInd ] ) // very paranoid, le sigh 136: + } 137: + switch direction_magnitude[ dirInd ] { 138: + case "up" : 139: + depth -= magnitude 140: + case "down" : 141: + depth += magnitude 142: + // fallthrough if I want that behavior 143: + default : // forward 144: + horizontalPosition += magnitude 145: + } 146: + } 147: + 148: + fmt.Println( "\tresult ", ( horizontalPosition * depth ) ) 149: +} 150: + 151: + 152: + 153: + 154: + 155: + 156: + 157: + 158: + 159: + 160: + 161: + 162: + 163: + 164: + 165: + 166: +