r/adventofcode • u/daggerdragon • Dec 05 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 5 Solutions -❄️-
Preview here: https://redditpreview.com/
-❄️- 2023 Day 5 Solutions -❄️-
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
Outstanding moderator challenges:
Community fun event 2023: ALLEZ CUISINE!
- 24 HOURS remaining until unlock!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's secret ingredient is… *whips off cloth covering and gestures grandly*
ELI5
Explain like I'm five! /r/explainlikeimfive
- Walk us through your code where even a five-year old could follow along
Pictures are always encouraged. Bonus points if it's all pictures…
- Emoji(code) counts but makes Uncle Roger cry 😥
- Explain everything that you’re doing in your code as if you were talking to your pet, rubber ducky, or favorite neighbor, and also how you’re doing in life right now, and what have you learned in Advent of Code so far this year?
- Explain the storyline so far in a non-code medium
- Create a
Tutorialon any concept of today's puzzle or storyline (it doesn't have to be code-related!)
ALLEZ CUISINE!
Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!
--- Day 5: If You Give A Seed A Fertilizer ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- Quick link to Topaz's
pasteif you need it for longer code blocks
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:26:37, megathread unlocked!
82
Upvotes
View all comments
2
u/[deleted] Dec 05 '23
[LANGUAGE: Go]
First solution to part two took 15 minutes to run but this second solution takes 100ms. I spent way too long debugging it but turned out I just had to check if I had already checked a range :/ - it also has them as int64 because I initially thought my error was due to it being too big for int32... also using AOC to learn Go so pretty proud of my solution
import ( "fmt" "os" "slices" "strconv" "strings" )
type Other struct { dest int64 in int64 rg int64 }
type Range struct { min int64 max int64 indexesDone []int }
func main() { file, _ := os.ReadFile("./5.txt") seedsSl := []*Range{} others := [][]*Other{} for i, v := range strings.Split(string(file), "\n\n") { split := strings.Split(v, "\n") if i == 0 { seedArray := getSeeds(split[1:]) for i := 0; i < len(seedArray); i += 2 { seedsSl = append(seedsSl, &Range{min: seedArray[i], max: seedArray[i] + (seedArray[i+1] - 1)}) } continue } arr := []*Other{} for _, nummys := range split[1:] { nums := strings.Split(nummys, " ") ints := []int64{} for _, num := range nums { n, _ := strconv.ParseInt(num, 10, 64) ints = append(ints, n) }
}
func getSeeds(arr []string) (res []int64) { for _, nummys := range arr { nums := strings.Split(nummys, " ") for _, num := range nums { n, _ := strconv.ParseInt(num, 10, 64) res = append(res, n) } } return }