-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_1.go
80 lines (65 loc) · 1.71 KB
/
part_1.go
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
package main
import (
"bufio"
"os"
"strconv"
"unicode"
)
func SumCalibration() (int, error) {
// First read the input file
file, err := os.Open("input.txt")
if err != nil {
return 0, err
}
defer file.Close()
// if the file open successfully then we will scan the things inside the file
scanner := bufio.NewScanner(file)
var sumCalibration int = 0
for scanner.Scan() {
var lines string = scanner.Text()
// Now lines hold the each and every line so let's select the line from each and every one
firstDigit, foundFirst := findFirstDigit(lines)
lastDigit, foundLast := findLastDigit(lines)
// Means Both we find and return true then
if foundFirst && foundLast {
calibrationValue := (firstDigit * 10) + lastDigit
sumCalibration += calibrationValue
}
}
// Now We Return the final Sum
// Now the Answer should be 142 as shown in example
return sumCalibration, nil
}
/*
Now Lets break the code like first lets find the first element digit
*/
func findFirstDigit(s string) (int, bool) {
// we will iterate though the string to scan for any available digits
for i := 0; i < len(s); i++ {
var char rune = rune(s[i])
if unicode.IsDigit(char) {
digit, err := strconv.Atoi(string(char))
if err != nil {
return 0, false
}
return digit, true
}
}
return 0, false
}
// Lets Find the last digit inside the String
func findLastDigit(s string) (int, bool) {
// Running the for loop from back of the string line
// so we will select the last element easily
for i := len(s) - 1; i >= 0; i-- {
var char rune = rune(s[i])
if unicode.IsDigit(char) {
digit, err := strconv.Atoi(string(char))
if err != nil {
return 0, false
}
return digit, true
}
}
return 0, false
}