Day 14: Docking Data

Classes:

Mask(clearing, setting)

Represent the bitmask of the initialization program.

Write(address, value)

Represent a write to the memory.

Program(mask, writes)

Represent the initialization program.

Memory(slots)

Represent the state of the memory.

Functions:

parse_mask(text)

Parse the text as an clearing and a setting mask, respectively.

parse_write(text)

Parse the write instruction and return (address, value).

parse_lines(lines)

Parse the input into an initialization program.

execute(program)

Execute the program and return the memory values.

sum_memory(memory)

Sum the values in the memory slots.

class Mask(clearing: int, setting: int)[source]

Represent the bitmask of the initialization program.

Methods:

__init__(clearing, setting)

Initialize with the given values.

Attributes:

clearing

mask of the bits to be cleared (AND'ed)

setting

mask of the bits to be set (OR'ed)

__init__(clearing: int, setting: int) None[source]

Initialize with the given values.

Requires
  • 0 <= setting <= 2**36 - 1

  • 0 <= clearing <= 2**36 - 1

clearing: Final[int]

mask of the bits to be cleared (AND’ed)

setting: Final[int]

mask of the bits to be set (OR’ed)

parse_mask(text: str) Mask[source]

Parse the text as an clearing and a setting mask, respectively.

Requires
  • MASK_RE.match(text)

Ensures
  • 0 <= result.setting <= 2**36 - 1

    (The setting mask not too large)

  • 0 <= result.clearing <= 2**36 - 1

    (The clearing mask not too large)

class Write(address: int, value: int)[source]

Represent a write to the memory.

Methods:

__init__(address, value)

Initialize with the given values.

Attributes:

address

Address (offset) in the memory

value

Value to be written

__init__(address: int, value: int) None[source]

Initialize with the given values.

Requires
  • 0 <= value <= 2**36 - 1

    (The value in expected range)

  • address >= 0

address: Final[int]

Address (offset) in the memory

value: Final[int]

Value to be written

parse_write(text: str) Tuple[int, int][source]

Parse the write instruction and return (address, value).

Requires
  • WRITE_RE.match(text)

Ensures
  • result[1] >= 0

    (Value non-negative)

  • result[0] >= 0

    (Address non-negative)

class Program(mask: Mask, writes: List[Write])[source]

Represent the initialization program.

Methods:

__init__(mask, writes)

Initialize with the given values.

Attributes:

mask

Mask used throughout the program

writes

Write instructions

__init__(mask: Mask, writes: List[Write]) None[source]

Initialize with the given values.

mask: Final[Mask]

Mask used throughout the program

writes: Final[List[Write]]

Write instructions

parse_lines(lines: Lines) Program[source]

Parse the input into an initialization program.

Requires
  • len(lines) > 2

  • MASK_RE.match(lines[0])

  • all(WRITE_RE.match(line) for line in lines[1:])

class Memory(slots: Mapping[int, int])[source]

Represent the state of the memory.

Methods:

__init__(slots)

Initialize with the given values.

Attributes:

slots

Slot map as address 🠒 value

__init__(slots: Mapping[int, int]) None[source]

Initialize with the given values.

Requires
  • all(key >= 0 for key in slots.keys())

    (Addresses non-negative)

  • all(value >= 0 for value in slots.values())

    (Values non-negative)

slots: Final[Mapping[int, int]]

Slot map as address 🠒 value

execute(program: Program) Memory[source]

Execute the program and return the memory values.

sum_memory(memory: Memory) int[source]

Sum the values in the memory slots.

Ensures
  • result >= 0