feat: fixed day3/1
This commit is contained in:
parent
a3f70ace6e
commit
b2054f12a8
2 changed files with 26 additions and 14 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -1,3 +1,6 @@
|
||||||
|
input.txt
|
||||||
|
|
||||||
|
|
||||||
# Created by https://www.toptal.com/developers/gitignore/api/pycharm+all,python
|
# Created by https://www.toptal.com/developers/gitignore/api/pycharm+all,python
|
||||||
# Edit at https://www.toptal.com/developers/gitignore?templates=pycharm+all,python
|
# Edit at https://www.toptal.com/developers/gitignore?templates=pycharm+all,python
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,18 @@ func print_number_with_surronding(number string, y int, end_x int, lines []strin
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func add_to_sum(number string, is_valid bool, sum_of_parts int) int {
|
||||||
|
if is_valid {
|
||||||
|
number, error := strconv.Atoi(number)
|
||||||
|
if error != nil {
|
||||||
|
fmt.Println("Error converting string to int")
|
||||||
|
}
|
||||||
|
|
||||||
|
sum_of_parts += number
|
||||||
|
}
|
||||||
|
return sum_of_parts
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
content, err := os.ReadFile("input.txt")
|
content, err := os.ReadFile("input.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -55,31 +67,28 @@ func main() {
|
||||||
|
|
||||||
for y, line := range lines {
|
for y, line := range lines {
|
||||||
current_number_string := ""
|
current_number_string := ""
|
||||||
current_number_is_vald := false
|
current_number_is_valid := false
|
||||||
for x, char := range line {
|
for x, char := range line {
|
||||||
_, err := strconv.Atoi(string(char))
|
_, err := strconv.Atoi(string(char))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if current_number_string != "" {
|
if current_number_string != "" {
|
||||||
number, error := strconv.Atoi(current_number_string)
|
sum_of_parts = add_to_sum(current_number_string, current_number_is_valid, sum_of_parts)
|
||||||
if error != nil {
|
|
||||||
fmt.Println("Error converting string to int")
|
|
||||||
}
|
|
||||||
|
|
||||||
if current_number_is_vald {
|
|
||||||
sum_of_parts += number
|
|
||||||
}
|
|
||||||
|
|
||||||
current_number_string = ""
|
current_number_string = ""
|
||||||
|
|
||||||
}
|
}
|
||||||
current_number_is_vald = false
|
current_number_is_valid = false
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
current_number_string += string(char)
|
current_number_string += string(char)
|
||||||
|
|
||||||
if !current_number_is_vald && is_adjacent_to_symbol(y, x, lines) {
|
if !current_number_is_valid && is_adjacent_to_symbol(y, x, lines) {
|
||||||
current_number_is_vald = true
|
current_number_is_valid = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if x == len(line) - 1 {
|
||||||
|
sum_of_parts = add_to_sum(current_number_string, current_number_is_valid, sum_of_parts)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue