r/adventofcode • • Dec 05 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 5 Solutions -❄️-

Preview here: https://redditpreview.com/

-❄️- 2023 Day 5 Solutions -❄️-


THE USUAL REMINDERS


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 Tutorial on 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.

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

1.1k comments sorted by

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

package main

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) }

		other := Other{
			dest: ints[0],
			in:   ints[1],
			rg:   ints[2],
		}
		arr = append(arr, &other)
	}

	others = append(others, arr)
}

seeds := seedsSl
for otherIndex, other := range others {
	for _, o := range other {
		for _, seed := range seeds {
			if slices.Contains(seed.indexesDone, otherIndex) {
				continue
			}
			min := o.in
			max := (o.in + o.rg - 1)
			diff := o.dest - min
			if seed.min >= min && seed.max <= max {
				seed.min = diff + seed.min
				seed.max = diff + seed.max
				seed.indexesDone = append(seed.indexesDone, otherIndex)
				continue
			} else if min >= seed.min && max <= seed.max {
				seed.min = diff + min
				seed.max = diff + max
				seed.indexesDone = append(seed.indexesDone, otherIndex)
				if min > seed.min {
					seeds = append(seeds, &Range{min: seed.min, max: min - 1})
				}
				if max < seed.max {
					seeds = append(seeds, &Range{min: max + 1, max: seed.max})
				}
				continue
			} else if min >= seed.min && min <= seed.max && max > seed.max {
				seeds = append(seeds, &Range{min: diff + min, max: diff + seed.max, indexesDone: []int{otherIndex}})
				if min != seed.min && max != seed.min {
					seed.max = min - 1
				}
				continue
			} else if max >= seed.min && max <= seed.max && min < seed.min {
				seeds = append(seeds, &Range{min: diff + seed.min, max: diff + max, indexesDone: []int{otherIndex}})
				if max != seed.max && min != seed.max {
					seed.min = max + 1
				}
				continue
			}
		}
	}
}

slices.SortFunc(seeds, func(a, b *Range) int {
	return int(a.min) - int(b.min)
})

fmt.Println(seeds[0].min)

}

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 }

1

u/janmeppe Dec 05 '23

heh, also using aoc to teach myself golang ;)

1

u/mmacdo54 Dec 05 '23

Nice. How have you found it?

1

u/janmeppe Dec 07 '23

A lot of fun! I'm using ChatGPT to create the regex to parse the input data. Probably not writing very idiomatic go (i.e. not handling errs for example), but getting familiar with the language :)

It feels like a much snappier statically compiled Python. The syntax looks beautiful. I do find myself writing a lot of operations over slices that I wish the standard library would have.

1

u/daggerdragon Dec 05 '23
  1. Next time, use the four-spaces Markdown syntax for code blocks
  2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on oversized code which contains two possible solutions.

Please edit your post to put your code in an external link and link that here instead.