-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.v
164 lines (154 loc) · 3.24 KB
/
main.v
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
164
import io
import os
import math.big
fn main() {
if os.args.len != 3 {
println("incorrect number of arguments")
return
}
mut ssdisplays := []SSDisplay{ cap: 200 }
file := os.open(os.args[2]) or {
panic("failed to open file: $err")
}
mut buf_reader := io.new_buffered_reader(reader: file)
for !buf_reader.end_of_stream() {
line := buf_reader.read_line() or {
break
}
ssdisplays << read_ssdisplay(line) or {
println("failed to read display: $err")
continue
}
}
match os.args[1] {
"part01" {
part01(ssdisplays)
}
"part02" {
part02(mut ssdisplays)
}
else {}
}
}
struct SSDisplay {
records []string
output_records []string
mut:
possibilities []byte
}
fn read_ssdisplay(line string) ?SSDisplay {
parts := line.split(" | ")
if parts.len != 2 {
return error("incorrect line")
}
return SSDisplay {
records: parts[0].split(" ")
output_records: parts[1].split(" ")
possibilities: []byte{ len: 7, init: 0b1111111 }
}
}
fn (mut ssdisplay SSDisplay) find_possibilities() {
mut fives := []byte{ len: 8 }
mut sixes := []byte{ len: 8 }
for record in ssdisplay.records {
indices := record.bytes().map(it - 'a'[0])
mut bit_mask := byte(0)
match record.len {
2 { bit_mask = 0b1011011 }
3 { bit_mask = 0b1011010 }
4 { bit_mask = 0b1010001 }
5 {
fives[7]++
for i in indices {
fives[i]++
}
continue
}
6 {
sixes[7]++
for i in indices {
sixes[i]++
}
continue
}
else { continue }
}
for i in 0..7 {
if i in indices {
ssdisplay.possibilities[i] &= ~bit_mask
} else {
ssdisplay.possibilities[i] &= bit_mask
}
}
}
if fives[7] >= 3 {
for i in 0..7 {
if fives[i] >= 3 {
ssdisplay.possibilities[i] &= 0b1001001
} else {
ssdisplay.possibilities[i] &= 0b0110110
}
}
}
if sixes[7] >= 3 {
for i in 0..7 {
if sixes[i] >= 3 {
ssdisplay.possibilities[i] &= 0b1100011
} else {
ssdisplay.possibilities[i] &= 0b0011100
}
}
}
}
fn (ssdisplay &SSDisplay) str() string {
return "records: ${ssdisplay.records.join(', ')}\n" +
"output: ${ssdisplay.output_records.join(', ')}\n" +
"poss: ${ssdisplay.possibilities.map(big.integer_from_int(it).binary_str()).join(', ')}\n"
}
fn part01(ssdisplays []SSDisplay) {
mut occurences := 0
for ssdisplay in ssdisplays {
for output_record in ssdisplay.output_records {
if output_record.len in [2, 3, 4, 7] {
occurences++
}
}
}
println("occurences: $occurences")
}
fn part02(mut ssdisplays []SSDisplay) {
mut output := 0
for mut ssdisplay in ssdisplays {
ssdisplay.find_possibilities()
mut value := 0
for output_record in ssdisplay.output_records {
mut bits := 0
for o in output_record {
bits |= ssdisplay.possibilities[o - 'a'[0]]
}
val := match bits {
0b1110111 { 0 }
0b0100100 { 1 }
0b1011101 { 2 }
0b1101101 { 3 }
0b0101110 { 4 }
0b1101011 { 5 }
0b1111011 { 6 }
0b0100101 { 7 }
0b1111111 { 8 }
0b1101111 { 9 }
else { -1 }
}
if val == -1 {
println("warn")
println("record: $output_record\n" +
"bits: " + big.integer_from_int(bits).binary_str() + "\n" +
"ssdisplay: " + ssdisplay.str())
} else {
value = val + 10 * value
}
}
output += value
}
println(output)
}