Skip to content

Instantly share code, notes, and snippets.

@Arnavion
Created December 20, 2021 11:49
Show Gist options
  • Save Arnavion/3da5a40cd51236ec4c220d85f4b071a6 to your computer and use it in GitHub Desktop.
Save Arnavion/3da5a40cd51236ec4c220d85f4b071a6 to your computer and use it in GitHub Desktop.
// If lines 72 and 73 are uncommented and 74 is commented out, the assert on line 77 succeeds (as expected).
// If lines 72 and 73 are commented out and 74 is uncommented, the assert on line 77 fails.
//
// $ zig run -O Debug ./main.zig
//
// Linux x86_64, 0.9.0-dev.2010+e8b39960b
const std = @import("std");
const Scanner = struct {
beacons: [3]Beacon,
};
const Beacon = struct {
pos: Coord,
};
const Coord = struct {
p: i32,
q: i32,
r: i32,
};
pub fn main() anyerror!void {
var scanners: [2]Scanner = .{
.{
.beacons = .{
.{
.pos = .{ .p = -638, .q = 485, .r = 266 },
},
.{
.pos = .{ .p = -599, .q = 423, .r = 286 },
},
.{
.pos = .{ .p = -579, .q = 631, .r = 334 },
},
},
},
.{
.beacons = .{
.{
.pos = .{ .p = -848, .q = 747, .r = 643 },
},
.{
.pos = .{ .p = -828, .q = 809, .r = 682 },
},
.{
.pos = .{ .p = -780, .q = 601, .r = 702 },
},
},
},
};
identifyScanners(scanners[0..]);
}
fn identifyScanners(scanners: []Scanner) void {
const scanner1 = scanners[0];
const scanner2 = &scanners[1];
const beacon1a = scanner1.beacons[0];
const beacon1b = scanner1.beacons[1];
const beacon1c = scanner1.beacons[2];
const beacon2a = &scanner2.beacons[0];
const beacon2b = &scanner2.beacons[1];
const beacon2c = &scanner2.beacons[2];
for (scanner2.beacons) |*beacon| {
// TODO: Inlining the calculation of `new_pos` corrupts `beacon.pos`. UB or compiler bug?
// const new_pos = permuteCoord(beacon.pos, 7);
// beacon.pos = new_pos;
beacon.pos = permuteCoord(beacon.pos, 7);
}
std.debug.assert(coordsLineUp(
beacon1a.pos, beacon1b.pos, beacon1c.pos,
beacon2a.pos, beacon2b.pos, beacon2c.pos,
));
}
fn coordsLineUp(
a1: Coord, b1: Coord, c1: Coord,
a2: Coord, b2: Coord, c2: Coord,
) bool {
return
(b1.p - a1.p == b2.p - a2.p) and
(b1.q - a1.q == b2.q - a2.q) and
(b1.r - a1.r == b2.r - a2.r) and
(c1.p - a1.p == c2.p - a2.p) and
(c1.q - a1.q == c2.q - a2.q) and
(c1.r - a1.r == c2.r - a2.r) and
(c1.p - b1.p == c2.p - b2.p) and
(c1.q - b1.q == c2.q - b2.q) and
(c1.r - b1.r == c2.r - b2.r);
}
fn permuteCoord(coord: Coord, i: usize) Coord {
return switch (i) {
7 => .{ .p = coord.r, .q = -coord.q, .r = coord.p },
else => unreachable,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment