mirror of
https://codeberg.org/andyscott/advent-of-code.git
synced 2024-12-22 01:23:11 -05:00
Compare commits
5 commits
ebaffe961f
...
1e3f01aef5
Author | SHA1 | Date | |
---|---|---|---|
1e3f01aef5 | |||
f65fc23fdd | |||
1d466f970c | |||
9f03226d18 | |||
56211245c6 |
6 changed files with 1100 additions and 2 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
.zig-cache/
|
||||
zig-out/
|
33
2024/build.zig
Normal file
33
2024/build.zig
Normal file
|
@ -0,0 +1,33 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const install_all = b.step("install-all", "Install all days");
|
||||
const run_all = b.step("run-all", "Run all days");
|
||||
const num_days = b.option(u32, "num-days", "The number of days to process") orelse 25;
|
||||
|
||||
var day: u8 = 1;
|
||||
while (day <= num_days) : (day += 1) {
|
||||
const str = b.fmt("day{d}", .{day});
|
||||
const file = b.fmt("{s}.zig", .{str});
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = str,
|
||||
.root_source_file = b.path(file),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
const install_exe = b.addInstallArtifact(exe, .{});
|
||||
const install_step = b.step(b.fmt("install_{s}", .{str}), b.fmt("Install {s}", .{str}));
|
||||
install_step.dependOn(&install_exe.step);
|
||||
install_all.dependOn(&install_exe.step);
|
||||
|
||||
const run_exe = b.addRunArtifact(exe);
|
||||
const run_step = b.step(b.fmt("run_{s}", .{str}), b.fmt("Run {s}", .{str}));
|
||||
run_step.dependOn(&run_exe.step);
|
||||
run_all.dependOn(&run_exe.step);
|
||||
}
|
||||
}
|
1000
2024/day1.txt
Normal file
1000
2024/day1.txt
Normal file
File diff suppressed because it is too large
Load diff
49
2024/day1.zig
Normal file
49
2024/day1.zig
Normal file
|
@ -0,0 +1,49 @@
|
|||
const std = @import("std");
|
||||
const print = std.debug.print;
|
||||
|
||||
pub fn main() !void {
|
||||
const file = try std.fs.cwd().openFile("day1.txt", .{});
|
||||
defer file.close();
|
||||
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer std.debug.assert(gpa.deinit() == .ok);
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const stat = try file.stat();
|
||||
const input = try file.reader().readAllAlloc(allocator, stat.size);
|
||||
defer allocator.free(input);
|
||||
|
||||
var l1 = std.ArrayList(i32).init(allocator);
|
||||
var l2 = std.ArrayList(i32).init(allocator);
|
||||
defer l1.deinit();
|
||||
defer l2.deinit();
|
||||
|
||||
var lines = std.mem.tokenizeScalar(u8, input, '\n');
|
||||
while (lines.next()) |line| {
|
||||
var split = std.mem.tokenizeScalar(u8, line, ' ');
|
||||
try l1.append(try std.fmt.parseInt(i32, split.next().?, 10));
|
||||
try l2.append(try std.fmt.parseInt(i32, split.next().?, 10));
|
||||
}
|
||||
|
||||
// PART 1
|
||||
std.mem.sort(i32, l1.items, {}, std.sort.asc(i32));
|
||||
std.mem.sort(i32, l2.items, {}, std.sort.asc(i32));
|
||||
|
||||
var distance: u32 = 0;
|
||||
for (l1.items, l2.items) |item1, item2| {
|
||||
distance += @abs(item1 - item2);
|
||||
}
|
||||
|
||||
// PART 2
|
||||
var simScore: i32 = 0;
|
||||
for (l1.items) |item1| {
|
||||
var count: i32 = 0;
|
||||
for (l2.items) |item2| {
|
||||
if (item1 == item2) count += 1;
|
||||
}
|
||||
simScore += item1 * count;
|
||||
}
|
||||
|
||||
print("PART 1 - DISTANCE: {d}\n", .{distance});
|
||||
print("PART 2 - SIMILARITY: {d}\n", .{simScore});
|
||||
}
|
11
2024/util.zig
Normal file
11
2024/util.zig
Normal file
|
@ -0,0 +1,11 @@
|
|||
const std = @import("std");
|
||||
const print = std.debug.print;
|
||||
|
||||
pub fn readAllInput(day: u8, allocator: std.mem.Allocator) ![]const u8 {
|
||||
var fileName: [16]u8 = undefined;
|
||||
const inputFile = try std.fmt.bufPrint(&fileName, "day{d}.txt", .{day});
|
||||
const file = try std.fs.cwd().openFile(inputFile, .{});
|
||||
defer file.close();
|
||||
const stat = try file.stat();
|
||||
return try file.reader().readAllAlloc(allocator, stat.size);
|
||||
}
|
|
@ -1,3 +1,6 @@
|
|||
# advent-of-code
|
||||
# Advent of Code
|
||||
|
||||
Casual attempts at Advent of Code
|
||||
> Advent of Code is an Advent calendar of small programming puzzles for a variety of skill levels that can be solved in any programming language you like. People use them as interview prep, company training, university coursework, practice problems, a speed contest, or to challenge each other.
|
||||
> -- [Eric Wastl](https://was.tl/) (Advent of Code creator)
|
||||
|
||||
My (casual) attempts at [Advent of Code](https://adventofcode.com). The solutions are released under the [*unlicense*](LICENSE). As such, they are public domain and you may ~~steal~~ use them in pretty much any manner you see fit.
|
||||
|
|
Loading…
Reference in a new issue