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/muffa Dec 03 '18 edited Dec 03 '18

Solution in go/golang:

package main

import (
	"fmt"
	"io/ioutil"
	"regexp"
	"strconv"
	"strings"
)

type point struct {
	x, y int
}

type rect struct {
	x0, x1, y0, y1 int
	id             string
}

func main() {
	file, _ := ioutil.ReadFile("input_day3.txt")
	area := 0
	// Solution to first part
	var rectangles []rect
	areaMem := make(map[point]int)
	for _, line := range strings.Split(string(file), "\n") {
		var s point
		var r rect

		re, _ := regexp.Compile("^#([0-9]+)? @")
		match := re.FindStringSubmatch(line)
		r.id = match[1]

		re, _ = regexp.Compile("@ (.+)?,")
		match = re.FindStringSubmatch(line)
		padLeft, _ := strconv.Atoi(match[1])

		re, _ = regexp.Compile(",(.+)?:")
		match = re.FindStringSubmatch(line)
		padTop, _ := strconv.Atoi(match[1])

		re, _ = regexp.Compile(": (.+)?x")
		match = re.FindStringSubmatch(line)
		wide, _ := strconv.Atoi(match[1])

		re, _ = regexp.Compile("x([0-9]+)")
		match = re.FindStringSubmatch(line)
		tall, _ := strconv.Atoi(match[1])

		r.x0 = padLeft
		r.x1 = padLeft + wide
		r.y0 = padTop
		r.y1 = padTop + tall
		for y := r.y0; y < r.y1; y++ {
			for x := r.x0; x < r.x1; x++ {
				s.x = x
				s.y = y
				if _, exists := areaMem[s]; exists {
					areaMem[s]++
				} else {
					areaMem[s] = 1
				}
			}
		}
		rectangles = append(rectangles, r)

	}
	for _, v := range areaMem {
		if v > 1 {
			area++
		}
	}
	fmt.Println(area)

	for _, r := range rectangles {
		var s point
		flag := true
		for y := r.y0; y < r.y1; y++ {
			for x := r.x0; x < r.x1; x++ {
				s.x = x
				s.y = y
				if areaMem[s] > 1 {
					flag = false
				}
			}

		}
		if flag == true {
			fmt.Println(r.id)
		}

	}

}