Add '2019/' from commit 'fc21396bc86bc0f706225f4f6e1d8294d344ca53'

git-subtree-dir: 2019
git-subtree-mainline: f1be11fca8
git-subtree-split: fc21396bc8
This commit is contained in:
2022-01-09 17:07:24 +00:00
36 changed files with 2978 additions and 0 deletions

53
2019/04/src/main.zig Normal file
View File

@@ -0,0 +1,53 @@
const std = @import("std");
const first = 171309;
const last = 643603;
// - [x] It is a six-digit number (implied)
// - [x] The value is within the range given in your puzzle input (implied)
// - [x] Two adjacent digits are the same (like 22 in 122345).
// - [x] Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
inline fn tests1(num: i32, str: []u8) bool {
if (str[0] > str[1] or str[1] > str[2] or
str[2] > str[3] or str[3] > str[4] or
str[4] > str[5]) return false;
return str[0] == str[1] or str[1] == str[2] or
str[2] == str[3] or str[3] == str[4] or
str[4] == str[5];
}
inline fn tests2(num: i32, str: []u8) bool {
if (str[0] == str[1] and str[1] != str[2]) return true; // [0 1]2 3 4 5
if (str[1] == str[2] and str[0] != str[1] and str[2] != str[3]) return true; // 0[1 2]3 4 5
if (str[2] == str[3] and str[1] != str[2] and str[3] != str[4]) return true; // 0 1[2 3]4 5
if (str[3] == str[4] and str[2] != str[3] and str[4] != str[5]) return true; // 0 1 2[3 4]5
if (str[4] == str[5] and str[3] != str[4]) return true; // 0 1 2 3[4 5]
return false;
}
pub fn main() anyerror!void {
var count1: i32 = 0;
var count2: i32 = 0;
var i: i32 = first;
while (i <= last) : (i += 1) {
var buf: [6]u8 = undefined;
var numStr = try std.fmt.bufPrint(&buf, "{}", i);
if (tests1(i, numStr)) {
count1 += 1;
if (tests2(i, numStr)) {
count2 += 1;
}
}
}
//std.debug.warn(" 012345\n");
std.debug.warn("Part 1: matches: {}\n", count1);
std.debug.warn("Part 2: matches: {}\n", count2);
}