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!

41 Upvotes

426 comments sorted by

View all comments

1

u/Ryuujinx Dec 03 '18

It isn't particularly elegant, but here's my solution in Go as I continue trying to learn it.

part1:

package main

import (
	"fmt"
	"bufio"
	"os"
	"strings"
	"strconv"
)

func main() {

	cloth := make([][]string,1000)
	for y := range cloth {
		cloth[y] = make([]string,1000)
	}

	f, err := os.Open("input")
	if err != nil {
		fmt.Print(err)
	}
	var conflict int
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		l := scanner.Text()
		specs := strings.Split(l, " ")
		id := string(specs[0][1:])
		pos := strings.Split(specs[2], ",")
		xpos,_ := strconv.Atoi(pos[0])
		ypos,_ := strconv.Atoi(strings.Split(pos[1],":")[0])
		xsize,_ := strconv.Atoi(strings.Split(specs[3], "x")[0])
		ysize,_ := strconv.Atoi(strings.Split(specs[3], "x")[1])


		for xidx := xpos; xidx < xpos+xsize; xidx++ {
			for yidx := ypos; yidx < ypos+ysize; yidx++ {
				if cloth[xidx][yidx] == "" {
					cloth[xidx][yidx] = id
				} else {
					cloth[xidx][yidx] = "O"
				}
			}
		}


	}


	for _,x := range cloth {
		for _,y := range x {
			if y == "O" {
				conflict = conflict + 1
			}
		}
	}

fmt.Println(conflict)
}    

Part2:

package main

import (
	"fmt"
	"bufio"
	"os"
	"strings"
	"strconv"
)

func main() {

	cloth := make([][]string,1000)
	for y := range cloth {
		cloth[y] = make([]string,1000)
	}

	f, err := os.Open("input")
	if err != nil {
		fmt.Print(err)
	}
	var conflict bool
	var answer string
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		l := scanner.Text()
		specs := strings.Split(l, " ")
		id := string(specs[0][1:])
		pos := strings.Split(specs[2], ",")
		xpos,_ := strconv.Atoi(pos[0])
		ypos,_ := strconv.Atoi(strings.Split(pos[1],":")[0])
		xsize,_ := strconv.Atoi(strings.Split(specs[3], "x")[0])
		ysize,_ := strconv.Atoi(strings.Split(specs[3], "x")[1])


		for xidx := xpos; xidx < xpos+xsize; xidx++ {
			for yidx := ypos; yidx < ypos+ysize; yidx++ {
				if cloth[xidx][yidx] == "" {
					cloth[xidx][yidx] = id
				} else {
					cloth[xidx][yidx] = "O"
				}
			}
		}


	}
	_, err = f.Seek(0, 0)
	if err != nil {
		fmt.Print(err)
	}
	scanner = bufio.NewScanner(f)
	for scanner.Scan() {
		l := scanner.Text()
		specs := strings.Split(l, " ")
		id := string(specs[0][1:])
		pos := strings.Split(specs[2], ",")
		xpos,_ := strconv.Atoi(pos[0])
		ypos,_ := strconv.Atoi(strings.Split(pos[1],":")[0])
		xsize,_ := strconv.Atoi(strings.Split(specs[3], "x")[0])
		ysize,_ := strconv.Atoi(strings.Split(specs[3], "x")[1])
		conflict = false
		for xidx := xpos; xidx < xpos+xsize; xidx++ {
			for yidx := ypos; yidx < ypos+ysize; yidx++ {
				if cloth[xidx][yidx] == "O" {
					 conflict = true
					 break
				}
			}
			if conflict {
				break
			}
		}
		if conflict == false {
			answer = id
			break
		}
	}
	fmt.Println(answer)
}