-
Notifications
You must be signed in to change notification settings - Fork 1
/
day3-1.toit
41 lines (34 loc) · 971 Bytes
/
day3-1.toit
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
import .file as file
import reader show BufferedReader
/**
Each line has a binary number of the same length.
Binary number 'gamma' has a '1' in a given column iff most bits of the input
have a '1' in the corresponding column.
Binary number 'epsilon' has a '1' in a given column iff most bits of the input
have a '0' in the corresponding column.
The result is gamma * epsilon.
*/
main:
reader := BufferedReader
file.Stream.for_read "3.txt"
first_line := reader.read_line
width := first_line.size
one_counts := List width: 0
accumulate one_counts first_line
line_count := 1
while line := reader.read_line:
accumulate one_counts line
line_count++
gamma := 0
epsilon := 0
one_counts.do:
gamma <<= 1
epsilon <<= 1
if it > line_count - it:
gamma |= 1
else:
epsilon |= 1
print gamma * epsilon
accumulate one_counts/List line/string -> none:
line.size.repeat:
if line[it] == '1': one_counts[it]++