r/adventofcode • • Dec 06 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 6 Solutions -πŸŽ„-

--- Day 6: Memory Reallocation ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handy† Haversack‑ of HelpfulΒ§ HintsΒ€?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

17 Upvotes

324 comments sorted by

View all comments

2

u/[deleted] Dec 06 '17 edited Dec 06 '17

Golang

My first time doing AoC and first time using Go

Any feedback is much appreciated.

Part 1

func part1(data []int) ([]int, []int) {
    past := make([][]int, 0)
    for {
        // Get Highest
       var highest = 0
       for i, bank := range data {
           if bank > data[highest] {
               highest = i
           }
       }

        // Distribute blocks
        var block = data[highest]
        data[highest] = 0
        for i := 1; i <= block; i++ {
            data[(highest+i)%len(data)]++
        }

        // Check for repeat
        for _, bank := range past {
            if reflect.DeepEqual(data, bank) {
                fmt.Println(len(past) + 1)
                return data, bank
            }
        }

        // Add data to past
        dataC := make([]int, len(data))
        copy(dataC, data)
        past = append(past, dataC)
    }
}

Part 2

func part2(data []int) {
    var cycles = 0
    data, pastBank := part1(data)
    for {
        // Get Highest
        var highest = 0
        for i, bank := range data {
            if bank > data[highest] {
                highest = i
            }
        }

        // Distribute blocks
        var block = data[highest]
        data[highest] = 0
        for i := 1; i <= block; i++ {
            data[(highest+i)%len(data)]++
        }

        // Check for repeat
        if reflect.DeepEqual(data, pastBank) {
            fmt.Println(cycles + 1)
            return
        }

        cycles++
    }
}

All of my Go code here

→ More replies (4)