attemps from before
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
const input = @embedFile("input1");
|
||||
const std = @import("std");
|
||||
|
||||
pub fn main() !void{
|
||||
var increments: usize = 0;
|
||||
var t = std.mem.tokenize(u8, input, "\n");
|
||||
|
||||
var window = .{
|
||||
try std.fmt.parseInt(usize, t.next().?, 10),
|
||||
try std.fmt.parseInt(usize, t.next().?, 10),
|
||||
try std.fmt.parseInt(usize, t.next().?, 10),
|
||||
};
|
||||
|
||||
while(t.next())|line|{
|
||||
var sum1: usize = window[0] + window[1] + window[2];
|
||||
|
||||
window[0] = window[1];
|
||||
window[1] = window[2];
|
||||
window[2] = try std.fmt.parseInt(usize, line, 10);
|
||||
var sum2: usize = window[0] + window[1] + window[2];
|
||||
|
||||
if ( sum1 < sum2) increments += 1;
|
||||
|
||||
}
|
||||
|
||||
std.debug.print("result: {}", .{increments});
|
||||
}
|
||||
|
||||
|
||||
|
||||
test "part1" {
|
||||
var t = std.mem.tokenize(u8, input, "\n");
|
||||
|
||||
var last: usize = std.math.maxInt(usize);
|
||||
var increments: usize = 0;
|
||||
|
||||
while(t.next()) |line| {
|
||||
const n = try std.fmt.parseInt(usize, line, 10);
|
||||
defer last = n;
|
||||
if ( n > last) increments += 1;
|
||||
}
|
||||
|
||||
try std.testing.expect(1583 == increments);
|
||||
|
||||
}
|
||||
|
||||
test "part2"{
|
||||
var increments: usize = 0;
|
||||
var t = std.mem.tokenize(u8, input, "\n");
|
||||
|
||||
// Read the first window
|
||||
var window = .{
|
||||
try std.fmt.parseInt(usize, t.next().?, 10),
|
||||
try std.fmt.parseInt(usize, t.next().?, 10),
|
||||
try std.fmt.parseInt(usize, t.next().?, 10),
|
||||
};
|
||||
|
||||
while(t.next())|line|{
|
||||
//with this window, calculate the current window sum
|
||||
var sum1: usize = window[0] + window[1] + window[2];
|
||||
|
||||
//slide window one over
|
||||
window[0] = window[1];
|
||||
window[1] = window[2];
|
||||
window[2] = try std.fmt.parseInt(usize, line, 10);
|
||||
|
||||
var sum2: usize = window[0] + window[1] + window[2];
|
||||
|
||||
if ( sum1 < sum2){
|
||||
increments += 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std.testing.expect(1627 == increments);
|
||||
}
|
||||
+2000
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,8 @@
|
||||
607
|
||||
618
|
||||
618
|
||||
617
|
||||
647
|
||||
716
|
||||
769
|
||||
792
|
||||
@@ -0,0 +1,70 @@
|
||||
const input = @embedFile("input2");
|
||||
const std = @import("std");
|
||||
|
||||
pub fn main() !void{
|
||||
|
||||
var horizontal: usize = 0;
|
||||
var depth: usize = 0;
|
||||
var aim: usize = 0;
|
||||
|
||||
var iter = std.mem.tokenize(u8, input, "\n "); // You can split on 2 delimeters, by adding them to the string
|
||||
|
||||
while(iter.next())|direction|{
|
||||
var value = try std.fmt.parseInt(usize, iter.next().?, 10);
|
||||
|
||||
if(std.mem.eql(u8, direction, "up")){ aim -= value; }
|
||||
if(std.mem.eql(u8, direction, "down")){ aim += value; }
|
||||
if(std.mem.eql(u8, direction, "forward")){
|
||||
horizontal += value;
|
||||
depth += (aim * value);
|
||||
}
|
||||
}
|
||||
|
||||
std.debug.print("result: {}", .{depth * horizontal});
|
||||
}
|
||||
|
||||
test "Part 1"{
|
||||
|
||||
var horizontal: usize = 0;
|
||||
var depth: usize = 0;
|
||||
|
||||
var iter = std.mem.tokenize(u8, input, "\n "); // You can split on 2 delimeters, by adding them to the string
|
||||
|
||||
while(iter.next())|direction|{
|
||||
var value = try std.fmt.parseInt(usize, iter.next().?, 10);
|
||||
|
||||
if(std.mem.eql(u8, direction, "forward")){ horizontal += value; }
|
||||
if(std.mem.eql(u8, direction, "up")){ depth -= value; }
|
||||
if(std.mem.eql(u8, direction, "down")){ depth += value; }
|
||||
|
||||
}
|
||||
|
||||
|
||||
std.testing.expect(1762050 == (depth * horizontal));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
test "part 2"{
|
||||
|
||||
var horizontal: usize = 0;
|
||||
var depth: usize = 0;
|
||||
var aim: usize = 0;
|
||||
|
||||
var iter = std.mem.tokenize(u8, input, "\n "); // You can split on 2 delimeters, by adding them to the string
|
||||
|
||||
//First token is the direction
|
||||
while(iter.next())|direction|{
|
||||
var value = try std.fmt.parseInt(usize, iter.next().?, 10);
|
||||
|
||||
if(std.mem.eql(u8, direction, "up")){ aim -= value; }
|
||||
if(std.mem.eql(u8, direction, "down")){ aim += value; }
|
||||
if(std.mem.eql(u8, direction, "forward")){
|
||||
horizontal += value;
|
||||
depth += (aim * value);
|
||||
}
|
||||
}
|
||||
|
||||
std.testing.expect(1855892637 == (depth * horizontal));
|
||||
}
|
||||
+1000
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,6 @@
|
||||
forward 5
|
||||
down 5
|
||||
forward 8
|
||||
up 3
|
||||
down 8
|
||||
forward 2
|
||||
@@ -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