added day 14 part 2
This commit is contained in:
parent
61d838f30d
commit
de7df62e28
|
@ -0,0 +1,4 @@
|
||||||
|
mask = 000000000000000000000000000000X1001X
|
||||||
|
mem[42] = 100
|
||||||
|
mask = 00000000000000000000000000000000X0XX
|
||||||
|
mem[26] = 1
|
|
@ -1,11 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
"strconv"
|
|
||||||
"io/ioutil"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -42,11 +42,75 @@ func main() {
|
||||||
mem[addr] = av
|
mem[addr] = av
|
||||||
} else if len(maskMatch) != 0 {
|
} else if len(maskMatch) != 0 {
|
||||||
mask = maskMatch[1]
|
mask = maskMatch[1]
|
||||||
} else {}
|
} else {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var sum int64 = 0
|
var sum int64 = 0
|
||||||
for _, v := range mem {
|
for _, v := range mem {
|
||||||
sum += v
|
sum += v
|
||||||
}
|
}
|
||||||
fmt.Println("Part 1:", sum)
|
fmt.Println("Part 1:", sum)
|
||||||
|
// Part 2
|
||||||
|
mem = make(map[int64]int64) // reset memory
|
||||||
|
mask = ""
|
||||||
|
for _, l := range lines {
|
||||||
|
memMatch := memre.FindStringSubmatch(l)
|
||||||
|
maskMatch := maskre.FindStringSubmatch(l)
|
||||||
|
if len(memMatch) != 0 {
|
||||||
|
addr, _ := strconv.ParseInt(memMatch[1], 10, 64)
|
||||||
|
value, _ := strconv.ParseInt(memMatch[2], 10, 64)
|
||||||
|
addrString := strconv.FormatInt(addr, 2) // addr as a binary string
|
||||||
|
v := ""
|
||||||
|
// Pad out to 36 bits
|
||||||
|
for i := 36 - len(addrString); i > 0; i-- {
|
||||||
|
v = "0" + v
|
||||||
|
}
|
||||||
|
v = v + addrString
|
||||||
|
applied := ""
|
||||||
|
var addresses []string
|
||||||
|
for i, _ := range v {
|
||||||
|
if mask[i] != byte('0') {
|
||||||
|
applied += string(mask[i])
|
||||||
|
} else {
|
||||||
|
applied += string(v[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addresses = constructAddresses(applied)
|
||||||
|
for _, a := range addresses {
|
||||||
|
d, _ := strconv.ParseInt(a, 2, 64)
|
||||||
|
mem[d] = value
|
||||||
|
}
|
||||||
|
} else if len(maskMatch) != 0 {
|
||||||
|
mask = maskMatch[1]
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sum = 0
|
||||||
|
for _, v := range mem {
|
||||||
|
sum += v
|
||||||
|
}
|
||||||
|
fmt.Println("Part 2:", sum)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func constructAddresses(addr string) []string {
|
||||||
|
var out []string
|
||||||
|
x := false
|
||||||
|
for _, c := range addr {
|
||||||
|
if c == 'X' {
|
||||||
|
x = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if x == true {
|
||||||
|
ones := constructAddresses(strings.Replace(addr, "X", "1", 1))
|
||||||
|
zeroes := constructAddresses(strings.Replace(addr, "X", "0", 1))
|
||||||
|
for _, i := range ones {
|
||||||
|
out = append(out, i)
|
||||||
|
}
|
||||||
|
for _, i := range zeroes {
|
||||||
|
out = append(out, i)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
return []string{addr}
|
||||||
}
|
}
|
Loading…
Reference in New Issue