Skip to content

Commit

Permalink
11-1
Browse files Browse the repository at this point in the history
  • Loading branch information
milanaleksic committed Dec 11, 2023
1 parent 35c29dc commit fa800af
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions src/day11.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");
const util = @import("util.zig");
const mem = std.mem;
const print = std.debug.print;
const math = std.math;

const NodeType = enum {
Space,
Expand All @@ -16,14 +17,21 @@ const NodeType = enum {
}
};

const Location = struct {
x: usize,
y: usize,
};

const Data = struct {
const Self = @This();
allocator: std.mem.Allocator,

galaxies: std.ArrayList(Location),
rows: std.ArrayList(std.ArrayList(NodeType)),

pub fn init(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !Self {
var rows: std.ArrayList(std.ArrayList(NodeType)) = try std.ArrayList(std.ArrayList(NodeType)).initCapacity(allocator, list.items.len);
var countOfGalaxies: usize = 0;
for (list.items) |line| {
// print("line={s}\n", .{line});
var row: std.ArrayList(NodeType) = try std.ArrayList(NodeType).initCapacity(allocator, line.len);
Expand All @@ -32,6 +40,7 @@ const Data = struct {
const nodeType = NodeType.fromChar(char);
if (nodeType == NodeType.Galaxy) {
numberOfGalaxiesInRow += 1;
countOfGalaxies += 1;
}
try row.append(nodeType);
}
Expand Down Expand Up @@ -61,9 +70,22 @@ const Data = struct {
}
x += 1;
}

var galaxies: std.ArrayList(Location) = try std.ArrayList(Location).initCapacity(allocator, countOfGalaxies);
for (rows.items, 0..) |rowData, yi| {
for (rowData.items, 0..) |cell, xi| {
if (cell == NodeType.Galaxy) {
try galaxies.append(Location{
.x = xi,
.y = yi,
});
}
}
}
return Self{
.rows = rows,
.allocator = allocator,
.galaxies = galaxies,
};
}

Expand All @@ -72,6 +94,7 @@ const Data = struct {
row.deinit();
}
self.rows.deinit();
self.galaxies.deinit();
}

pub fn printMap(self: *Self) void {
Expand All @@ -88,20 +111,32 @@ const Data = struct {
}
};

pub fn part1(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !i64 {
var sum: i64 = 0;
fn manhattan(loc1: Location, loc2: Location) usize {
var loc1x: i64 = @bitCast(loc1.x);
var loc1y: i64 = @bitCast(loc1.y);
var loc2x: i64 = @bitCast(loc2.x);
var loc2y: i64 = @bitCast(loc2.y);
return math.absCast(loc1x - loc2x) + math.absCast(loc1y - loc2y);
}

pub fn part1(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !usize {
var sum: usize = 0;

var data = try Data.init(allocator, list);
defer data.deinit();

data.printMap();
// data.printMap();

// access input data...
// for (data.rows.items) |rowData| {

// }
for (data.galaxies.items) |galaxy1| {
for (data.galaxies.items) |galaxy2| {
if (galaxy1.x == galaxy2.x and galaxy1.y == galaxy2.y) {
continue;
}
sum += manhattan(galaxy1, galaxy2);
}
}

return sum;
return @divTrunc(sum, 2);
}

test "part 1 test 1" {
Expand All @@ -119,16 +154,16 @@ test "part 1 test 1" {
);
defer list.deinit();

const testValue: i64 = try part1(std.testing.allocator, list);
try std.testing.expectEqual(testValue, -1);
const testValue: usize = try part1(std.testing.allocator, list);
try std.testing.expectEqual(testValue, 374);
}

test "part 1 full" {
var data = try util.openFile(std.testing.allocator, "data/input-11-1.txt");
defer data.deinit();

const testValue: i64 = try part1(std.testing.allocator, data.lines);
try std.testing.expectEqual(testValue, -1);
const testValue: usize = try part1(std.testing.allocator, data.lines);
try std.testing.expectEqual(testValue, 0);
}

pub fn part2(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !i64 {
Expand Down

0 comments on commit fa800af

Please sign in to comment.