# Advent of Code 2020: Day 14 (Ruby solution)

Subscribe to my newsletter and never miss my upcoming articles

Today's tasks were bit manipulation. The initialization program (your puzzle input) can either update the bitmask or write a value to memory. Values and memory addresses are both 36-bit unsigned integers.

In the first task, a bitmask is used to transform a value before writing it. A 0 or 1 overwrites the corresponding bit in the value, while an X leaves the bit in the value unchanged.

In another task, a bitmask is used to transform an address. Immediately before a value is written to memory, each bit in the bitmask modifies the corresponding bit of the destination memory address in the following way:

• If the bitmask bit is 0, the corresponding memory address bit is unchanged.
• If the bitmask bit is 1, the corresponding memory address bit is overwritten with 1.
• If the bitmask bit is X, the corresponding memory address bit is floating. A floating bit is not connected to anything and instead fluctuates unpredictably. In practice, this means the floating bits will take on all possible values, potentially causing many memory addresses to be written all at once.

This is my Ruby solution:

``````def int_to_bin value
value.to_i.to_s(2).rjust(36, "0")
end

def sum_mem_values file, decode_address = false
mem = {}
file.each_line do |line|
command = line.split " = "
variable = command.first
value = command.last.strip

address_bin_copy[index] = '1' if char == '1'
end
end
else # decode value
value_bin = int_to_bin(value)
value_bin[index] = '1' if char == '1'
value_bin[index] = '0' if char == '0'
end