Created
December 14, 2020 08:38
-
-
Save felix-larsen/715ac200fcf923357af04a786a5f94a9 to your computer and use it in GitHub Desktop.
14th December solution - Advent of Code 2020 - swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var currentMask = "" | |
var memory = [Int: String]() | |
lines.forEach { line in | |
let parts = line.components(separatedBy: " = ") | |
let variable = parts[0] | |
let value = parts[1] | |
print(variable, value) | |
if variable.contains("mask") { | |
currentMask = value | |
} else { | |
let address = Int(variable.filter { Int(String($0)) != nil })! | |
let valueDeci = Int(value)! | |
let valueBinary = Array(String(valueDeci, radix: 2).padLeft(upTo: 36, char: "0")) | |
var valueWithMaskApplied = "" | |
for (i, maskChar) in currentMask.enumerated() { | |
if maskChar == "X" { | |
if i < valueBinary.count { | |
valueWithMaskApplied += String(valueBinary[i]) | |
} else { | |
valueWithMaskApplied += "0" | |
} | |
} else { | |
valueWithMaskApplied += String(maskChar) | |
} | |
} | |
memory[address] = valueWithMaskApplied | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var currentMask = "" | |
var memory = [Int: String]() | |
lines.forEach { line in | |
let parts = line.components(separatedBy: " = ") | |
let variable = parts[0] | |
let value = parts[1] | |
if variable.contains("mask") { | |
currentMask = value | |
} else { | |
let address = Int(variable.filter { Int(String($0)) != nil })! | |
let addressBinary = Array(String(address, radix: 2).padLeft(upTo: 36, char: "0")) | |
let valueBinary = String(Int(value)!, radix: 2).padLeft(upTo: 36, char: "0") | |
var addressWithMaskApplied = "" | |
for (i, maskChar) in currentMask.enumerated() { | |
if maskChar == "0" { | |
addressWithMaskApplied += String(addressBinary[i]) | |
} | |
else { | |
addressWithMaskApplied += String(maskChar) | |
} | |
} | |
let addresses = addressesReplacedX(addressBinary: addressWithMaskApplied) | |
for a in addresses { | |
memory[Int(a,radix: 2)!] = valueBinary | |
} | |
} | |
} | |
let sumOfMemory = memory.values.map { Int($0, radix: 2)! }.reduce(0, +) | |
print(sumOfMemory) | |
func addressesReplacedX(addressBinary: String) -> [String] { | |
if !addressBinary.contains("X") { | |
return [addressBinary] | |
} else { | |
let indexOfX = addressBinary.firstIndex(of: "X")! | |
let firstAddress = addressBinary.replacingOccurrences(of: "X", with: "0", range: indexOfX ..< addressBinary.index(indexOfX, offsetBy: 1)) | |
let secondAddress = addressBinary.replacingOccurrences(of: "X", with: "1", range: indexOfX ..< addressBinary.index(indexOfX, offsetBy: 1)) | |
return addressesReplacedX(addressBinary: firstAddress) + addressesReplacedX(addressBinary: secondAddress) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment