Day 1 and 2 :)
This commit is contained in:
Submodule AdventOfCode2015 updated: be9147d3f6...1498ea3784
2
AdventOfCode2023/.gitignore
vendored
Normal file
2
AdventOfCode2023/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
zig-cache
|
||||
zig-out
|
75
AdventOfCode2023/build.zig
Normal file
75
AdventOfCode2023/build.zig
Normal file
@ -0,0 +1,75 @@
|
||||
const std = @import("std");
|
||||
|
||||
// Although this function looks imperative, note that its job is to
|
||||
// declaratively construct a build graph that will be executed by an external
|
||||
// runner.
|
||||
pub fn build(b: *std.Build) void {
|
||||
// Standard target options allows the person running `zig build` to choose
|
||||
// what target to build for. Here we do not override the defaults, which
|
||||
// means any target is allowed, and the default is native. Other options
|
||||
// for restricting supported target set are available.
|
||||
const target = b.standardTargetOptions(.{});
|
||||
|
||||
// Standard optimization options allow the person running `zig build` to select
|
||||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
|
||||
// set a preferred release mode, allowing the user to decide how to optimize.
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "AdventOfCode2023",
|
||||
// In this case the main source file is merely a path, however, in more
|
||||
// complicated build scripts, this could be a generated file.
|
||||
.root_source_file = .{ .path = "src/main.zig" },
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
const utils_module = b.createModule(.{
|
||||
.source_file = .{ .path = "src/utils.zig" },
|
||||
});
|
||||
exe.addModule("utils", utils_module);
|
||||
|
||||
// This declares intent for the executable to be installed into the
|
||||
// standard location when the user invokes the "install" step (the default
|
||||
// step when running `zig build`).
|
||||
b.installArtifact(exe);
|
||||
|
||||
// This *creates* a Run step in the build graph, to be executed when another
|
||||
// step is evaluated that depends on it. The next line below will establish
|
||||
// such a dependency.
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
|
||||
// By making the run step depend on the install step, it will be run from the
|
||||
// installation directory rather than directly from within the cache directory.
|
||||
// This is not necessary, however, if the application depends on other installed
|
||||
// files, this ensures they will be present and in the expected location.
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
|
||||
// This allows the user to pass arguments to the application in the build
|
||||
// command itself, like this: `zig build run -- arg1 arg2 etc`
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
}
|
||||
|
||||
// This creates a build step. It will be visible in the `zig build --help` menu,
|
||||
// and can be selected like this: `zig build run`
|
||||
// This will evaluate the `run` step rather than the default, which is "install".
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
// Creates a step for unit testing. This only builds the test executable
|
||||
// but does not run it.
|
||||
const unit_tests = b.addTest(.{
|
||||
.root_source_file = .{ .path = "src/main.zig" },
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
const run_unit_tests = b.addRunArtifact(unit_tests);
|
||||
|
||||
// Similar to creating the run step earlier, this exposes a `test` step to
|
||||
// the `zig build --help` menu, providing a way for the user to request
|
||||
// running the unit tests.
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
test_step.dependOn(&run_unit_tests.step);
|
||||
}
|
168
AdventOfCode2023/src/day1/day1.zig
Normal file
168
AdventOfCode2023/src/day1/day1.zig
Normal file
@ -0,0 +1,168 @@
|
||||
const std = @import("std");
|
||||
const mem = std.mem;
|
||||
const ArrayList = std.ArrayList;
|
||||
|
||||
pub fn solve(input: [][]const u8) !void {
|
||||
var part1: usize = 0;
|
||||
|
||||
for (input) |line| {
|
||||
var firstDigit: usize = 0;
|
||||
for (line) |char| {
|
||||
if (char >= '0' and char <= '9') {
|
||||
firstDigit = char - '0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var lastDigit: usize = 0;
|
||||
for (line) |char| {
|
||||
if (char >= '0' and char <= '9') {
|
||||
lastDigit = char - '0';
|
||||
}
|
||||
}
|
||||
|
||||
part1 += firstDigit * 10 + lastDigit;
|
||||
}
|
||||
|
||||
std.debug.print("Part 1: {}\n", .{part1});
|
||||
|
||||
var part2: usize = 0;
|
||||
|
||||
for (input) |line| {
|
||||
var firstDigit: usize = 0;
|
||||
var index: usize = 0;
|
||||
|
||||
for (line) |char| {
|
||||
if (char >= '0' and char <= '9') {
|
||||
firstDigit = char - '0';
|
||||
break;
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
|
||||
if (std.mem.indexOf(u8, line, "one")) |position| {
|
||||
if (position < index) {
|
||||
index = position;
|
||||
firstDigit = 1;
|
||||
}
|
||||
}
|
||||
if (std.mem.indexOf(u8, line, "two")) |position| {
|
||||
if (position < index) {
|
||||
index = position;
|
||||
firstDigit = 2;
|
||||
}
|
||||
}
|
||||
if (std.mem.indexOf(u8, line, "three")) |position| {
|
||||
if (position < index) {
|
||||
index = position;
|
||||
firstDigit = 3;
|
||||
}
|
||||
}
|
||||
if (std.mem.indexOf(u8, line, "four")) |position| {
|
||||
if (position < index) {
|
||||
index = position;
|
||||
firstDigit = 4;
|
||||
}
|
||||
}
|
||||
if (std.mem.indexOf(u8, line, "five")) |position| {
|
||||
if (position < index) {
|
||||
index = position;
|
||||
firstDigit = 5;
|
||||
}
|
||||
}
|
||||
if (std.mem.indexOf(u8, line, "six")) |position| {
|
||||
if (position < index) {
|
||||
index = position;
|
||||
firstDigit = 6;
|
||||
}
|
||||
}
|
||||
if (std.mem.indexOf(u8, line, "seven")) |position| {
|
||||
if (position < index) {
|
||||
index = position;
|
||||
firstDigit = 7;
|
||||
}
|
||||
}
|
||||
if (std.mem.indexOf(u8, line, "eight")) |position| {
|
||||
if (position < index) {
|
||||
index = position;
|
||||
firstDigit = 8;
|
||||
}
|
||||
}
|
||||
if (std.mem.indexOf(u8, line, "nine")) |position| {
|
||||
if (position < index) {
|
||||
index = position;
|
||||
firstDigit = 9;
|
||||
}
|
||||
}
|
||||
|
||||
var lastDigit: usize = 0;
|
||||
var currentIndex: usize = 0;
|
||||
var indexLast: usize = 0;
|
||||
for (line) |char| {
|
||||
if (char >= '0' and char <= '9') {
|
||||
lastDigit = char - '0';
|
||||
indexLast = currentIndex;
|
||||
}
|
||||
currentIndex += 1;
|
||||
}
|
||||
|
||||
if (std.mem.lastIndexOf(u8, line, "one")) |position| {
|
||||
if (position > indexLast) {
|
||||
indexLast = position;
|
||||
lastDigit = 1;
|
||||
}
|
||||
}
|
||||
if (std.mem.lastIndexOf(u8, line, "two")) |position| {
|
||||
if (position > indexLast) {
|
||||
indexLast = position;
|
||||
lastDigit = 2;
|
||||
}
|
||||
}
|
||||
if (std.mem.lastIndexOf(u8, line, "three")) |position| {
|
||||
if (position > indexLast) {
|
||||
indexLast = position;
|
||||
lastDigit = 3;
|
||||
}
|
||||
}
|
||||
if (std.mem.lastIndexOf(u8, line, "four")) |position| {
|
||||
if (position > indexLast) {
|
||||
indexLast = position;
|
||||
lastDigit = 4;
|
||||
}
|
||||
}
|
||||
if (std.mem.lastIndexOf(u8, line, "five")) |position| {
|
||||
if (position > indexLast) {
|
||||
indexLast = position;
|
||||
lastDigit = 5;
|
||||
}
|
||||
}
|
||||
if (std.mem.lastIndexOf(u8, line, "six")) |position| {
|
||||
if (position > indexLast) {
|
||||
indexLast = position;
|
||||
lastDigit = 6;
|
||||
}
|
||||
}
|
||||
if (std.mem.lastIndexOf(u8, line, "seven")) |position| {
|
||||
if (position > indexLast) {
|
||||
indexLast = position;
|
||||
lastDigit = 7;
|
||||
}
|
||||
}
|
||||
if (std.mem.lastIndexOf(u8, line, "eight")) |position| {
|
||||
if (position > indexLast) {
|
||||
indexLast = position;
|
||||
lastDigit = 8;
|
||||
}
|
||||
}
|
||||
if (std.mem.lastIndexOf(u8, line, "nine")) |position| {
|
||||
if (position > indexLast) {
|
||||
indexLast = position;
|
||||
lastDigit = 9;
|
||||
}
|
||||
}
|
||||
|
||||
part2 += firstDigit * 10 + lastDigit;
|
||||
}
|
||||
|
||||
std.debug.print("Part 2: {}\n", .{part2});
|
||||
}
|
66
AdventOfCode2023/src/day2/day2.zig
Normal file
66
AdventOfCode2023/src/day2/day2.zig
Normal file
@ -0,0 +1,66 @@
|
||||
const std = @import("std");
|
||||
|
||||
const Rgb = struct {
|
||||
red: usize,
|
||||
green: usize,
|
||||
blue: usize,
|
||||
};
|
||||
|
||||
pub fn solve(input: [][]const u8) !void {
|
||||
const dices = Rgb{ .red = 12, .green = 13, .blue = 14 };
|
||||
var part1: usize = 0;
|
||||
var part2: usize = 0;
|
||||
|
||||
for (input) |line| {
|
||||
var splitIter = std.mem.splitSequence(u8, line, ": ");
|
||||
|
||||
var game = splitIter.next().?;
|
||||
var gameInput = splitIter.next().?;
|
||||
var gameNum = try std.fmt.parseInt(usize, game[5..], 10);
|
||||
|
||||
var gameSteps = std.mem.splitSequence(u8, gameInput, "; ");
|
||||
var part1Count = true;
|
||||
|
||||
var highestDice = Rgb{ .red = 0, .green = 0, .blue = 0 };
|
||||
|
||||
while (gameSteps.next()) |step| {
|
||||
var singleDice = std.mem.tokenizeAny(u8, step, ", ");
|
||||
var stepDices = Rgb{ .red = 0, .green = 0, .blue = 0 };
|
||||
|
||||
while (singleDice.next()) |dice| {
|
||||
var diceNum = try std.fmt.parseInt(usize, dice, 10);
|
||||
var colour = singleDice.next().?;
|
||||
if (std.mem.eql(u8, colour, "red")) {
|
||||
stepDices.red = diceNum;
|
||||
} else if (std.mem.eql(u8, colour, "green")) {
|
||||
stepDices.green = diceNum;
|
||||
} else if (std.mem.eql(u8, colour, "blue")) {
|
||||
stepDices.blue = diceNum;
|
||||
}
|
||||
}
|
||||
|
||||
if (part1Count and !(stepDices.red <= dices.red and stepDices.green <= dices.green and stepDices.blue <= dices.blue)) {
|
||||
part1Count = false;
|
||||
}
|
||||
|
||||
if (highestDice.red < stepDices.red) {
|
||||
highestDice.red = stepDices.red;
|
||||
}
|
||||
if (highestDice.green < stepDices.green) {
|
||||
highestDice.green = stepDices.green;
|
||||
}
|
||||
if (highestDice.blue < stepDices.blue) {
|
||||
highestDice.blue = stepDices.blue;
|
||||
}
|
||||
}
|
||||
|
||||
if (part1Count) {
|
||||
part1 += gameNum;
|
||||
}
|
||||
|
||||
part2 += highestDice.red * highestDice.green * highestDice.blue;
|
||||
}
|
||||
|
||||
std.debug.print("Part 1: {}\n", .{part1});
|
||||
std.debug.print("Part 2: {}\n", .{part2});
|
||||
}
|
12
AdventOfCode2023/src/main.zig
Normal file
12
AdventOfCode2023/src/main.zig
Normal file
@ -0,0 +1,12 @@
|
||||
const std = @import("std");
|
||||
// const day1 = @import("./day1/day1.zig");
|
||||
const day2 = @import("./day2/day2.zig");
|
||||
const utils = @import("utils.zig");
|
||||
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.page_allocator;
|
||||
var input = try utils.getInput("./src/day2/input.txt", allocator);
|
||||
defer allocator.free(input);
|
||||
|
||||
try day2.solve(input);
|
||||
}
|
20
AdventOfCode2023/src/utils.zig
Normal file
20
AdventOfCode2023/src/utils.zig
Normal file
@ -0,0 +1,20 @@
|
||||
const std = @import("std");
|
||||
const mem = std.mem;
|
||||
const ArrayList = std.ArrayList;
|
||||
|
||||
pub fn getInput(fileName: []const u8, allocator: mem.Allocator) !([][]const u8) {
|
||||
const file = try std.fs.cwd().openFile(fileName, .{});
|
||||
var content = try file.reader().readAllAlloc(allocator, 999999);
|
||||
|
||||
var lines = ArrayList([]const u8).init(allocator);
|
||||
defer lines.deinit();
|
||||
|
||||
var iterator = mem.tokenize(u8, content, "\n");
|
||||
|
||||
while (iterator.next()) |line| {
|
||||
try lines.append(line);
|
||||
}
|
||||
|
||||
var input = try lines.toOwnedSlice();
|
||||
return input;
|
||||
}
|
Reference in New Issue
Block a user