aoc-2020-zig

git clone https://git.sinitax.com/sinitax/aoc-2020-zig
Log | Files | Refs | README | sfeed.txt

commit 46260c9b3a28452c9e80bb809c1f4c3e0fe83f99
parent 2c89e8f69b733f1a30acbad6763f57cbfad66df2
Author: Louis Burda <quent.burda@gmail.com>
Date:   Thu,  3 Dec 2020 18:07:48 +0100

added day 3

Diffstat:
Adata/template.zig | 37+++++++++++++++++++++++++++++++++++++
Mscripts/prepare | 12++++++------
Mscripts/scrape | 17+++++++++++++++--
Asrc/day3/input | 323+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day3/main.zig | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day3/part1 | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day3/part2 | 18++++++++++++++++++
7 files changed, 531 insertions(+), 8 deletions(-)

diff --git a/data/template.zig b/data/template.zig @@ -0,0 +1,37 @@ +const std = @import("std"); +const aoc = @import("aoc"); + +const Entry = u16; + +fn free_entries(allocator: *std.mem.Allocator, entries: std.ArrayList(Entry)) void { + entries.deinit(); +} + +fn parse(allocator: *std.mem.Allocator, input: []u8) !std.ArrayList(Entry) { + var entries = std.ArrayList(int).init(allocator); + errdefer free_entries(allocator, entries); + + // ... + + return entries; +} + +fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { + const entries = try parse(allocator, input); + defer free_entries(allocator, entries); + + var answer: u32 = 0; + + std.debug.print("{}\n", .{answer}); +} + +fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { + const entries = try parse(allocator, input); + defer free_entries(allocator, entries); + + var answer: u32 = 0; + + std.debug.print("{}\n", .{answer}); +} + +pub const main = aoc.gen_main(part1, part2); diff --git a/scripts/prepare b/scripts/prepare @@ -23,7 +23,7 @@ if [[ "$1" =~ "[0-9]+" ]]; then fi daynum="$1" -folder="$REPOROOT/day$daynum" +folder="$REPOROOT/src/day$daynum" if [ -e "$folder" ]; then echo "Directory for problem already exists" exit 1 @@ -31,12 +31,12 @@ fi echo "Initializing folder" -mkdir $folder -pushd $reporoot$folder &>/dev/null +mkdir "$folder" + +echo "Copying template" + +cp "$REPOROOT/data/template.zig" "$folder/main.zig" load "Loading problem input" $daynum input load "Loading first problem text" $daynum text 1 load "Loading second problem text" $daynum text 1 - -popd &>/dev/null - diff --git a/scripts/scrape b/scripts/scrape @@ -64,12 +64,25 @@ def scrape_text(day, part): textobj = soup.select("article.day-desc")[part - 1] with open(path, "w+") as of: - of.write("{}\n".format(extract_hltext(textobj))) + text = extract_hltext(textobj) + parts = text.split("\n\n") + for i,p in enumerate(parts): + newp = [list(),] + words = p.split() + linelen = 0 + for w in words: + if linelen + len(w) > 100: + newp.append(list()) + linelen = 0 + newp[-1].append(w) + linelen += len(w) + 1 + parts[i] = "\n".join([" ".join(words) for words in newp]) + of.write("{}\n".format("\n\n".join(parts))) def main(): if cmd == "input": scrape_input(day) - elif cmd == "text": + elif cmd == "part": part = int(cmdargs[0]) if cmdargs else 1 scrape_text(day, part) else: diff --git a/src/day3/input b/src/day3/input @@ -0,0 +1,323 @@ +.........#.#.#.........#.#..... +...#......#...#.....#.....#.... +#.....#.....#.....#.#........#. +......#..#......#.......#...... +.#..........#.............#...# +............#..##.......##...## +....#.....#..#....#............ +.#..#.........#....#.#....#.... +#.#...#...##..##.#..##..#....#. +.#.......#.#...#..........#.... +...#...#........##...#..#.....# +..................#..........#. +.....#.##..............#....... +........#....##..##....#....... +...#.....#.##..........#...##.. +.......#.#....#............#... +..............#......#......#.. +#.......#...........#........## +.......#.......##......#....... +................#....##...#.#.# +#.......#....................#. +.##.#..##..#..#.#.....#.....#.. +#...#............#......##....# +.#....##.#......#.#......#..#.. +..........#........#.#.#....... +...#...#..........#..#....#.... +..#.#...#...#...##...##......#. +......#...#........#.......###. +....#...............#.###...#.# +..................#.....#..#.#. +.#...#..#..........#........#.. +#..........##................## +...#.....#...#......#.#......#. +......#..........#.#......#..#. +..#......#..................... +............#.........##....... +......#.......#........#....... +#.#...#...........#.......#.... +.#.#........#.#.#....#........# +#.....##........#.#.....#.#.... +.#...#..........##...#.....#..# +.........#....###............#. +..#...#..............#.#.#..... +.....#.#.#..#.#.#.###......#.#. +.#.##...#.......###..#......... +.....##....#.##..#.##..#....... +..#...........##......#..#..... +................##...#......... +##.....................#..#.### +...#..#....#...........#....... +.#.............##.#.....#.#.... +.......#.#.#....##..#....#...#. +...##..#..........#....#....... +....##......#.........#........ +.##....#...........#.#.......#. +...#...#.##.......#.#.......... +..#.........#.##...........#... +....#.##........#.......#...##. +...................#..#..#...## +#...#......###..##.#..###...... +#.............#.#........#...#. +...#...#..#..#..............#.. +#.....#..#...#...#......#.#..#. +...##.............#........##.# +......#.#.........#.#....#...#. +........##............#...#.... +............#.#...#......#..... +...#...........#...#........... +.........#.#......#............ +....#.............#..#.....#..# +#.....#...........#.....#.#.#.# +.............#.....##......#... +...................###.#......# +#.##.....#...#..#..#...#....#.. +............#.....#....#.#..... +#....#..#..........#....#..#... +..........##..................# +....#.......###..#......###.... +.......#...#.##.##..#....##.... +...##...............#.....#...# +#...........#...#......#..#..#. +..##....#.......#...#.....#.... +.......##..............#.##..#. +.#......#..........#.......#... +....##...................#.#..# +......#....###................# +.#........#...........#........ +.#.....##....#..##...........#. +##..............##...#.......#. +......#..........#..........##. +..#.....#.#.........####....#.. +.............#......#......#... +..#.............#...........##. +#.#...#........#..........##... +...#....#.....#.....#.....##... +......#........................ +......#..#...#......#.....#.... +......#....##.....#....#....... +#.#......#.##..#............... +..........#.#.##..##..#........ +......#.#..#....###.#.......... +........................#....#. +#.#.............#....#.....##.# +.#.#..........#.......#..#....# +..#...#......#..#..#...#.#...#. +...#.##...###.................. +........#.#...........#...#.... +........#..........#....#...... +#....#........#.......##.....#. +......###...##...#......#...... +............#.......#.....##..# +....#..#.......##......#....... +#............##................ +...............#..#......#...#. +.#....##...#......#............ +#...#................#......... +..#....#..#........##......#... +....#....###......##.#.......#. +......#.#..#.#..#....#..#...... +....#..........#..#..#......... +.#..#.......#......#........#.. +.......#..#..#............#.... +.............#...#....#..#..... +..............#.#.#.........#.. +#.....##.......#....#.......... +...#.....#......#.............. +...##.#..#.#........#..##....#. +.......#.#.....##..#........... +.....#.#....#.................. +.#......#.###..............##.. +..#....#...#..#...##...##....#. +..........##..#...#..#......... +..#............#........#.#...# +.........................#.#.#. +......#........#.#..#......##.# +#.#......#..#.........#........ +.....#........#......#...#.#... +........##....##....#.#........ +....#....#.#...#...##....#..#.. +#.............#.....#.......... +#............##..#............# +..#.#......#........#.......... +.#......#......#.#.##.##....... +..#.....#..........#......##... +...#......#...#.##....#.....##. +......#......#...........#.#... +....#........#..#..#........#.# +....#.........#.....#...#.#.#.. +....#.....###........#......... +.............##........#.#..... +...#............#........#.#.#. +......#....#.......#.#......... +.....#................#........ +.#....#..#.#.............#...#. +#..##...#............#......#.. +...#..#........................ +.#.#...........#.......#....... +#....###............##......... +...##....#.#............##..... +.........####......#........#.. +.....#.......#.#............... +.......#...#.###..#....#...#..# +...#.....##..#....#..#.#...###. +.............#........#.#.#..#. +................#..........##.. +.......####..##..##........##.# +..#......#..#..#.#.##.......... +#....#........#....#...###.#.#. +........##........##.....#..... +...........#.#...........#....# +#.............#...........#.... +...#.........#...#....#.....#.. +..##......#...#...............# +.............#.........#....#.. +..#...........#..#........#.##. +.#.#......#.............##...#. +.#......#.....##.#..#..#....... +....##......#.................. +.#.#..##............#....#....# +........#...##.............#..# +........#....##.....#......###. +.........#....#.#.............. +#.....#........................ +.#..#....#.....#......#.###..#. +..........#...#....##....#..#.. +...#.#.#...##..#..#......#..#.# +#............#.....#....#...... +#.###...#.#......###..#....#..# +...#.###........#......#....#.. +..#.##...#.........#.#......... +............##................. +....#..........#.#.#.#.#....#.. +...##.#...#.......#.......##..# +....##.#........#....#...#..... +.............#.#....#...#.....# +...#......................##... +..#...#.....#.....#........#..# +..#..#.......#....#..##.....#.. +..#..#.#....................... +.......##..#....#....#..#...... +....#......##....#............# +.#...#..#..#.##...#.#...#...... +.....#......#....#.........#... +.##......##.........#....#..... +#...........#...##.....#......# +.....#.#.......#.........#..... +.........#..........#..####.##. +............#..#......#.#...... +.#.............#........#.#.... +......#......#...#..#....#####. +.........##.#..##...###..#....# +....#.#....#.#..#.........#.... +..#.............#...##...##.... +........#..........#.##..#....# +.....#...#..##........#.#..#... +##..#.#.....#............#..... +.............#........##...##.. +#......####.....##............. +..##.....##....###..#.#....#... +......##.##.#...#..#.#..##..... +......#.................#...... +#.....#.#...#......#.#....#.... +....#.#........#..............# +##........#.......##.#...##...# +..#..................#.#....#.. +...........#..........#.#.....# +........##.#.....#......#..#..# +.....#....#..#.....#.........## +#.#..#..#...#......#..........# +#...##.....#..#.#.......#.##... +..#....##...............#...... +#..........#.#.........#.#....# +..............#......#....#.... +.....#...........#...#...#...#. +...#......#....#....#.......... +.#..........#.#....##..##....#. +..............#.........#.#.... +.......#.....#.....#...##....#. +##.#.........#....#.....#.#.... +....#..#......#................ +......##.....#.......##........ +.....##...#........#...#...#... +..#...#...#..#..#.#......#..#.. +....#...#.......#.............. +....#..#.........###........#.. +....#.............##..#........ +..........##.#.......##..##.... +#.##..................#.....#.. +#........#........#.....#...... +.#...#......#.................. +#....##.##......#...#.........# +......#.##..##................# +............#.........##....... +..........####.#........#.....# +.##...#...#....#..#............ +.#.##...#..#...#......#......## +.....#.#....#..###......#.#.#.. +...#.......................##.. +......................#.......# +..#....#.........#..#.#.....#.. +.#....#..#....#...#............ +..........#...##.....#.#..#.... +........#..#..#....#...#...#... +.....#......#.#................ +.....#...........#...#......... +.....#...##..#.#....#..#.....#. +#.......#.............##....... +................#....#.#..#.... +.#..##...#.#........#......#.#. +.#.##..........#............... +....##......#....#........#.... +....#..#....#.##.#............. +.......#..#......##.#.....#.... +.......#.....#.............#... +.....#....#.......#............ +........#.#...##..##..##....... +#.........##....##...##........ +........#..#.#..........###.#.. +..........................#.#.. +#.....#.......#..#........#.... +...##.....#.......#......#..... +.#.#..#...........#...........# +.....##..#........#...####..... +.#.#...##.#.#..#..#.#..#....... +..#.##.#...#.#.#...#..#........ +............#..........#..#.... +...............#..##.#......... +.............#.....#....#...... +...##..##......##..........#... +..#.......#....#..........#...# +.##................#.#.#....... +.....##.....#..#.....#......... +......#.#.......#......#..#.... +.....#.....#........#.......##. +......#.......##......#...#...# +....#...........###.........#.. +...#.....#.........##........#. +..#.....#..............#....... +....#.......#...#....#....#..## +......#...........#...........# +.##......#......#.#.....#.##... +....#..##......#...#..#.#.###.. +.......#.#....#......#..#...... +..........#........#........... +#.##.........#.#.#...#...#.#... +.#......###.....#....#.#....#.. +...................##..#....... +....#..#..............#.#.....# +#..................#.....#..... +...........##.##.......#..#.#.. +........#.#......#...........#. +#..#.......#...#...........#.#. +......##...........#........... +.........#.#........#........#. +#......#....#.#.....#..#....... +............#..#.....##...#.... +.#......#..#......#.........#.. +.......#...#.........#.##.....# +........................#..#... +.###..............#.#..#....... +.....#.........#.......#......# +..##..##....#.....#.......#.#.. +...###.#..#.##............#.... diff --git a/src/day3/main.zig b/src/day3/main.zig @@ -0,0 +1,72 @@ +const std = @import("std"); +const aoc = @import("aoc"); + +const Entry = []u1; +const Vector = struct { + x: u16, y: u16 +}; + +fn free_entries(allocator: *std.mem.Allocator, entries: 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 free_entries(allocator, entries); + + var lineit = std.mem.tokenize(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 trees_hit(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) !void { + const map = try parse(allocator, input); + defer free_entries(allocator, map); + + const answer = trees_hit(map, Vector{ .x = 3, .y = 1 }); + std.debug.print("{}\n", .{answer}); +} + +fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { + const map = try parse(allocator, input); + defer free_entries(allocator, map); + + var answer: u32 = 1; + 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 }, + }; + for (slopes) |slope| { + answer *= trees_hit(map, slope); + } + + std.debug.print("{}\n", .{answer}); +} + +pub const main = aoc.gen_main(part1, part2); diff --git a/src/day3/part1 b/src/day3/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/day3/part2 b/src/day3/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? + +