Compare commits
	
		
			2 Commits
		
	
	
		
			b13d5bb5f9
			...
			0b6eda2892
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						0b6eda2892
	
				 | 
					
					
						|||
| 
						
						
							
						
						b9bff3998e
	
				 | 
					
					
						
							
								
								
									
										94
									
								
								src/day14.zig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								src/day14.zig
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,94 @@
 | 
				
			|||||||
 | 
					const std = @import("std");
 | 
				
			||||||
 | 
					const util = @import("util.zig");
 | 
				
			||||||
 | 
					const mem = std.mem;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn main() !void {
 | 
				
			||||||
 | 
					    const input = @embedFile("data/day14.txt");
 | 
				
			||||||
 | 
					    const sln = try solve(util.gpa, input);
 | 
				
			||||||
 | 
					    std.debug.print("{d}\n", .{sln.a});
 | 
				
			||||||
 | 
					    std.debug.print("{d}\n", .{sln.b});
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Solution = struct {
 | 
				
			||||||
 | 
					    a: usize,
 | 
				
			||||||
 | 
					    b: usize,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn northWeight(alloc: mem.Allocator, grid: [][]u8) !usize {
 | 
				
			||||||
 | 
					    var next_slot = try alloc.alloc(u32, grid[0].len);
 | 
				
			||||||
 | 
					    defer alloc.free(next_slot);
 | 
				
			||||||
 | 
					    @memset(next_slot, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    var weight: usize = 0;
 | 
				
			||||||
 | 
					    for (grid, 0..) |line, row| {
 | 
				
			||||||
 | 
					        for (line, 0..) |char, col| {
 | 
				
			||||||
 | 
					            switch (char) {
 | 
				
			||||||
 | 
					                'O' => {
 | 
				
			||||||
 | 
					                    weight += grid.len - next_slot[col];
 | 
				
			||||||
 | 
					                    next_slot[col] += 1;
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                '#' => next_slot[col] = @as(u32, @intCast(row)) + 1,
 | 
				
			||||||
 | 
					                else => {},
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return weight;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn spin(grid: [][]u8, n: usize) void {
 | 
				
			||||||
 | 
					    _ = grid;
 | 
				
			||||||
 | 
					    for (0..n) |_| {}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
 | 
				
			||||||
 | 
					    var height = mem.count(u8, input, &[_]u8{'\n'});
 | 
				
			||||||
 | 
					    if (input[input.len - 1] != '\n') height += 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    var grid = try alloc.alloc([]u8, height);
 | 
				
			||||||
 | 
					    defer alloc.free(grid);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    var it = util.splitLines(input);
 | 
				
			||||||
 | 
					    var row: u32 = 0;
 | 
				
			||||||
 | 
					    while (it.next()) |line| {
 | 
				
			||||||
 | 
					        if (line.len == 0) continue;
 | 
				
			||||||
 | 
					        grid[row] = try alloc.alloc(u8, line.len);
 | 
				
			||||||
 | 
					        @memcpy(grid[row], line);
 | 
				
			||||||
 | 
					        row += 1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const w = try northWeight(alloc, grid);
 | 
				
			||||||
 | 
					    spin(grid, 1000000000);
 | 
				
			||||||
 | 
					    const v = try northWeight(alloc, grid);
 | 
				
			||||||
 | 
					    _ = v;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (grid) |line| {
 | 
				
			||||||
 | 
					        alloc.free(line);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return .{ .a = w, .b = 0 };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test "silver" {
 | 
				
			||||||
 | 
					    const input =
 | 
				
			||||||
 | 
					        \\O....#....
 | 
				
			||||||
 | 
					        \\O.OO#....#
 | 
				
			||||||
 | 
					        \\.....##...
 | 
				
			||||||
 | 
					        \\OO.#O....O
 | 
				
			||||||
 | 
					        \\.O.....O#.
 | 
				
			||||||
 | 
					        \\O.#..O.#.#
 | 
				
			||||||
 | 
					        \\..O..#O..O
 | 
				
			||||||
 | 
					        \\.......O..
 | 
				
			||||||
 | 
					        \\#....###..
 | 
				
			||||||
 | 
					        \\#OO..#....
 | 
				
			||||||
 | 
					    ;
 | 
				
			||||||
 | 
					    const sln = try solve(std.testing.allocator, input);
 | 
				
			||||||
 | 
					    try std.testing.expectEqual(@as(usize, 136), sln.a);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test "gold" {
 | 
				
			||||||
 | 
					    const input =
 | 
				
			||||||
 | 
					        \\
 | 
				
			||||||
 | 
					    ;
 | 
				
			||||||
 | 
					    _ = input;
 | 
				
			||||||
 | 
					    // const sln = try solve(std.testing.allocator, input);
 | 
				
			||||||
 | 
					    // try std.testing.expectEqual(@as(usize, 0), sln.b);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user