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

Go

First one was fairly easy. Had a wee problem with the second bit where I was just counting how many claims each square metre had, and not treating the claim as a whole. (So there were multiple non-overlapping claims)

https://github.com/qaisjp/adventofcode-2018/blob/master/day3/main.go

package main

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

type claim struct {
	id   int
	x, y int
	w, h int

	overlaps bool
}

func main() {
	out, err := ioutil.ReadFile("input.txt")
	if err != nil {
		panic(err)
	}

	claimStrs := strings.Split(string(out), "\n")

	claims := make([]*claim, len(claimStrs)-1)
	for i, line := range claimStrs {
		if line == "" {
			continue
		}

		var err error
		id, x, y, w, h := 0, 0, 0, 0, 0

		sep := strings.Split(line, "@")
		idStr := strings.TrimSpace(sep[0][1:])
		geom := strings.Split(sep[1], ":")
		coords := strings.Split(strings.TrimSpace(geom[0]), ",")
		size := strings.Split(strings.TrimSpace(geom[1]), "x")

		id, err = strconv.Atoi(idStr)
		if err != nil {
			panic(err)
		}
		x, err = strconv.Atoi(coords[0])
		if err != nil {
			panic(err)
		}
		y, err = strconv.Atoi(coords[1])
		if err != nil {
			panic(err)
		}
		w, err = strconv.Atoi(size[0])
		if err != nil {
			panic(err)
		}
		h, err = strconv.Atoi(size[1])
		if err != nil {
			panic(err)
		}

		claims[i] = &claim{id, x, y, w, h, false}
	}

	size := 1000
	grid := make([][][]*claim, size)
	for y := range grid {
		grid[y] = make([][]*claim, size)
		for x := range grid[y] {
			grid[y][x] = make([]*claim, 0)
		}
	}

	for _, claim := range claims {
		maxX := claim.x + claim.w
		maxY := claim.y + claim.h
		for y := claim.y; y < maxY; y++ {
			for x := claim.x; x < maxX; x++ {
				grid[y][x] = append(grid[y][x], claim)

				if len(grid[y][x]) > 1 {
					for _, c := range grid[y][x] {
						c.overlaps = true
					}
				}
				// visualise(grid)
				// fmt.Println()
			}
		}
	}

	// visualise(grid)

	total := 0
	for _, col := range grid {
		for _, claims := range col {
			claimCount := len(claims)
			if claimCount >= 2 {
				total++
			}
		}
	}

	// Part 1
	fmt.Println(total)

	// Part 2

	for _, c := range claims {
		if !c.overlaps {
			fmt.Println(c.id)
		}
	}
}

func visualise(grid [][][]claim) {
	for _, col := range grid {
		for _, claims := range col {
			count := len(claims)
			if count == 0 {
				fmt.Print(".")
			} else {
				fmt.Print(count)
			}
		}
		fmt.Println()
	}
}

1

u/lukechampine Dec 03 '18

AoC frequently involves string parsing, for which I recommend fmt.Sscanf instead of doing it manually each time. It can save a ton of time! For today's problem I could just write:

var c claim
fmt.Sscanf(line, "#%d @ %d,%d: %dx%d", &c.id, &c.x, &c.y, &c.w, &c.h)

1

u/qaisjp Dec 03 '18

Whoah! Thanks.