feat: fixed day3/1
This commit is contained in:
parent
a3f70ace6e
commit
b2054f12a8
2 changed files with 26 additions and 14 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
|
@ -1,3 +1,6 @@
|
|||
input.txt
|
||||
|
||||
|
||||
# Created by https://www.toptal.com/developers/gitignore/api/pycharm+all,python
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=pycharm+all,python
|
||||
|
||||
|
|
@ -256,4 +259,4 @@ cython_debug/
|
|||
poetry.toml
|
||||
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/pycharm+all,python
|
||||
# End of https://www.toptal.com/developers/gitignore/api/pycharm+all,python
|
||||
|
|
|
|||
|
|
@ -44,6 +44,18 @@ func print_number_with_surronding(number string, y int, end_x int, lines []strin
|
|||
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() {
|
||||
content, err := os.ReadFile("input.txt")
|
||||
if err != nil {
|
||||
|
|
@ -55,31 +67,28 @@ func main() {
|
|||
|
||||
for y, line := range lines {
|
||||
current_number_string := ""
|
||||
current_number_is_vald := false
|
||||
current_number_is_valid := false
|
||||
for x, char := range line {
|
||||
_, err := strconv.Atoi(string(char))
|
||||
if err != nil {
|
||||
if current_number_string != "" {
|
||||
number, error := strconv.Atoi(current_number_string)
|
||||
if error != nil {
|
||||
fmt.Println("Error converting string to int")
|
||||
}
|
||||
|
||||
if current_number_is_vald {
|
||||
sum_of_parts += number
|
||||
}
|
||||
|
||||
sum_of_parts = add_to_sum(current_number_string, current_number_is_valid, sum_of_parts)
|
||||
current_number_string = ""
|
||||
|
||||
}
|
||||
current_number_is_vald = false
|
||||
current_number_is_valid = false
|
||||
continue
|
||||
}
|
||||
current_number_string += string(char)
|
||||
|
||||
if !current_number_is_vald && is_adjacent_to_symbol(y, x, lines) {
|
||||
current_number_is_vald = true
|
||||
if !current_number_is_valid && is_adjacent_to_symbol(y, x, lines) {
|
||||
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