-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.go
163 lines (128 loc) · 3.12 KB
/
day14.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Advent of Code 2020, Day 14
// (c) blu3r4y
package main
import (
"bufio"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
)
const INPUT_PATH = "./data/day14.txt"
const BIT_LENGTH = 36
type Assignment struct {
addr uint64
val uint64
}
type Masking struct {
zeros uint64
ones uint64
floats uint64
}
func main() {
program := load(INPUT_PATH)
fmt.Println(part1(program))
fmt.Println(part2(program))
}
func part1(program []interface{}) uint64 {
var mask Masking
memory := make(map[uint64]uint64)
for _, stmt := range program {
switch stmt.(type) {
case Masking:
// activate new mask
mask = stmt.(Masking)
case Assignment:
// set ones, clear zeros
var val uint64
val = stmt.(Assignment).val
val |= mask.ones
val &= ^mask.zeros
memory[stmt.(Assignment).addr] = val
}
}
return sumMapValues(memory)
}
func part2(program []interface{}) uint64 {
var mask Masking
memory := make(map[uint64]uint64)
for _, stmt := range program {
switch stmt.(type) {
case Masking:
// activate new mask
mask = stmt.(Masking)
case Assignment:
// set ones, clear floats
var addr uint64
addr = stmt.(Assignment).addr
addr |= mask.ones
addr &= ^mask.floats
// find all indexes of 1s in the original floating mask
var indexes []int
for i := 0; i < BIT_LENGTH; i++ {
if (mask.floats & (1 << i)) != 0 {
indexes = append(indexes, i)
}
}
// iterate over all possible binary combinations
numCombinations := int(math.Pow(2.0, float64(len(indexes))))
for i := 0; i < numCombinations; i++ {
newAddr := addr
bitPos := 0
quo := i
rem := 0
for quo > 0 {
rem = quo % 2
quo = quo / 2
if rem == 1 {
// 0 bits are cleared already at the beginning
newAddr |= (1 << indexes[bitPos])
}
bitPos++
}
memory[newAddr] = stmt.(Assignment).val
}
}
}
return sumMapValues(memory)
}
func load(path string) []interface{} {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
var program []interface{}
// extract three bit masks from the mask string
zrep := strings.NewReplacer("0", "1", "1", "0", "X", "0")
orep := strings.NewReplacer("0", "0", "1", "1", "X", "0")
frep := strings.NewReplacer("0", "0", "1", "0", "X", "1")
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "mask") {
// 'mask = BITS'
bits := strings.TrimPrefix(line, "mask = ")
zeros, _ := strconv.ParseUint(zrep.Replace(bits), 2, 64)
ones, _ := strconv.ParseUint(orep.Replace(bits), 2, 64)
floats, _ := strconv.ParseUint(frep.Replace(bits), 2, 64)
program = append(program, Masking{zeros, ones, floats})
} else if strings.HasPrefix(line, "mem") {
// 'mem[ADDR] = VAL'
parts := strings.Split(strings.TrimPrefix(line, "mem["), "] = ")
addr, _ := strconv.ParseUint(parts[0], 10, 64)
val, _ := strconv.ParseUint(parts[1], 10, 64)
program = append(program, Assignment{addr, val})
}
}
return program
}
func sumMapValues(m map[uint64]uint64) uint64 {
var result uint64
for _, val := range m {
result += val
}
return result
}