Day 1 and 2 :)
This commit is contained in:
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