nothe

She's a Nerd!

  • she/her

Knitter, sewist, Seattle Soccer Fan, occasional tech stuff, whatever else tickles my fancy. Cilantro is fine, but pickles are a waste of a perfectly good cucumber.
@nothe@notacult.social


I haven't given up!! But I'm making peace with "failure" right now.

I did just finish up day 18 (just before 19 releases! aah!) after a nice chill day of video games and cooking, so I'm even in a good head-space to talk about it! ...below the fold, of course


The problem involved finding the surface area of a shape defined as cubic coordinates, and then the outer surface area of same.

The interesting thing to me, was that for part 1 (how many surfaces of these cubes touch air?), It was easier to calculate it as we read in the data, but for part 2, I tried that and ended up snarled in the concept of "ok, what if a really BIG air pocket gets closed in at the end?" and decided that it would be easier to "draw a box" around my shape, (one bigger, so we didn't miss those outer surfaces!), and then recursively walk every square until we bumped into the shape. Each time we bump it, that's one more surface area increment!

I did spend a lot of time thinking about how to express a surface in a 3d coordinate space, and am pretty pleased about the results, especially once I realized I could normalize them by always referring to the surface as the "smaller" side of any coordinate, for comparison purposes.

class Surface: CustomStringConvertible, Equatable {
    var coord: ThreeDCoord
    var dimension: Dimension

    init(coord: ThreeDCoord, dimension: Dimension, lowSide: Bool) {
        self.coord = coord
        self.dimension = dimension
        if lowSide == false {
            switch (dimension) {
            case .x:
                self.coord = ThreeDCoord(x: coord.x + 1, y: coord.y, z: coord.z)
            case .y:
                self.coord = ThreeDCoord(x: coord.x, y: coord.y + 1, z: coord.z)
            case .z:
                self.coord = ThreeDCoord(x: coord.x, y: coord.y, z: coord.z + 1)
            }
        }
    }

    var description: String {
        return "\(coord): low side of \(dimension)"
    }

    static func == (lhs: Surface, rhs: Surface) -> Bool {
        return lhs.coord == rhs.coord && lhs.dimension == rhs.dimension
    }

    static func removeTouching(surfaces: [Surface], from coord: ThreeDCoord) -> [Surface] {
        let coordSurfaces: [Surface] = Surface.surfacesFor(coord: coord)
        var newSurfaces = Array(surfaces) // copy of our current outer surfaces of our shape.
        for surface in coordSurfaces { // for each surface on my new shape
            if newSurfaces.contains(surface) {
                if let index = newSurfaces.firstIndex(of: surface) {
                    newSurfaces.remove(at: index)
                }
            } else {
                newSurfaces.append(surface)
            }
        }
        return newSurfaces
    }

    static func surfacesFor(coord: ThreeDCoord) -> [Surface] {
        var surfaces: [Surface] = []
        surfaces.append(Surface(coord: coord, dimension: .x, lowSide: true))
        surfaces.append(Surface(coord: coord, dimension: .x, lowSide: false))
        surfaces.append(Surface(coord: coord, dimension: .y, lowSide: true))
        surfaces.append(Surface(coord: coord, dimension: .y, lowSide: false))
        surfaces.append(Surface(coord: coord, dimension: .z, lowSide: true))
        surfaces.append(Surface(coord: coord, dimension: .z, lowSide: false))

        return surfaces
    }
}

(Dimension is just an enum containing x, y, and z)

(https://github.com/nothes/AdventOfCode/blob/main/AoC/AoC/Day%2018/day18.swift)


You must log in to comment.