Add solution for 2023 day 01
This commit is contained in:
parent
659db82f54
commit
8386cf2f66
60
2023/README.md
Normal file
60
2023/README.md
Normal file
@ -0,0 +1,60 @@
|
||||
# [Advent of Code 2023](https://adventofcode.com/2023)
|
||||
|
||||
## Progress
|
||||
|
||||
| Day | Part 1 | Part 2 | Language |
|
||||
| :--: | :----: | :----: | :------- |
|
||||
| [01] | ✓ | | [Zig] |
|
||||
| [02] | | | |
|
||||
| [03] | | | |
|
||||
| [04] | | | |
|
||||
| [05] | | | |
|
||||
| [06] | | | |
|
||||
| [07] | | | |
|
||||
| [08] | | | |
|
||||
| [09] | | | |
|
||||
| [10] | | | |
|
||||
| [11] | | | |
|
||||
| [12] | | | |
|
||||
| [13] | | | |
|
||||
| [14] | | | |
|
||||
| [15] | | | |
|
||||
| [16] | | | |
|
||||
| [17] | | | |
|
||||
| [18] | | | |
|
||||
| [19] | | | |
|
||||
| [20] | | | |
|
||||
| [21] | | | |
|
||||
| [22] | | | |
|
||||
| [23] | | | |
|
||||
| [24] | | | |
|
||||
| [25] | | | |
|
||||
|
||||
<!-- links -->
|
||||
|
||||
[Zig]: https://ziglang.org
|
||||
[01]: day-01/
|
||||
[02]: day-02/
|
||||
[03]: day-03/
|
||||
[04]: day-04/
|
||||
[05]: day-05/
|
||||
[06]: day-06/
|
||||
[07]: day-07/
|
||||
[08]: day-08/
|
||||
[09]: day-09/
|
||||
[10]: day-10/
|
||||
[11]: day-11/
|
||||
[12]: day-12/
|
||||
[13]: day-13/
|
||||
[14]: day-14/
|
||||
[15]: day-15/
|
||||
[16]: day-16/
|
||||
[17]: day-17/
|
||||
[18]: day-18/
|
||||
[19]: day-19/
|
||||
[20]: day-20/
|
||||
[21]: day-21/
|
||||
[22]: day-22/
|
||||
[23]: day-23/
|
||||
[24]: day-24/
|
||||
[25]: day-25/
|
1000
2023/day-01/inputs/puzzle.txt
Normal file
1000
2023/day-01/inputs/puzzle.txt
Normal file
File diff suppressed because it is too large
Load Diff
4
2023/day-01/inputs/sample_part_one.txt
Normal file
4
2023/day-01/inputs/sample_part_one.txt
Normal file
@ -0,0 +1,4 @@
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
7
2023/day-01/inputs/sample_part_two.txt
Normal file
7
2023/day-01/inputs/sample_part_two.txt
Normal file
@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
34
2023/day-01/part_one.zig
Normal file
34
2023/day-01/part_one.zig
Normal file
@ -0,0 +1,34 @@
|
||||
const std = @import("std");
|
||||
|
||||
// Can't be bothered to read the file dynamically
|
||||
const data = @embedFile("inputs/puzzle.txt");
|
||||
|
||||
pub fn main() !void {
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
var lines = std.mem.tokenizeSequence(u8, data, "\n");
|
||||
var sum: u32 = 0;
|
||||
|
||||
while (lines.next()) |line| {
|
||||
var first: ?u8 = null;
|
||||
var last: ?u8 = null;
|
||||
|
||||
for (line) |byte| {
|
||||
if (std.ascii.isDigit(byte)) {
|
||||
if (first == null) {
|
||||
first = byte;
|
||||
}
|
||||
last = byte;
|
||||
}
|
||||
}
|
||||
|
||||
if (first != null and last != null) {
|
||||
const first_d = try std.fmt.charToDigit(first.?, 10);
|
||||
const last_d = try std.fmt.charToDigit(last.?, 10);
|
||||
const val = (first_d * 10) + last_d;
|
||||
|
||||
sum += val;
|
||||
}
|
||||
}
|
||||
|
||||
try stdout.print("{}", .{sum});
|
||||
}
|
70
2023/day-01/part_two.zig
Normal file
70
2023/day-01/part_two.zig
Normal file
@ -0,0 +1,70 @@
|
||||
const std = @import("std");
|
||||
const List = std.ArrayList;
|
||||
|
||||
// Can't be bothered to read the file dynamically
|
||||
const data = @embedFile("inputs/puzzle.txt");
|
||||
|
||||
const numbers = [_][]const u8{
|
||||
"one",
|
||||
"two",
|
||||
"three",
|
||||
"four",
|
||||
"five",
|
||||
"six",
|
||||
"seven",
|
||||
"eight",
|
||||
"nine",
|
||||
};
|
||||
|
||||
const Digit = struct {
|
||||
pos: usize,
|
||||
val: u8,
|
||||
};
|
||||
|
||||
fn digit_position(_: void, a: Digit, b: Digit) bool {
|
||||
return a.pos < b.pos;
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
const alloc = std.heap.page_allocator;
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
var lines = std.mem.tokenizeSequence(u8, data, "\n");
|
||||
var sum: u32 = 0;
|
||||
|
||||
while (lines.next()) |line| {
|
||||
var digits = List(Digit).init(alloc);
|
||||
defer digits.deinit();
|
||||
|
||||
for (line, 0..) |byte, idx| {
|
||||
if (std.ascii.isDigit(byte)) {
|
||||
const digit = try std.fmt.charToDigit(byte, 10);
|
||||
try digits.append(.{ .pos = idx, .val = digit });
|
||||
}
|
||||
}
|
||||
|
||||
var line_idx: usize = 0;
|
||||
|
||||
while (line_idx < line.len) {
|
||||
const cur = line[line_idx..];
|
||||
|
||||
for (numbers, 1..) |number, digit| {
|
||||
if (std.mem.startsWith(u8, cur, number)) {
|
||||
try digits.append(.{ .pos = line_idx, .val = @intCast(digit) });
|
||||
}
|
||||
}
|
||||
|
||||
line_idx += 1;
|
||||
}
|
||||
|
||||
const sorted = try digits.toOwnedSlice();
|
||||
std.mem.sort(Digit, sorted, {}, digit_position);
|
||||
const first = sorted[0].val;
|
||||
const last = sorted[sorted.len - 1].val;
|
||||
|
||||
const val = (first * 10) + last;
|
||||
sum += val;
|
||||
}
|
||||
|
||||
try stdout.print("{}\n", .{sum});
|
||||
}
|
Loading…
Reference in New Issue
Block a user