day 1 - Part02
This commit is contained in:
parent
5fad9922cf
commit
25c51154bb
3 changed files with 20487 additions and 10 deletions
8
src/data/day01.test.txt
Normal file
8
src/data/day01.test.txt
Normal file
|
@ -0,0 +1,8 @@
|
|||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
||||
|
179
src/day01.zig
179
src/day01.zig
|
@ -8,13 +8,53 @@ const BitSet = std.DynamicBitSet;
|
|||
const util = @import("util.zig");
|
||||
const gpa = util.gpa;
|
||||
|
||||
// const data = @embedFile("data/day01.test.txt");
|
||||
const data = @embedFile("data/day01.txt");
|
||||
const data2 = @embedFile("data/day01_part2.txt");
|
||||
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.page_allocator;
|
||||
|
||||
try part01(allocator, data);
|
||||
const part01_total = try part01(allocator, data);
|
||||
const part02_total = try part02(allocator, data);
|
||||
std.debug.print("Part01: {d}\n", .{part01_total});
|
||||
std.debug.print("Part02: {d}\n", .{part02_total});
|
||||
}
|
||||
|
||||
fn replace_number_with_digit(allocator: std.mem.Allocator, word: []const u8) ![]u8 {
|
||||
var size = std.mem.replacementSize(u8, word, "one", "1");
|
||||
const output = try allocator.alloc(u8, size);
|
||||
_ = std.mem.replace(u8, word, "one", "1", output);
|
||||
|
||||
size = std.mem.replacementSize(u8, output, "two", "2");
|
||||
const output2 = try allocator.alloc(u8, size);
|
||||
_ = std.mem.replace(u8, output, "two", "2", output2);
|
||||
|
||||
size = std.mem.replacementSize(u8, output2, "three", "3");
|
||||
const output3 = try allocator.alloc(u8, size);
|
||||
_ = std.mem.replace(u8, output2, "three", "3", output3);
|
||||
|
||||
size = std.mem.replacementSize(u8, output3, "four", "4");
|
||||
const output4 = try allocator.alloc(u8, size);
|
||||
_ = std.mem.replace(u8, output3, "four", "4", output4);
|
||||
size = std.mem.replacementSize(u8, output4, "five", "5");
|
||||
const output5 = try allocator.alloc(u8, size);
|
||||
_ = std.mem.replace(u8, output4, "five", "5", output5);
|
||||
size = std.mem.replacementSize(u8, output5, "six", "6");
|
||||
const output6 = try allocator.alloc(u8, size);
|
||||
_ = std.mem.replace(u8, output5, "six", "6", output6);
|
||||
size = std.mem.replacementSize(u8, output6, "seven", "7");
|
||||
const output7 = try allocator.alloc(u8, size);
|
||||
_ = std.mem.replace(u8, output6, "seven", "7", output7);
|
||||
size = std.mem.replacementSize(u8, output7, "eight", "8");
|
||||
const output8 = try allocator.alloc(u8, size);
|
||||
_ = std.mem.replace(u8, output7, "eight", "8", output8);
|
||||
size = std.mem.replacementSize(u8, output8, "nine", "9");
|
||||
const output9 = try allocator.alloc(u8, size);
|
||||
_ = std.mem.replace(u8, output8, "nine", "9", output9);
|
||||
|
||||
// std.debug.print("REPLACED {s}\n", .{output9});
|
||||
return try allocator.dupe(u8, output9);
|
||||
}
|
||||
|
||||
fn find_first_digit_number(word: []const u8) !u8 {
|
||||
|
@ -26,23 +66,115 @@ fn find_first_digit_number(word: []const u8) !u8 {
|
|||
}
|
||||
}
|
||||
|
||||
return word[i];
|
||||
return '0';
|
||||
}
|
||||
|
||||
fn find_first_digit_number_or_string(word: []const u8) !u32 {
|
||||
const numbers = [_][]const u8{ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
|
||||
|
||||
var digit: u32 = 0;
|
||||
|
||||
var i: usize = 0;
|
||||
var digit_i: usize = 10;
|
||||
|
||||
while (i < word.len) : (i += 1) {
|
||||
if (std.ascii.isDigit(word[i])) {
|
||||
digit = try std.fmt.charToDigit(word[i], 10);
|
||||
digit_i = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (numbers, 0..) |n, j| {
|
||||
const index = std.mem.indexOf(u8, word, n) orelse word.len;
|
||||
if (index < digit_i) {
|
||||
digit_i = index;
|
||||
digit = @intCast(j);
|
||||
}
|
||||
}
|
||||
|
||||
return digit;
|
||||
}
|
||||
|
||||
fn find_last_digit_number_or_string(word: []const u8) !u32 {
|
||||
const numbers = [_][]const u8{ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
|
||||
|
||||
var digit: u32 = 0;
|
||||
|
||||
var i: usize = word.len - 1;
|
||||
var digit_i: usize = word.len;
|
||||
|
||||
while (i >= 0) : (i -= 1) {
|
||||
// std.debug.print("word[{d}]={c}\n", .{ i, word[i] });
|
||||
if (std.ascii.isDigit(word[i])) {
|
||||
digit = try std.fmt.charToDigit(word[i], 10);
|
||||
digit_i = i;
|
||||
break;
|
||||
}
|
||||
if (i == 0) {
|
||||
digit_i = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (numbers, 0..) |n, j| {
|
||||
// std.debug.print("checking for word {s}\n", .{n});
|
||||
const index = std.mem.lastIndexOf(u8, word, n) orelse 0;
|
||||
if (index > digit_i) {
|
||||
digit_i = index;
|
||||
// std.debug.print("found word {s} {d}\n", .{ n, index });
|
||||
digit = @intCast(j);
|
||||
}
|
||||
}
|
||||
|
||||
return digit;
|
||||
}
|
||||
fn find_last_digit_number(word: []const u8) !u8 {
|
||||
var i: usize = word.len - 1;
|
||||
|
||||
while (i < word.len) : (i -= 1) {
|
||||
while (i >= 0) : (i -= 1) {
|
||||
if (std.ascii.isDigit(word[i])) {
|
||||
std.debug.print("last {}\n", .{word[i]});
|
||||
// std.debug.print("last {c}\n", .{word[i]});
|
||||
return word[i];
|
||||
}
|
||||
}
|
||||
|
||||
return word[i];
|
||||
return '0';
|
||||
}
|
||||
|
||||
fn part01(allocator: std.mem.Allocator, day01: []const u8) !void {
|
||||
test "validate part01" {
|
||||
const test_data =
|
||||
\\1abc2
|
||||
\\pqr3stu8vwx
|
||||
\\a1b2c3d4e5f
|
||||
\\treb7uchet
|
||||
;
|
||||
|
||||
const expectedValue: u32 = 142;
|
||||
const part01_total = try part01(std.testing.allocator, test_data);
|
||||
|
||||
try std.testing.expectEqual(expectedValue, part01_total);
|
||||
}
|
||||
|
||||
test "validate part02" {
|
||||
const test_data =
|
||||
\\two1nine
|
||||
\\eightwothree
|
||||
\\abcone2threexyz
|
||||
\\xtwone3four
|
||||
\\4nineeightseven2
|
||||
\\zoneight234
|
||||
\\oneeighttwo34dcjck5eightjznpzhxdlc
|
||||
\\7pqrstsixteen
|
||||
;
|
||||
|
||||
const expected_value: u32 = 299;
|
||||
|
||||
const part02_total = try part02(std.testing.allocator, test_data);
|
||||
|
||||
try std.testing.expectEqual(expected_value, part02_total);
|
||||
}
|
||||
|
||||
fn part01(allocator: std.mem.Allocator, day01: []const u8) !u32 {
|
||||
var splits = std.mem.split(u8, day01, "\n");
|
||||
var total: u32 = 0;
|
||||
while (splits.next()) |line| {
|
||||
|
@ -50,19 +182,46 @@ fn part01(allocator: std.mem.Allocator, day01: []const u8) !void {
|
|||
continue;
|
||||
}
|
||||
|
||||
std.debug.print("Line: {s}\n", .{line});
|
||||
// std.debug.print("Line: {s}\n", .{line});
|
||||
const first_digit_char = try find_first_digit_number(line);
|
||||
const last_digit_char = try find_last_digit_number(line);
|
||||
|
||||
const first_and_last = try std.fmt.allocPrint(allocator, "{c}{c}", .{ first_digit_char, last_digit_char });
|
||||
defer allocator.free(first_and_last);
|
||||
|
||||
std.debug.print("{c} + {c}\n", .{ first_digit_char, last_digit_char });
|
||||
// std.debug.print("{c} + {c}\n", .{ first_digit_char, last_digit_char });
|
||||
var digits: u32 = 0;
|
||||
digits = try std.fmt.parseInt(u32, first_and_last, 10);
|
||||
total += digits;
|
||||
}
|
||||
|
||||
std.debug.print("First Total: {d}\n", .{total});
|
||||
return total;
|
||||
// std.debug.print("First Total: {d}\n", .{total});
|
||||
}
|
||||
|
||||
fn part02(allocator: std.mem.Allocator, day01: []const u8) !u32 {
|
||||
var splits = std.mem.split(u8, day01, "\n");
|
||||
var total: u32 = 0;
|
||||
while (splits.next()) |line| {
|
||||
if (line.len <= 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// std.debug.print("line: {s}\n", .{line});
|
||||
const first_digit_char = try find_first_digit_number_or_string(line);
|
||||
const last_digit_char = try find_last_digit_number_or_string(line);
|
||||
|
||||
const first_and_last = try std.fmt.allocPrint(allocator, "{d}{d}", .{ first_digit_char, last_digit_char });
|
||||
defer allocator.free(first_and_last);
|
||||
|
||||
// std.debug.print("{d} + {d}\n", .{ first_digit_char, last_digit_char });
|
||||
var digits: u32 = 0;
|
||||
digits = try std.fmt.parseInt(u32, first_and_last, 10);
|
||||
// std.debug.print("{d} + {d}\n", .{ total, digits });
|
||||
total += digits;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
// Useful stdlib functions
|
||||
|
|
Loading…
Reference in a new issue