diff options
| author | Louis Burda <quent.burda@gmail.com> | 2023-04-08 12:40:30 -0400 |
|---|---|---|
| committer | Louis Burda <quent.burda@gmail.com> | 2023-04-09 10:21:36 -0400 |
| commit | 9282e95e8844afe856ba76ceb6d2c3010df8bb1a (patch) | |
| tree | e35affc89b20324371381e079f7cb5f8a06aa81b /src/03 | |
| parent | 2b5d4232879dc74491dabf54a0ddc958d66ebcec (diff) | |
| download | aoc2020-zig-master.tar.gz aoc2020-zig-master.zip | |
Diffstat (limited to 'src/03')
| -rw-r--r-- | src/03/input | 323 | ||||
| -rw-r--r-- | src/03/main.zig | 77 | ||||
| -rw-r--r-- | src/03/part1 | 60 | ||||
| -rw-r--r-- | src/03/part2 | 18 |
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: + +[1m[37m..##.......[0m..##.........##.........##.........##.........##....... ---> +[1m[37m#...#...#..[0m#...#...#..#...#...#..#...#...#..#...#...#..#...#...#.. +[1m[37m.#....#..#.[0m.#....#..#..#....#..#..#....#..#..#....#..#..#....#..#. +[1m[37m..#.#...#.#[0m..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.# +[1m[37m.#...##..#.[0m.#...##..#..#...##..#..#...##..#..#...##..#..#...##..#. +[1m[37m..#.##.....[0m..#.##.......#.##.......#.##.......#.##.......#.##..... ---> +[1m[37m.#.#.#....#[0m.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....# +[1m[37m.#........#[0m.#........#.#........#.#........#.#........#.#........# +[1m[37m#.##...#...[0m#.##...#...#.##...#...#.##...#...#.##...#...#.##...#... +[1m[37m#...##....#[0m#...##....##...##....##...##....##...##....##...##....# +[1m[37m.#..#...#.#[0m.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.# ---> + +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 [1m[37mcounting all the trees[0m you would encounter for the slope +[1m[37mright 3, down 1[0m: + +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 [1m[37mO[0m where there was +an open square and [1m[37mX[0m where there was a tree: + +..##.........##.........##.........##.........##.........##....... ---> +#..[1m[37mO[0m#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#.. +.#....[1m[37mX[0m..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#. +..#.#...#[1m[37mO[0m#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.# +.#...##..#..[1m[37mX[0m...##..#..#...##..#..#...##..#..#...##..#..#...##..#. +..#.##.......#.[1m[37mX[0m#.......#.##.......#.##.......#.##.......#.##..... ---> +.#.#.#....#.#.#.#.[1m[37mO[0m..#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....# +.#........#.#........[1m[37mX[0m.#........#.#........#.#........#.#........# +#.##...#...#.##...#...#.[1m[37mX[0m#...#...#.##...#...#.##...#...#.##...#... +#...##....##...##....##...#[1m[37mX[0m....##...##....##...##....##...##....# +.#..#...#.#.#..#...#.#.#..#...[1m[37mX[0m.#.#..#...#.#.#..#...#.#.#..#...#.# ---> + +In this example, traversing the map using this slope would cause you to encounter [1m[37m7[0m +trees. + +Starting at the top-left corner of your map and following a slope of right 3 and down 1, +[1m[37mhow many trees would you encounter?[0m + + 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 [1m[37m336[0m. + +[1m[37mWhat do you get if you multiply together the number of trees encountered on each of the +listed slopes?[0m + + |
