2022-02 in zig
This commit is contained in:
parent
e669eefcee
commit
4f69e038e1
4 changed files with 2560 additions and 0 deletions
3
2022/02-rock-paper-scissors/example
Normal file
3
2022/02-rock-paper-scissors/example
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
A Y
|
||||||
|
B X
|
||||||
|
C Z
|
25
2022/02-rock-paper-scissors/first.zig
Normal file
25
2022/02-rock-paper-scissors/first.zig
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const example = @embedFile("example");
|
||||||
|
const input = @embedFile("input");
|
||||||
|
|
||||||
|
pub fn main() anyerror!void {
|
||||||
|
try std.testing.expectEqual(solve(example), 15);
|
||||||
|
const result = try solve(input);
|
||||||
|
try std.io.getStdOut().writer().print("{}\n", .{result});
|
||||||
|
}
|
||||||
|
|
||||||
|
const scores = [3][3]u8{ // X Y Z
|
||||||
|
[3]u8{ 4, 8, 3 }, // A 4 8 3
|
||||||
|
[3]u8{ 1, 5, 9 }, // B 1 5 9
|
||||||
|
[3]u8{ 7, 2, 6 }, // C 7 2 6
|
||||||
|
};
|
||||||
|
|
||||||
|
fn solve(puzzle: []const u8) !u64 {
|
||||||
|
var it = std.mem.tokenize(u8, puzzle, "\n");
|
||||||
|
var tot: u64 = 0;
|
||||||
|
while (it.next()) |line| {
|
||||||
|
tot += scores[line[0] - 'A'][line[2] - 'X'];
|
||||||
|
}
|
||||||
|
return tot;
|
||||||
|
}
|
2500
2022/02-rock-paper-scissors/input
Normal file
2500
2022/02-rock-paper-scissors/input
Normal file
File diff suppressed because it is too large
Load diff
32
2022/02-rock-paper-scissors/second.zig
Normal file
32
2022/02-rock-paper-scissors/second.zig
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const example = @embedFile("example");
|
||||||
|
const input = @embedFile("input");
|
||||||
|
|
||||||
|
pub fn main() anyerror!void {
|
||||||
|
try std.testing.expectEqual(solve(example), 12);
|
||||||
|
const result = try solve(input);
|
||||||
|
try std.io.getStdOut().writer().print("{}\n", .{result});
|
||||||
|
}
|
||||||
|
|
||||||
|
const scores = [3][3]u8{ // X Y Z
|
||||||
|
[3]u8{ 4, 8, 3 }, // A 4 8 3
|
||||||
|
[3]u8{ 1, 5, 9 }, // B 1 5 9
|
||||||
|
[3]u8{ 7, 2, 6 }, // C 7 2 6
|
||||||
|
};
|
||||||
|
|
||||||
|
const strategy = [3][3]u8{ // X Y Z
|
||||||
|
[3]u8{ 2, 0, 1 }, // A 2 0 1
|
||||||
|
[3]u8{ 0, 1, 2 }, // B 0 1 2
|
||||||
|
[3]u8{ 1, 2, 0 }, // C 1 2 0
|
||||||
|
};
|
||||||
|
|
||||||
|
fn solve(puzzle: []const u8) !u64 {
|
||||||
|
var it = std.mem.tokenize(u8, puzzle, "\n");
|
||||||
|
var tot: u64 = 0;
|
||||||
|
while (it.next()) |line| {
|
||||||
|
const move = strategy[line[0] - 'A'][line[2] - 'X'];
|
||||||
|
tot += scores[line[0] - 'A'][move];
|
||||||
|
}
|
||||||
|
return tot;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue