₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,623 members, 8,422,898 topics. Date: Tuesday, 09 June 2026 at 01:23 AM

Toggle theme

[AOC December 2023] - Programming - Nairaland

Nairaland ForumScience/TechnologyProgramming[AOC December 2023] (488 Views)

1 Reply (Go Down)

[AOC December 2023] by Deicide(op):
I would be drooping my solution here, which would mostly be written in Golang (because I am learning the language) and Rust. Let's see how far I can go before Santa gets me smiley
Re: [AOC December 2023] by Deicide(op):
Day 1: Trebuchet?!

Writing the solution of this one in Go cool
part 1
func Trebuchet(input string) int {
data, err := utils.ReadFile(input)
if err != nil {
fmt.Println(err)
}
sum := 0
v1 := strings.Split(data, "\n" )
for _, y := range v1 {
left := 0
right := len(y) - 1
ptr := 0
a := 0
b := 0
for ptr <= len(y) {
if y[left] >= 48 && y[left] <= 57 {
a = int(y[left] - 48)
} else {
left++
}
if y[right] >= 48 && y[right] <= 57 {
b = int(y[right] - 48)
} else {
right--
}
ptr++
}
v2, _ := strconv.ParseInt(fmt.Sprintf("%d%d", a, b), 10, 64)
sum += int(v2)
}
return sum
}


part 2
Just add a function to transform the input because it was tricky, I have to do stuff I am not proud of

func transform(word string) string {
word = strings.ReplaceAll(word, "twone", "21" )
word = strings.ReplaceAll(word, "eightwo", "82" )
word = strings.ReplaceAll(word, "nineight", "98" )
word = strings.ReplaceAll(word, "eightthree", "83" )
word = strings.ReplaceAll(word, "threeight", "38" )
word = strings.ReplaceAll(word, "fiveight", "58" )
word = strings.ReplaceAll(word, "nineight", "98" )
word = strings.ReplaceAll(word, "oneight", "18" )
word = strings.ReplaceAll(word, "sevenine", "79" )

v := []string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
for x := range v {
word = strings.ReplaceAll(word, fmt.Sprintf("%d", x+1), v[x])
}
p := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"}
for x := range p {
word = strings.ReplaceAll(word, v[x], p[x])
}
return word
}
Re: [AOC December 2023] by preciouswoman66(m): 2:57pm On Dec 06, 2023
Una dey try sha embarassed
Re: [AOC December 2023] by Loopdeeloopa:
Deicide:
Day 1: Trebuchet?!

Writing the solution of this one in Go cool
part 1
func Trebuchet(input string) int {
data, err := utils.ReadFile(input)
if err != nil {
fmt.Println(err)
}
sum := 0
v1 := strings.Split(data, "\n" )
for _, y := range v1 {
left := 0
right := len(y) - 1
ptr := 0
a := 0
b := 0
for ptr <= len(y) {
if y[left] >= 48 && y[left] <= 57 {
a = int(y[left] - 48)
} else {
left++
}
if y[right] >= 48 && y[right] <= 57 {
b = int(y[right] - 48)
} else {
right--
}
ptr++
}
v2, _ := strconv.ParseInt(fmt.Sprintf("%d%d", a, b), 10, 64)
sum += int(v2)
}
return sum
}


part 2
Just add a function to transform the input because it was tricky, I have to do stuff I am not proud of

func transform(word string) string {
word = strings.ReplaceAll(word, "twone", "21" )
word = strings.ReplaceAll(word, "eightwo", "82" )
word = strings.ReplaceAll(word, "nineight", "98" )
word = strings.ReplaceAll(word, "eightthree", "83" )
word = strings.ReplaceAll(word, "threeight", "38" )
word = strings.ReplaceAll(word, "fiveight", "58" )
word = strings.ReplaceAll(word, "nineight", "98" )
word = strings.ReplaceAll(word, "oneight", "18" )
word = strings.ReplaceAll(word, "sevenine", "79" )

v := []string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
for x := range v {
word = strings.ReplaceAll(word, fmt.Sprintf("%d", x+1), v[x])
}
p := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"}
for x := range p {
word = strings.ReplaceAll(word, v[x], p[x])
}
return word
}
Nice to see gophers
Re: [AOC December 2023] by BBIA: 8:00am On Feb 08, 2024
Deicide:
Day 1: Trebuchet?!

Writing the solution of this one in Go cool
part 1
func Trebuchet(input string) int {
data, err := utils.ReadFile(input)
if err != nil {
fmt.Println(err)
}
sum := 0
v1 := strings.Split(data, "\n" )
for _, y := range v1 {
left := 0
right := len(y) - 1
ptr := 0
a := 0
b := 0
for ptr <= len(y) {
if y[left] >= 48 && y[left] <= 57 {
a = int(y[left] - 48)
} else {
left++
}
if y[right] >= 48 && y[right] <= 57 {
b = int(y[right] - 48)
} else {
right--
}
ptr++
}
v2, _ := strconv.ParseInt(fmt.Sprintf("%d%d", a, b), 10, 64)
sum += int(v2)
}
return sum
}


part 2
Just add a function to transform the input because it was tricky, I have to do stuff I am not proud of

func transform(word string) string {
word = strings.ReplaceAll(word, "twone", "21" )
word = strings.ReplaceAll(word, "eightwo", "82" )
word = strings.ReplaceAll(word, "nineight", "98" )
word = strings.ReplaceAll(word, "eightthree", "83" )
word = strings.ReplaceAll(word, "threeight", "38" )
word = strings.ReplaceAll(word, "fiveight", "58" )
word = strings.ReplaceAll(word, "nineight", "98" )
word = strings.ReplaceAll(word, "oneight", "18" )
word = strings.ReplaceAll(word, "sevenine", "79" )

v := []string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
for x := range v {
word = strings.ReplaceAll(word, fmt.Sprintf("%d", x+1), v[x])
}
p := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"}
for x := range p {
word = strings.ReplaceAll(word, v[x], p[x])
}
return word
}
are you into systems or network programming?
Re: [AOC December 2023] by Deicide(op): 3:49pm On Feb 14, 2024
BBIA:
are you into systems or network programming?
both
Re: [AOC December 2023] by BBIA: 5:06pm On Feb 14, 2024
Deicide:
both
no wonder you use golang.
1 Reply

AOC 2024My Goal Of Earning $40k/month Before December 2023 Startup234

Challenges Facing Nigerians In Securing International Remote Tech JobsC ++ : Understand Variables And Data Types In MinutesDream It And It’s Possible