aboutsummaryrefslogtreecommitdiffstats
path: root/src/03
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2023-04-08 12:40:30 -0400
committerLouis Burda <quent.burda@gmail.com>2023-04-09 10:21:36 -0400
commit9282e95e8844afe856ba76ceb6d2c3010df8bb1a (patch)
treee35affc89b20324371381e079f7cb5f8a06aa81b /src/03
parent2b5d4232879dc74491dabf54a0ddc958d66ebcec (diff)
downloadaoc2020-zig-master.tar.gz
aoc2020-zig-master.zip
Restructure repo and update solutions to zig 0.10.1HEADmaster
Diffstat (limited to 'src/03')
-rw-r--r--src/03/input323
-rw-r--r--src/03/main.zig77
-rw-r--r--src/03/part160
-rw-r--r--src/03/part218
4 files changed, 478 insertions, 0 deletions
diff --git a/src/03/input b/src/03/input
new file mode 100644
index 0000000..a11fc85
--- /dev/null
+++ b/src/03/input
@@ -0,0 +1,323 @@
+.........#.#.#.........#.#.....
+...#......#...#.....#.....#....
+#.....#.....#.....#.#........#.
+......#..#......#.......#......
+.#..........#.............#...#
+............#..##.......##...##
+....#.....#..#....#............
+.#..#.........#....#.#....#....
+#.#...#...##..##.#..##..#....#.
+.#.......#.#...#..........#....
+...#...#........##...#..#.....#
+..................#..........#.
+.....#.##..............#.......
+........#....##..##....#.......
+...#.....#.##..........#...##..
+.......#.#....#............#...
+..............#......#......#..
+#.......#...........#........##
+.......#.......##......#.......
+................#....##...#.#.#
+#.......#....................#.
+.##.#..##..#..#.#.....#.....#..
+#...#............#......##....#
+.#....##.#......#.#......#..#..
+..........#........#.#.#.......
+...#...#..........#..#....#....
+..#.#...#...#...##...##......#.
+......#...#........#.......###.
+....#...............#.###...#.#
+..................#.....#..#.#.
+.#...#..#..........#........#..
+#..........##................##
+...#.....#...#......#.#......#.
+......#..........#.#......#..#.
+..#......#.....................
+............#.........##.......
+......#.......#........#.......
+#.#...#...........#.......#....
+.#.#........#.#.#....#........#
+#.....##........#.#.....#.#....
+.#...#..........##...#.....#..#
+.........#....###............#.
+..#...#..............#.#.#.....
+.....#.#.#..#.#.#.###......#.#.
+.#.##...#.......###..#.........
+.....##....#.##..#.##..#.......
+..#...........##......#..#.....
+................##...#.........
+##.....................#..#.###
+...#..#....#...........#.......
+.#.............##.#.....#.#....
+.......#.#.#....##..#....#...#.
+...##..#..........#....#.......
+....##......#.........#........
+.##....#...........#.#.......#.
+...#...#.##.......#.#..........
+..#.........#.##...........#...
+....#.##........#.......#...##.
+...................#..#..#...##
+#...#......###..##.#..###......
+#.............#.#........#...#.
+...#...#..#..#..............#..
+#.....#..#...#...#......#.#..#.
+...##.............#........##.#
+......#.#.........#.#....#...#.
+........##............#...#....
+............#.#...#......#.....
+...#...........#...#...........
+.........#.#......#............
+....#.............#..#.....#..#
+#.....#...........#.....#.#.#.#
+.............#.....##......#...
+...................###.#......#
+#.##.....#...#..#..#...#....#..
+............#.....#....#.#.....
+#....#..#..........#....#..#...
+..........##..................#
+....#.......###..#......###....
+.......#...#.##.##..#....##....
+...##...............#.....#...#
+#...........#...#......#..#..#.
+..##....#.......#...#.....#....
+.......##..............#.##..#.
+.#......#..........#.......#...
+....##...................#.#..#
+......#....###................#
+.#........#...........#........
+.#.....##....#..##...........#.
+##..............##...#.......#.
+......#..........#..........##.
+..#.....#.#.........####....#..
+.............#......#......#...
+..#.............#...........##.
+#.#...#........#..........##...
+...#....#.....#.....#.....##...
+......#........................
+......#..#...#......#.....#....
+......#....##.....#....#.......
+#.#......#.##..#...............
+..........#.#.##..##..#........
+......#.#..#....###.#..........
+........................#....#.
+#.#.............#....#.....##.#
+.#.#..........#.......#..#....#
+..#...#......#..#..#...#.#...#.
+...#.##...###..................
+........#.#...........#...#....
+........#..........#....#......
+#....#........#.......##.....#.
+......###...##...#......#......
+............#.......#.....##..#
+....#..#.......##......#.......
+#............##................
+...............#..#......#...#.
+.#....##...#......#............
+#...#................#.........
+..#....#..#........##......#...
+....#....###......##.#.......#.
+......#.#..#.#..#....#..#......
+....#..........#..#..#.........
+.#..#.......#......#........#..
+.......#..#..#............#....
+.............#...#....#..#.....
+..............#.#.#.........#..
+#.....##.......#....#..........
+...#.....#......#..............
+...##.#..#.#........#..##....#.
+.......#.#.....##..#...........
+.....#.#....#..................
+.#......#.###..............##..
+..#....#...#..#...##...##....#.
+..........##..#...#..#.........
+..#............#........#.#...#
+.........................#.#.#.
+......#........#.#..#......##.#
+#.#......#..#.........#........
+.....#........#......#...#.#...
+........##....##....#.#........
+....#....#.#...#...##....#..#..
+#.............#.....#..........
+#............##..#............#
+..#.#......#........#..........
+.#......#......#.#.##.##.......
+..#.....#..........#......##...
+...#......#...#.##....#.....##.
+......#......#...........#.#...
+....#........#..#..#........#.#
+....#.........#.....#...#.#.#..
+....#.....###........#.........
+.............##........#.#.....
+...#............#........#.#.#.
+......#....#.......#.#.........
+.....#................#........
+.#....#..#.#.............#...#.
+#..##...#............#......#..
+...#..#........................
+.#.#...........#.......#.......
+#....###............##.........
+...##....#.#............##.....
+.........####......#........#..
+.....#.......#.#...............
+.......#...#.###..#....#...#..#
+...#.....##..#....#..#.#...###.
+.............#........#.#.#..#.
+................#..........##..
+.......####..##..##........##.#
+..#......#..#..#.#.##..........
+#....#........#....#...###.#.#.
+........##........##.....#.....
+...........#.#...........#....#
+#.............#...........#....
+...#.........#...#....#.....#..
+..##......#...#...............#
+.............#.........#....#..
+..#...........#..#........#.##.
+.#.#......#.............##...#.
+.#......#.....##.#..#..#.......
+....##......#..................
+.#.#..##............#....#....#
+........#...##.............#..#
+........#....##.....#......###.
+.........#....#.#..............
+#.....#........................
+.#..#....#.....#......#.###..#.
+..........#...#....##....#..#..
+...#.#.#...##..#..#......#..#.#
+#............#.....#....#......
+#.###...#.#......###..#....#..#
+...#.###........#......#....#..
+..#.##...#.........#.#.........
+............##.................
+....#..........#.#.#.#.#....#..
+...##.#...#.......#.......##..#
+....##.#........#....#...#.....
+.............#.#....#...#.....#
+...#......................##...
+..#...#.....#.....#........#..#
+..#..#.......#....#..##.....#..
+..#..#.#.......................
+.......##..#....#....#..#......
+....#......##....#............#
+.#...#..#..#.##...#.#...#......
+.....#......#....#.........#...
+.##......##.........#....#.....
+#...........#...##.....#......#
+.....#.#.......#.........#.....
+.........#..........#..####.##.
+............#..#......#.#......
+.#.............#........#.#....
+......#......#...#..#....#####.
+.........##.#..##...###..#....#
+....#.#....#.#..#.........#....
+..#.............#...##...##....
+........#..........#.##..#....#
+.....#...#..##........#.#..#...
+##..#.#.....#............#.....
+.............#........##...##..
+#......####.....##.............
+..##.....##....###..#.#....#...
+......##.##.#...#..#.#..##.....
+......#.................#......
+#.....#.#...#......#.#....#....
+....#.#........#..............#
+##........#.......##.#...##...#
+..#..................#.#....#..
+...........#..........#.#.....#
+........##.#.....#......#..#..#
+.....#....#..#.....#.........##
+#.#..#..#...#......#..........#
+#...##.....#..#.#.......#.##...
+..#....##...............#......
+#..........#.#.........#.#....#
+..............#......#....#....
+.....#...........#...#...#...#.
+...#......#....#....#..........
+.#..........#.#....##..##....#.
+..............#.........#.#....
+.......#.....#.....#...##....#.
+##.#.........#....#.....#.#....
+....#..#......#................
+......##.....#.......##........
+.....##...#........#...#...#...
+..#...#...#..#..#.#......#..#..
+....#...#.......#..............
+....#..#.........###........#..
+....#.............##..#........
+..........##.#.......##..##....
+#.##..................#.....#..
+#........#........#.....#......
+.#...#......#..................
+#....##.##......#...#.........#
+......#.##..##................#
+............#.........##.......
+..........####.#........#.....#
+.##...#...#....#..#............
+.#.##...#..#...#......#......##
+.....#.#....#..###......#.#.#..
+...#.......................##..
+......................#.......#
+..#....#.........#..#.#.....#..
+.#....#..#....#...#............
+..........#...##.....#.#..#....
+........#..#..#....#...#...#...
+.....#......#.#................
+.....#...........#...#.........
+.....#...##..#.#....#..#.....#.
+#.......#.............##.......
+................#....#.#..#....
+.#..##...#.#........#......#.#.
+.#.##..........#...............
+....##......#....#........#....
+....#..#....#.##.#.............
+.......#..#......##.#.....#....
+.......#.....#.............#...
+.....#....#.......#............
+........#.#...##..##..##.......
+#.........##....##...##........
+........#..#.#..........###.#..
+..........................#.#..
+#.....#.......#..#........#....
+...##.....#.......#......#.....
+.#.#..#...........#...........#
+.....##..#........#...####.....
+.#.#...##.#.#..#..#.#..#.......
+..#.##.#...#.#.#...#..#........
+............#..........#..#....
+...............#..##.#.........
+.............#.....#....#......
+...##..##......##..........#...
+..#.......#....#..........#...#
+.##................#.#.#.......
+.....##.....#..#.....#.........
+......#.#.......#......#..#....
+.....#.....#........#.......##.
+......#.......##......#...#...#
+....#...........###.........#..
+...#.....#.........##........#.
+..#.....#..............#.......
+....#.......#...#....#....#..##
+......#...........#...........#
+.##......#......#.#.....#.##...
+....#..##......#...#..#.#.###..
+.......#.#....#......#..#......
+..........#........#...........
+#.##.........#.#.#...#...#.#...
+.#......###.....#....#.#....#..
+...................##..#.......
+....#..#..............#.#.....#
+#..................#.....#.....
+...........##.##.......#..#.#..
+........#.#......#...........#.
+#..#.......#...#...........#.#.
+......##...........#...........
+.........#.#........#........#.
+#......#....#.#.....#..#.......
+............#..#.....##...#....
+.#......#..#......#.........#..
+.......#...#.........#.##.....#
+........................#..#...
+.###..............#.#..#.......
+.....#.........#.......#......#
+..##..##....#.....#.......#.#..
+...###.#..#.##............#....
diff --git a/src/03/main.zig b/src/03/main.zig
new file mode 100644
index 0000000..2ec8f24
--- /dev/null
+++ b/src/03/main.zig
@@ -0,0 +1,77 @@
+const std = @import("std");
+const aoc = @import("aoc");
+
+const Entry = []u1;
+const Vector = struct { x: u16, y: u16 };
+
+fn freeEntries(allocator: std.mem.Allocator, entries: *const std.ArrayList(Entry)) void {
+ for (entries.items) |entry| {
+ allocator.free(entry);
+ }
+ entries.deinit();
+}
+
+fn parse(allocator: std.mem.Allocator, input: []u8) !std.ArrayList(Entry) {
+ var entries = std.ArrayList(Entry).init(allocator);
+ errdefer freeEntries(allocator, &entries);
+
+ var lineit = std.mem.tokenize(u8, input, "\n");
+ while (lineit.next()) |line| {
+ var linemap: []u1 = try allocator.alloc(u1, line.len);
+ errdefer allocator.free(linemap);
+
+ for (line) |c, i| {
+ linemap[i] = @boolToInt(c == '#');
+ }
+ try entries.append(linemap);
+ }
+
+ return entries;
+}
+
+fn treesHit(map: std.ArrayList(Entry), slope: Vector) u16 {
+ var count: u16 = 0;
+ var pos = Vector{ .x = 0, .y = 0 };
+ while (pos.y < map.items.len - 1) {
+ pos.x += slope.x;
+ pos.y += slope.y;
+
+ count += map.items[pos.y][pos.x % map.items[0].len];
+ }
+ return count;
+}
+
+fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 {
+ _ = args;
+
+ const map = try parse(allocator, input);
+ defer freeEntries(allocator, &map);
+
+ const answer = treesHit(map, Vector{ .x = 3, .y = 1 });
+
+ return try std.fmt.allocPrint(allocator, "{d}", .{answer});
+}
+
+fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 {
+ _ = args;
+
+ const map = try parse(allocator, input);
+ defer freeEntries(allocator, &map);
+
+ const slopes = [_]Vector{
+ Vector{ .x = 1, .y = 1 },
+ Vector{ .x = 3, .y = 1 },
+ Vector{ .x = 5, .y = 1 },
+ Vector{ .x = 7, .y = 1 },
+ Vector{ .x = 1, .y = 2 },
+ };
+
+ var answer: u32 = 1;
+ for (slopes) |slope| {
+ answer *= treesHit(map, slope);
+ }
+
+ return try std.fmt.allocPrint(allocator, "{d}", .{answer});
+}
+
+pub const main = aoc.main(part1, part2, .{ "282", "958815792" });
diff --git a/src/03/part1 b/src/03/part1
new file mode 100644
index 0000000..e44b180
--- /dev/null
+++ b/src/03/part1
@@ -0,0 +1,60 @@
+--- Day 3: Toboggan Trajectory ---
+
+With the toboggan login problems resolved, you set off toward the airport. While travel by toboggan
+might be easy, it's certainly not safe: there's very minimal steering and the area is covered in
+trees. You'll need to see which angles will take you near the fewest trees.
+
+Due to the local geology, trees in this area only grow on exact integer coordinates in a grid. You
+make a map (your puzzle input) of the open squares (.) and trees (#) you can see. For example:
+
+..##....... #...#...#.. .#....#..#. ..#.#...#.# .#...##..#. ..#.##..... .#.#.#....# .#........#
+#.##...#... #...##....# .#..#...#.#
+
+These aren't the only trees, though; due to something you read about once involving arboreal
+genetics and biome stability, the same pattern repeats to the right many times:
+
+..##.........##.........##.........##.........##.........##....... --->
+#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..
+.#....#..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.
+..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#
+.#...##..#..#...##..#..#...##..#..#...##..#..#...##..#..#...##..#.
+..#.##.......#.##.......#.##.......#.##.......#.##.......#.##..... --->
+.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#
+.#........#.#........#.#........#.#........#.#........#.#........#
+#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...
+#...##....##...##....##...##....##...##....##...##....##...##....#
+.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.# --->
+
+You start on the open square (.) in the top-left corner and need to reach the bottom (below the
+bottom-most row on your map).
+
+The toboggan can only follow a few specific slopes (you opted for a cheaper model that prefers
+rational numbers); start by counting all the trees you would encounter for the slope
+right 3, down 1:
+
+From your starting position at the top-left, check the position that is right 3 and down 1. Then,
+check the position that is right 3 and down 1 from there, and so on until you go past the bottom of
+the map.
+
+The locations you'd check in the above example are marked here with O where there was
+an open square and X where there was a tree:
+
+..##.........##.........##.........##.........##.........##....... --->
+#..O#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..
+.#....X..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.
+..#.#...#O#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#
+.#...##..#..X...##..#..#...##..#..#...##..#..#...##..#..#...##..#.
+..#.##.......#.X#.......#.##.......#.##.......#.##.......#.##..... --->
+.#.#.#....#.#.#.#.O..#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#
+.#........#.#........X.#........#.#........#.#........#.#........#
+#.##...#...#.##...#...#.X#...#...#.##...#...#.##...#...#.##...#...
+#...##....##...##....##...#X....##...##....##...##....##...##....#
+.#..#...#.#.#..#...#.#.#..#...X.#.#..#...#.#.#..#...#.#.#..#...#.# --->
+
+In this example, traversing the map using this slope would cause you to encounter 7
+trees.
+
+Starting at the top-left corner of your map and following a slope of right 3 and down 1,
+how many trees would you encounter?
+
+
diff --git a/src/03/part2 b/src/03/part2
new file mode 100644
index 0000000..715fd1a
--- /dev/null
+++ b/src/03/part2
@@ -0,0 +1,18 @@
+--- Part Two ---
+
+Time to check the rest of the slopes - you need to minimize the probability of a sudden arboreal
+stop, after all.
+
+Determine the number of trees you would encounter if, for each of the following slopes, you start at
+the top-left corner and traverse the map all the way to the bottom:
+
+- Right 1, down 1. - Right 3, down 1. (This is the slope you already checked.) - Right 5, down 1. -
+Right 7, down 1. - Right 1, down 2.
+
+In the above example, these slopes would find 2, 7, 3, 4, and 2 tree(s) respectively; multiplied
+together, these produce the answer 336.
+
+What do you get if you multiply together the number of trees encountered on each of the
+listed slopes?
+
+