Day 4.2
This commit is contained in:
@@ -8,35 +8,47 @@ const last = 643603;
|
|||||||
// - [x] Two adjacent digits are the same (like 22 in 122345).
|
// - [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).
|
// - [x] Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
|
||||||
|
|
||||||
fn tests1(num: i32) anyerror!bool {
|
fn tests1(num: i32, str: []u8) bool {
|
||||||
var buf: [6]u8 = undefined;
|
if (str[0] > str[1] or str[1] > str[2] or
|
||||||
var numStr = try std.fmt.bufPrint(&buf, "{}", num);
|
str[2] > str[3] or str[3] > str[4] or
|
||||||
|
str[4] > str[5]) return false;
|
||||||
|
|
||||||
if (numStr[0] > numStr[1] or
|
return str[0] == str[1] or str[1] == str[2] or
|
||||||
numStr[1] > numStr[2] or
|
str[2] == str[3] or str[3] == str[4] or
|
||||||
numStr[2] > numStr[3] or
|
str[4] == str[5];
|
||||||
numStr[3] > numStr[4] or
|
}
|
||||||
numStr[4] > numStr[5]
|
|
||||||
) return false;
|
|
||||||
|
|
||||||
return
|
fn tests2(num: i32, str: []u8) bool {
|
||||||
numStr[0] == numStr[1] or
|
if (str[0] == str[1] and str[1] != str[2]) return true; // [0 1]2 3 4 5
|
||||||
numStr[1] == numStr[2] or
|
if (str[1] == str[2] and str[0] != str[1] and str[2] != str[3]) return true; // 0[1 2]3 4 5
|
||||||
numStr[2] == numStr[3] or
|
if (str[2] == str[3] and str[1] != str[2] and str[3] != str[4]) return true; // 0 1[2 3]4 5
|
||||||
numStr[3] == numStr[4] or
|
if (str[3] == str[4] and str[2] != str[3] and str[4] != str[5]) return true; // 0 1 2[3 4]5
|
||||||
numStr[4] == numStr[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 {
|
pub fn main() anyerror!void {
|
||||||
var count: i32 = 0;
|
var count1: i32 = 0;
|
||||||
|
var count2: i32 = 0;
|
||||||
var i: i32 = first;
|
var i: i32 = first;
|
||||||
|
|
||||||
while (i <= last) : (i+=1) {
|
while (i <= last) : (i += 1) {
|
||||||
if (try tests1(i)) {
|
var buf: [6]u8 = undefined;
|
||||||
//std.debug.warn("Match: {}\n", i);
|
var numStr = try std.fmt.bufPrint(&buf, "{}", i);
|
||||||
count+=1;
|
|
||||||
|
if (tests1(i, numStr)) {
|
||||||
|
count1 += 1;
|
||||||
|
|
||||||
|
if (tests2(i, numStr)) {
|
||||||
|
std.debug.warn("Match: {}\n", i);
|
||||||
|
count2 += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std.debug.warn("Part 1: matches: {}\n", count);
|
std.debug.warn(" 012345\n");
|
||||||
|
|
||||||
|
std.debug.warn("Part 1: matches: {}\n", count1);
|
||||||
|
std.debug.warn("Part 2: matches: {}\n", count2);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user