Day22
Copyright(c) Eric Mertens 2021
LicenseISC
Maintaineremertens@gmail.com
Safe HaskellNone
LanguageHaskell2010

Main

Description

https://adventofcode.com/2021/day/22

This problem is made simple by processing commands by subtracting away all future cuboids. Only the region unique to the current command will affect the final output.

Synopsis

Documentation

data C Source #

On and off commands from the input file

Constructors

Con 
Coff 

main :: IO () Source #

>>> :main
606484
1162571910364852

solve :: [(C, Box n)] -> Int Source #

Figure out how many lights the given instructions turn on.

applyCommand Source #

Arguments

:: [Box n]

pre-lit boxes

-> (C, Box n)

command

-> [Box n]

post-lit boxes

Apply a command given a list of non-overlapping, illuminated regions.

Segments

data Seg Source #

A segment defined by an inclusive lower-bound and an exclusive upper-bound.

Constructors

Seg !Int !Int 

len :: Seg -> Int Source #

Compute the length of a segment

intersectSeg :: Seg -> Seg -> Maybe Seg Source #

Determine if two segments have some overlap

inSeg :: Int -> Seg -> Bool Source #

Determine if a value falls within a segment.

N-dimensional boxes

data N Source #

Natural numbers (used for type index)

Constructors

S N 
Z 

data Box :: N -> Type where Source #

An n-dimensional box.

Constructors

Pt 

Fields

  • :: Box 'Z

    A single point

(:*) infixr 6 

Fields

  • :: Seg
     
  • -> Box n
     
  • -> Box ('S n)

    A box extended along an axis

size :: Box n -> Int Source #

Returns the number of points contained in a box.

>>> size (Seg 1 4 :* Seg 0 3 :* Seg 0 2 :* Pt)
18

intersectBox :: Box n -> Box n -> Maybe (Box n) Source #

The intersection of two boxes is the intersection of their segments.

subBox Source #

Arguments

:: Box n

remove this

-> Box n

from this

-> [Box n] 

Subtract the second box from the first box returning a list of boxes that cover all the remaining area.

>>> subBox (Seg 2 3 :* Pt) (Seg 0 4 :* Pt)
[Seg 0 2 :* Pt,Seg 3 4 :* Pt]
>>> subBox (Seg 3 5 :* Pt) (Seg 0 4 :* Pt)
[Seg 0 3 :* Pt]

traverseBox2 :: Applicative f => (Seg -> Seg -> f Seg) -> Box n -> Box n -> f (Box n) Source #

Zip two boxes together.