-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
134 lines (112 loc) · 2.37 KB
/
main.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
package main
import (
"io"
"strings"
aoc "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
lines := aoc.ReaderToStrings(input)
programs := toPrograms(lines)
memory := make(map[uint]uint)
for _, program := range programs {
for _, write := range program.writes {
v := applyMask(write.value, program.mask)
memory[write.address] = v
}
}
sum := 0
for _, v := range memory {
sum += int(v)
}
return sum
}
func applyMask(value uint, mask string) uint {
for i := len(mask) - 1; i >= 0; i-- {
r := mask[i]
switch r {
case '0':
// Complement
m := (1 << (len(mask) - i - 1)) ^ ^uint(0)
value = value & m
case '1':
value = value | (1 << (len(mask) - i - 1))
}
}
return value
}
func toPrograms(lines []string) []Program {
var programs []Program
for i := 0; i < len(lines); {
del := aoc.NewDelimiter(lines[i], " ")
mask := del.GetString(2)
j := i + 1
var writes []Write
for ; j < len(lines); j++ {
if strings.HasPrefix(lines[j], "mask") {
break
}
del2 := aoc.NewDelimiter(lines[j], " ")
s := del2.GetString(0)
from := aoc.StringToInt(s[4 : len(s)-1])
to := del2.GetInt(2)
writes = append(writes, Write{
address: uint(from),
value: uint(to),
})
}
programs = append(programs, Program{
mask: mask,
writes: writes,
})
i = j
}
return programs
}
type Program struct {
mask string
writes []Write
}
type Write struct {
address uint
value uint
}
func fs2(input io.Reader) int {
lines := aoc.ReaderToStrings(input)
programs := toPrograms(lines)
memory := make(map[uint]uint)
for _, program := range programs {
for _, write := range program.writes {
addresses := applyMask2(len(program.mask)-1, write.address, program.mask)
for _, address := range addresses {
memory[address] = write.value
}
}
}
sum := 0
for _, v := range memory {
sum += int(v)
}
return sum
}
func applyMask2(i int, value uint, mask string) []uint {
if i == -1 {
return []uint{value}
}
r := mask[i]
switch r {
case '0':
return applyMask2(i-1, value, mask)
case '1':
value = value | (1 << (len(mask) - i - 1))
return applyMask2(i-1, value, mask)
case 'X':
// With zero
m := (1 << (len(mask) - i - 1)) ^ ^uint(0)
a := applyMask2(i-1, value&m, mask)
// With one
b := applyMask2(i-1, value|(1<<(len(mask)-i-1)), mask)
return append(a, b...)
default:
panic(mask)
}
}