codebuddies / DailyAlgorithms

Do a problem. Create (or find) your problem in the issues. Paste a link to your solution. See others' solutions of the same problem.
12 stars 1 forks source link

[Advent of Code]: Day 3: No Matter How You Slice It #4

Open billglover opened 5 years ago

billglover commented 5 years ago

Day 3: No Matter How You Slice It

Part One

The Elves managed to locate the chimney-squeeze prototype fabric for Santa's suit (thanks to someone who helpfully wrote its box IDs on the wall of the warehouse in the middle of the night). Unfortunately, anomalies are still affecting them - nobody can even agree on how to cut the fabric.

The whole piece of fabric they're working on is a very large square - at least 1000 inches on each side.

Each Elf has made a claim about which area of fabric would be ideal for Santa's suit. All claims have an ID and consist of a single rectangle with edges parallel to the edges of the fabric. Each claim's rectangle is defined as follows:

A claim like #123 @ 3,2: 5x4 means that claim ID 123 specifies a rectangle 3 inches from the left edge, 2 inches from the top edge, 5 inches wide, and 4 inches tall. Visually, it claims the square inches of fabric represented by # (and ignores the square inches of fabric represented by .) in the diagram below:

...........
...........
...#####...
...#####...
...#####...
...#####...
...........
...........
...........

The problem is that many of the claims overlap, causing two or more claims to cover part of the same areas. For example, consider the following claims:

#1 @ 1,3: 4x4
#2 @ 3,1: 4x4
#3 @ 5,5: 2x2

Visually, these claim the following areas:

........
...2222.
...2222.
.11XX22.
.11XX22.
.111133.
.111133.
........

The four square inches marked with X are claimed by both 1 and 2. (Claim 3, while adjacent to the others, does not overlap either of them.)

If the Elves all proceed with their own plans, none of them will have enough fabric. How many square inches of fabric are within two or more claims?

Part Two

Amidst the chaos, you notice that exactly one claim doesn't overlap by even a single square inch of fabric with any other claim. If you can somehow draw attention to it, maybe the Elves will be able to make Santa's suit after all!

For example, in the claims above, only claim 3 is intact after all claims are made.

What is the ID of the only claim that doesn't overlap?

billglover commented 5 years ago

Solution: github.com/billglover/aoc/blob/master/2018/03/main.go

I don't like the idea that I used a 2D array to solve this. I could have made it a single dimensional array but am not sure that would feel any nicer. Would be really interested in seeing solutions that don't end up mapping square inches to memory locations in some way.

ISPOL commented 5 years ago

https://repl.it/@ISPOL/AdventOfCode2018

Python solution.

I spent a good hour or so wondering at why my logic wasn't working before I realized that I'd defined my 2d list wrong. As it turns out, [[]num]num gives you a list with num references to the same child list.

gauravchl commented 5 years ago

The javaScript solution: https://github.com/gauravchl/sourcecode/blob/master/adventofcode/day3.js

lpatmo commented 5 years ago

My Part 1 javascript solution which is really clunky but uh, it works: https://repl.it/@lpatmo/RelievedFarSites. No regex.

Update: Part 2 is in the same repl now too.


/*Part 2*/
//Compare the data with the new matrix
//If there are any numbers greater than 1, that array is NOT the fabric. 

function findFabricIndex() {
    for (let i = 0; i < positions.length; i++) {
        let exit = true; 
        for (let row = positions[i][0]; row < positions[i][0] + area[i][0]; row++) {
            for (let column = positions[i][1]; column < positions[i][1]+area[i][1]; column++) {
                if (rectangle[row][column] !== 1) {
                    exit = false; 
                } 
            }
        }
        if (exit) {
            console.log(i)
            return i;
        }
    }
}
findFabricIndex()
let fabricID = data[findFabricIndex()][0]
console.log(fabricID)```
MarsRoamer commented 5 years ago

JS solution: https://repl.it/@Maars/AoCDay3pt2

This is both parts 1 and 2. If anybody wants to take a look, I'd really appreciate some feedback!