You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
1.5 KiB
Zig
86 lines
1.5 KiB
Zig
const input = @embedFile("input3");
|
|
const std = @import("std");
|
|
|
|
pub fn main() !void{
|
|
|
|
|
|
var counting = [1]usize{0} ** 12;
|
|
|
|
var iter = std.mem.tokenize(u8, input, "\n");
|
|
while(iter.next() ) |line| {
|
|
|
|
for(line)|c, i|{
|
|
counting[i] += (c -48); //HACK: subtract 48 to go from ascii to bit
|
|
}
|
|
|
|
|
|
//@popCount(u8,line); // >=3 means 1 is most common bit.
|
|
}
|
|
|
|
|
|
//
|
|
var gamma: u12 = 0;
|
|
var epsilon:u12 = 0;
|
|
|
|
for(counting)|count, i|{
|
|
|
|
if(count > 500){
|
|
counting[i] = 1;
|
|
}else{
|
|
counting[i] = 0;
|
|
}
|
|
}
|
|
|
|
|
|
for(counting)|c, i|{
|
|
if(c == 1) gamma |= @as(u12, 1) << @intCast(u4,i ) ;
|
|
}
|
|
|
|
gamma = @bitReverse(u12, gamma);
|
|
epsilon = ~gamma;
|
|
|
|
const result: u64 = gamma * @as(u64, epsilon);
|
|
|
|
std.debug.print("result: {}", .{result});
|
|
}
|
|
|
|
test "part 1"{
|
|
|
|
|
|
var counting = [1]usize{0} ** 12;
|
|
|
|
var iter = std.mem.tokenize(u8, input, "\n");
|
|
while(iter.next() ) |line| {
|
|
|
|
for(line)|c, i|{
|
|
counting[i] += (c -48); //HACK: subtract 48 to go from ascii to bit
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//
|
|
var gamma: u12 = 0;
|
|
var epsilon:u12 = 0;
|
|
|
|
for(counting)|count, i|{
|
|
|
|
if(count > 500){
|
|
counting[i] = 1;
|
|
}else{
|
|
counting[i] = 0;
|
|
}
|
|
}
|
|
|
|
|
|
for(counting)|c, i|{
|
|
if(c == 1) gamma |= @as(u12, 1) << @intCast(u4,i ) ;
|
|
}
|
|
|
|
gamma = @bitReverse(u12, gamma);
|
|
epsilon = ~gamma;
|
|
const result: u64 = gamma * @as(u64, epsilon);
|
|
|
|
std.testing.expect(2648450 == result);
|
|
}
|