attemps from before
commit
680da5cc2c
@ -0,0 +1,6 @@
|
||||
int main(){
|
||||
var input = File.ReadAllText(input1);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,99 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn main() anyerror!void{
|
||||
// fuck it, inline time
|
||||
comptime const numbers: [200]u32 = .{
|
||||
1695,1157,1484,1717,622,
|
||||
1513,1924,63,1461,1971,
|
||||
1382,1587,1913,1665,1464,
|
||||
1914,1637,1527,1424,1361,
|
||||
1187,272,1909,1448,1623,
|
||||
1164,1931,1646,1096,1655,
|
||||
1962,1961,1694,1792,1989,
|
||||
1616,138,1887,1357,1965,
|
||||
1085,308,2007,1254,1179,
|
||||
1124,1719,1467,1928,1630,
|
||||
1676,1359,1241,1511,1413,
|
||||
1656,1818,1919,1422,1745,
|
||||
1208,1609,1544,1775,1154,
|
||||
1057,1440,1242,1202,1266,
|
||||
1305,1836,1760,1730,1396,
|
||||
1315,1496,1964,1300,1195,
|
||||
1583,1607,1743,1682,1453,
|
||||
1848,1320,1601,954,1473,
|
||||
1847,1486,1853,1668,1342,
|
||||
1087,1139,1349,1568,1728,
|
||||
1420,1233,1073,1376,1658,
|
||||
1477,1871,1958,1950,1503,
|
||||
1758,1474,1203,1336,1981,
|
||||
1309,1618,1846,1974,1940,
|
||||
1333,1119,1756,1918,961,
|
||||
1307,1375,1346,1611,1284,
|
||||
84,1754,1608,2010,1341,
|
||||
1136,1218,1882,1911,1288,
|
||||
1930,1749,1952,1556,1757,
|
||||
1761,1112,1963,1186,1373,
|
||||
1622,1973,1330,1508,1222,
|
||||
1226,1389,1679,1584,1237,
|
||||
1563,1763,1998,1293,1642,
|
||||
95,1661,1674,1100,1262,
|
||||
1895,1548,1400,1205,1435,
|
||||
1156,1034,1577,1701,1198,
|
||||
1173,1500,1858,1809,1780,
|
||||
1412,1982,1070,1523,1776,
|
||||
1598,1113,1144,1777,1313,
|
||||
1102,1999,1405,1784,1196,
|
||||
};
|
||||
|
||||
var task1timer = try std.time.Timer.start();
|
||||
var task1answer:u32 = task1(numbers);
|
||||
var task1time = task1timer.read();
|
||||
std.debug.print("T1 answer found in : {0}ns \n", .{task1time});
|
||||
std.debug.print("T1 answer is: {0} \n", .{task1answer});
|
||||
|
||||
std.debug.print("\n",.{});
|
||||
|
||||
var task2timer = try std.time.Timer.start();
|
||||
var task2answer:u32 = task2(numbers);
|
||||
var task2time = task2timer.read();
|
||||
std.debug.print("T2 answer found in : {0}ns \n", .{task2time});
|
||||
std.debug.print("T2 answer is: {0} \n", .{task2answer});
|
||||
}
|
||||
|
||||
pub fn task2(numbers: [200]u32)u32{
|
||||
for(numbers)|num1|{
|
||||
for(numbers)|num2|{
|
||||
if(num1+num2 > 2020){
|
||||
continue;
|
||||
}
|
||||
var num3 = (2020 - (num1+num2));
|
||||
if(isInArray(num3, &numbers)){
|
||||
return num3 * num2 * num1;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn task1(numbers: [200]u32)u32{
|
||||
for(numbers)|num|{
|
||||
var num1 = 2020 - num;
|
||||
if(isInArray(num1, &numbers)){
|
||||
return num1*num;
|
||||
}
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn isInArray(num: u32, arr: *const [200]u32)bool{
|
||||
for(arr)|checkee|{
|
||||
if(checkee == num){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
const std = @import("std");
|
||||
const warn = std.debug.warn;
|
||||
|
||||
pub fn main() anyerror!void{
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
const allocator = &arena.allocator;
|
||||
|
||||
//read data from file.
|
||||
//each line is one number
|
||||
var file = try std.fs.cwd().openFile("input1", .{});
|
||||
defer file.close();
|
||||
|
||||
const file_size:u64 = try file.getEndPos();
|
||||
std.debug.print("{0} \n", .{file_size});
|
||||
|
||||
var buffer = try allocator.alloc(u8, file_size);
|
||||
var reader = file.reader();
|
||||
|
||||
var size = try reader.read(buffer);
|
||||
|
||||
std.debug.print("{0} \n", .{buffer});
|
||||
|
||||
//read from buffer
|
||||
//put value in array
|
||||
//is value 10?
|
||||
//finish array, interpret as int32.
|
||||
const numberArr: []u32 = try allocator.alloc(u32, 4);
|
||||
var i:i32 = 0;
|
||||
|
||||
for(buffer) |byte|{
|
||||
if(byte == 10){
|
||||
i=0;
|
||||
//std.debug.print("{0}", .{@as(i32,numberArr)});
|
||||
|
||||
}else{
|
||||
//numberArr.push(byte);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
// for(buffer) |byte|{
|
||||
// if(byte == 10){
|
||||
// std.debug.print("Newline!\n", .{});
|
||||
// }else{
|
||||
// std.debug.print("{0}", .{byte});
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
//what two numbers from the input file add up to 2020?
|
||||
//once those two numbers have been found, multiply them
|
||||
|
||||
|
||||
//naive:
|
||||
//foreach number |num1|
|
||||
//foreach number |num2|
|
||||
//if(num1+num2 = 2020)
|
||||
//return num1*num2
|
||||
|
||||
//v2
|
||||
//foreach number |num1|
|
||||
// if(number.contains(2020-num1))
|
||||
// return num1*(num2;
|
||||
}
|
||||
@ -0,0 +1,200 @@
|
||||
1695
|
||||
1157
|
||||
1484
|
||||
1717
|
||||
622
|
||||
1513
|
||||
1924
|
||||
63
|
||||
1461
|
||||
1971
|
||||
1382
|
||||
1587
|
||||
1913
|
||||
1665
|
||||
1464
|
||||
1914
|
||||
1637
|
||||
1527
|
||||
1424
|
||||
1361
|
||||
1187
|
||||
272
|
||||
1909
|
||||
1448
|
||||
1623
|
||||
1164
|
||||
1931
|
||||
1646
|
||||
1096
|
||||
1655
|
||||
1962
|
||||
1961
|
||||
1694
|
||||
1792
|
||||
1989
|
||||
1616
|
||||
138
|
||||
1887
|
||||
1357
|
||||
1965
|
||||
1085
|
||||
308
|
||||
2007
|
||||
1254
|
||||
1179
|
||||
1124
|
||||
1719
|
||||
1467
|
||||
1928
|
||||
1630
|
||||
1676
|
||||
1359
|
||||
1241
|
||||
1511
|
||||
1413
|
||||
1656
|
||||
1818
|
||||
1919
|
||||
1422
|
||||
1745
|
||||
1208
|
||||
1609
|
||||
1544
|
||||
1775
|
||||
1154
|
||||
1057
|
||||
1440
|
||||
1242
|
||||
1202
|
||||
1266
|
||||
1305
|
||||
1836
|
||||
1760
|
||||
1730
|
||||
1396
|
||||
1315
|
||||
1496
|
||||
1964
|
||||
1300
|
||||
1195
|
||||
1583
|
||||
1607
|
||||
1743
|
||||
1682
|
||||
1453
|
||||
1848
|
||||
1320
|
||||
1601
|
||||
954
|
||||
1473
|
||||
1847
|
||||
1486
|
||||
1853
|
||||
1668
|
||||
1342
|
||||
1087
|
||||
1139
|
||||
1349
|
||||
1568
|
||||
1728
|
||||
1420
|
||||
1233
|
||||
1073
|
||||
1376
|
||||
1658
|
||||
1477
|
||||
1871
|
||||
1958
|
||||
1950
|
||||
1503
|
||||
1758
|
||||
1474
|
||||
1203
|
||||
1336
|
||||
1981
|
||||
1309
|
||||
1618
|
||||
1846
|
||||
1974
|
||||
1940
|
||||
1333
|
||||
1119
|
||||
1756
|
||||
1918
|
||||
961
|
||||
1307
|
||||
1375
|
||||
1346
|
||||
1611
|
||||
1284
|
||||
84
|
||||
1754
|
||||
1608
|
||||
2010
|
||||
1341
|
||||
1136
|
||||
1218
|
||||
1882
|
||||
1911
|
||||
1288
|
||||
1930
|
||||
1749
|
||||
1952
|
||||
1556
|
||||
1757
|
||||
1761
|
||||
1112
|
||||
1963
|
||||
1186
|
||||
1373
|
||||
1622
|
||||
1973
|
||||
1330
|
||||
1508
|
||||
1222
|
||||
1226
|
||||
1389
|
||||
1679
|
||||
1584
|
||||
1237
|
||||
1563
|
||||
1763
|
||||
1998
|
||||
1293
|
||||
1642
|
||||
95
|
||||
1661
|
||||
1674
|
||||
1100
|
||||
1262
|
||||
1895
|
||||
1548
|
||||
1400
|
||||
1205
|
||||
1435
|
||||
1156
|
||||
1034
|
||||
1577
|
||||
1701
|
||||
1198
|
||||
1173
|
||||
1500
|
||||
1858
|
||||
1809
|
||||
1780
|
||||
1412
|
||||
1982
|
||||
1070
|
||||
1523
|
||||
1776
|
||||
1598
|
||||
1113
|
||||
1144
|
||||
1777
|
||||
1313
|
||||
1102
|
||||
1999
|
||||
1405
|
||||
1784
|
||||
1196
|
||||
@ -0,0 +1,73 @@
|
||||
const std = @import("std");
|
||||
|
||||
|
||||
pub fn main() anyerror!void{
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
const allocator = &arena.allocator;
|
||||
|
||||
//format:
|
||||
//[d-d] [c]: [password]
|
||||
//[minamount [sperator] maxamount] [character]: [password]
|
||||
|
||||
var file = try std.fs.cwd().openFile("input2", .{});
|
||||
defer file.close();
|
||||
|
||||
const file_size:u64 = try file.getEndPos();
|
||||
std.debug.print("{0} \n", .{file_size});
|
||||
|
||||
//var buffer = try allocator.alloc(u8, file_size);
|
||||
//var reader = file.reader();
|
||||
|
||||
//var size = try reader.read(buffer);
|
||||
|
||||
//std.debug.print("{0} \n", .{buffer});
|
||||
try file.seekTo(0);
|
||||
|
||||
const contents = try file.reader().readAllAlloc(
|
||||
allocator,
|
||||
file_size
|
||||
);
|
||||
defer allocator.free(contents);
|
||||
|
||||
// const passBuffer = try allocator.alloc(u8, 40); //get a 40 char buffer
|
||||
// defer allocator.free(passBuffer);
|
||||
|
||||
const ArrayList = std.ArrayList;
|
||||
var list = ArrayList(u8).init(allocator);
|
||||
defer list.deinit();
|
||||
|
||||
for(contents)|char,index|{
|
||||
if(char == 12){
|
||||
std.debug.print("\n", .{});
|
||||
//end of line, so clear everything
|
||||
|
||||
continue;
|
||||
}
|
||||
if(char == 45){
|
||||
//current char is (-), so we take the input so far
|
||||
//and set it as the min
|
||||
for(list)|didget|{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
list.append(char);
|
||||
|
||||
//put byte in buffer
|
||||
//is current byte a dash (-)?
|
||||
//yes, then this is min
|
||||
//read untill space
|
||||
//from (-) until the space, is the max
|
||||
|
||||
std.debug.print("{c}", .{char});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isPassValid(char:u8, amountMin:i32, amountMax:i32, input: []u8 )bool{
|
||||
//in here check if password is valid
|
||||
return false;
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@ -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);
|
||||
}
|
||||
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));
|
||||
}
|
||||
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);
|
||||
}
|
||||
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
|
||||
Loading…
Reference in New Issue