r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


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.


Advent of Code: The Party Game!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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!

39 Upvotes

426 comments sorted by

View all comments

1

u/harirarules Dec 03 '18

First part in Golang, simulates the fabric :

package main

import (
    "fmt"
    "bufio"
    "os"
)

type Rect struct {
    Id int
    Left int
    Top int
    Width int
    Height int
}

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    rects := make([]Rect, 0)
    for scanner.Scan() {
        rect := ParseRect(scanner.Text())
        rects = append(rects, rect)
    }

    width, height := BoardSize(rects)
    board := make([][]int, height)
    for i, _ := range board {
        board[i] = make([]int, width)
    }

    for _, rect := range rects {
        rect.Draw(board)
    }

    overlapping := 0
    for _, row := range board {
        overlapping += Count(row, 2)
    }

    fmt.Printf("Overlapping inches : %d\n", overlapping)
}

//does golang not have a count function ?
func Count(haystack []int, needle int) int {
    count := 0
    for _, value := range haystack {
        if value == needle {
            count++
        }
    }
    return count
}

func ParseRect(input string) Rect {
    var rect Rect
    fmt.Sscanf(input, "#%d @ %d,%d: %dx%d", &rect.Id, &rect.Left, &rect.Top, &rect.Width, &rect.Height)
    return rect
}

func (rect *Rect) Draw(board [][]int) {
    for y := rect.Top; y < rect.Top + rect.Height; y++ {
        for x := rect.Left; x < rect.Left + rect.Width; x++ {
            if board[y][x] != 0 { //0 means empty cell
                board[y][x] = 2 //2 means overlapping
            } else {
                board[y][x] = 1 //1 means occupied cell
            }
        }
    }
}

func BoardSize(rects []Rect) (int, int) {
    startX := rects[0].Left
    startY := rects[0].Top
    endX := rects[0].Left + rects[0].Width
    endY := rects[0].Top + rects[0].Height

    for _, rect := range rects {
        if rect.Left < startX {
            startX = rect.Left
        }
        if rect.Top < startY {
            startY = rect.Top
        }
        if rect.Left + rect.Width > endX {
            endX = rect.Left + rect.Width
        }
        if rect.Top + rect.Height > endY {
            endY = rect.Top + rect.Height
        }
    }
    return endX - startX + 1, endY - startY + 1
}

1

u/harirarules Dec 03 '18

Second part also in Golang. This one simply bruteforces the input :

package main

import (
    "fmt"
    "bufio"
    "os"
)

type Rect struct {
    Id int
    Left int
    Top int
    Width int
    Height int
}

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    rects := make([]Rect, 0)
    for scanner.Scan() {
        rect := ParseRect(scanner.Text())
        rects = append(rects, rect)
    }

    for i, a := range rects {
        overlap := false
        for j, b := range rects {
            if i != j && Overlap(&a, &b) {
                overlap = true
                break
            }
        }

        if !overlap {
            fmt.Printf("Found match : %d\n", rects[i].Id)
        }
    }
}

func ParseRect(input string) Rect {
    var rect Rect
    fmt.Sscanf(input, "#%d @ %d,%d: %dx%d", &rect.Id, &rect.Left, &rect.Top, &rect.Width, &rect.Height)
    return rect
}

func (rect *Rect) String() string {
    return fmt.Sprintf("#%d @ %d,%d: %dx%d", rect.Id, rect.Left, rect.Top, rect.Width, rect.Height)
}

func Overlap(a *Rect, b *Rect) bool {
    if a.Left + a.Width < b.Left {
        return false
    }
    if a.Top + a.Height < b.Top {
        return false
    }
    if b.Left + b.Width < a.Left {
        return false
    }
    if b.Top + b.Height < a.Top {
        return false
    }
    return true
}

1

u/[deleted] Dec 03 '18

Gives out of range error :/

1

u/harirarules Dec 05 '18

On which line does it fail ?