attemps from before
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
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);
|
||||
}
|
||||
+1000
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,12 @@
|
||||
00100
|
||||
11110
|
||||
10110
|
||||
10111
|
||||
10101
|
||||
01111
|
||||
00111
|
||||
11100
|
||||
10000
|
||||
11001
|
||||
00010
|
||||
01010
|
||||
Reference in New Issue
Block a user