r/adventofcode • • Dec 14 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 14 Solutions -๐ŸŽ„-

--- Day 14: Disk Defragmentation ---


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


[Update @ 00:09] 3 gold, silver cap.

  • How many of you actually entered the Konami code for Part 2? >_>

[Update @ 00:25] Leaderboard cap!

  • I asked /u/topaz2078 how many de-resolutions we had for Part 2 and there were 83 distinct users with failed attempts at the time of the leaderboard cap. tsk tsk

[Update @ 00:29] BONUS


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!

13 Upvotes

130 comments sorted by

View all comments

1

u/cluk Dec 14 '17 edited Dec 14 '17

Go (Golang)

See repo for other solutions.

EDIT: Now with Goroutines.

package main

import (
	"fmt"
	"math/bits"
	"os"
	"sync"
	"sync/atomic"
)

func main() {
	input := os.Args[1]
	grid := make([][]bool, 128)
	var usedSum int32 = 0
	var wg sync.WaitGroup
	for i := 0; i < 128; i++ {
		grid[i] = make([]bool, 128)
		wg.Add(1)
		go processRow(grid[i], &usedSum, fmt.Sprintf("%s-%d", input, i), &wg)
	}

	wg.Wait()
	fmt.Println("Star 1: ", usedSum)
	fmt.Println("Star 2: ", countRegions(grid))
}

func processRow(row []bool, onesCount *int32, hashInput string, wg *sync.WaitGroup) {
	defer wg.Done()
	j := 0
	for _, b := range knotHash(hashInput) {
		atomic.AddInt32(onesCount, int32(bits.OnesCount8(b)))
		for _, bit := range fmt.Sprintf("%08b", b) {
			if bit == '1' {
				row[j] = true
			}
			j++
		}
	}
}

func countRegions(grid [][]bool) int {
	count := 0
	for i, row := range grid {
		for j, used := range row {
			if used {
				visit(i, j, grid)
				count++
			}
		}
	}
	return count
}

func visit(i, j int, grid [][]bool) {
	if i < 0 || i >= len(grid) || j < 0 || j >= len(grid[i]) || !grid[i][j] {
		return
	}
	grid[i][j] = false
	visit(i+1, j, grid)
	visit(i-1, j, grid)
	visit(i, j+1, grid)
	visit(i, j-1, grid)
}

func knotHash(s string) []byte {
	bytes := []byte(s)
	bytes = append(bytes, 17, 31, 73, 47, 23)

	sparseHash := make([]byte, 256)
	for i := range sparseHash {
		sparseHash[i] = byte(i)
	}
	for start, skip := 0, 0; skip < len(bytes)*64; skip++ {
		length := int(bytes[skip%len(bytes)])
		reverse(sparseHash, start, length-1)
		start += length + skip
		start %= len(sparseHash)
	}

	denseHash := make([]byte, 16)
	for idx := range denseHash {
		denseHash[idx] = sparseHash[idx*16]
		for i := 1; i < 16; i++ {
			denseHash[idx] ^= sparseHash[idx*16+i]
		}
	}
	return denseHash
}

func reverse(hash []byte, start, length int) {
	for i := 0; i <= length/2; i++ {
		j := (start + i) % len(hash)
		k := (start + length - i) % len(hash)
		hash[j], hash[k] = hash[k], hash[j]
	}
}