Last active
October 18, 2022 01:55
-
-
Save zoeisnowooze/3fd5b2f592a82a1b79be386ff3c53c3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char **argv) { | |
int number_of_doors = atoi(argv[1]); | |
int number_of_passes = atoi(argv[2]); | |
int open_doors = 0; | |
for (int i = 1; i <= number_of_doors; i++) { | |
int toggles = 0; | |
for (int j = 1; j <= number_of_passes; j++) { | |
toggles += i % j == 0 ? 1 : 0; | |
} | |
open_doors += toggles % 2; | |
} | |
printf("%d\n", open_doors); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def odd_number_of_divisors(n, number_of_passes): | |
"""Counts the number of times the door is toggled, and returns True if left open after a number of passes.""" | |
return sum((n % i == 0 for i in range(1, number_of_passes + 1))) % 2 == 1 | |
def pass_doors(number_of_doors, number_of_passes): | |
if number_of_passes > number_of_doors: | |
number_of_passes = number_of_doors | |
return sum( | |
( | |
odd_number_of_divisors(i, number_of_passes) | |
for i in range(1, number_of_doors + 1) | |
) | |
) | |
if __name__ == "__main__": | |
print(pass_doors(int(sys.argv[1]), int(sys.argv[2]))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
pub fn count_open_doors(number_of_doors: usize, number_of_passes: usize) usize { | |
var open_doors: usize = 0; | |
var i: usize = 1; | |
while (i <= number_of_doors) : (i += 1) { | |
var j: usize = 1; | |
var toggles: usize = 0; | |
while (j <= number_of_passes) : (j += 1) { | |
toggles += @boolToInt(i % j == 0); | |
} | |
open_doors += toggles % 2; | |
} | |
return open_doors; | |
} | |
pub fn main() !void { | |
const allocator = std.heap.page_allocator; | |
const args = try std.process.argsAlloc(allocator); | |
defer std.process.argsFree(allocator, args); | |
if (args.len < 3) return error.ExpectedArgument; | |
const number_of_doors: usize = try std.fmt.parseInt(usize, args[1], 10); | |
const number_of_passes: usize = try std.fmt.parseInt(usize, args[2], 10); | |
const open_doors: usize = count_open_doors(number_of_doors, number_of_passes); | |
const stdout = std.io.getStdOut().writer(); | |
try stdout.print("{}\n", .{open_doors}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment