Created
August 6, 2023 17:19
-
-
Save natemcintosh/9903716ade9fa76aa95f931c035798b8 to your computer and use it in GitHub Desktop.
A solver for [making the rounds](https://thefiddler.substack.com/i/135704603/making-the-rounds)
This file contains hidden or 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
| using Combinatorics | |
| using Test | |
| #= | |
| https://thefiddler.substack.com/i/135704603/making-the-rounds | |
| Sixteen chocolates sit in a four-by-four box. Can you remove six from the box so | |
| that an even number is left in each row and each column? | |
| Once you’ve solved it, you can further challenge yourself by counting how many | |
| different ways you can choose six chocolates to remove such that an even number | |
| is left in each row and column. Counting ways to arrange dots in a grid … now | |
| where have I seen this before? | |
| =# | |
| """ | |
| Idea here is to find a solution to the problem listed above. | |
| """ | |
| function find_soln()::Vector{BitMatrix} | |
| solns = Vector{BitMatrix}() | |
| box = BitMatrix(1 for _ = 1:4, _ = 1:4) | |
| for combo_inds in combinations(1:16, 6) | |
| # Set these indices to false | |
| box[combo_inds] .= false | |
| # Check for evenness | |
| if all_even(box) | |
| push!(solns, copy(box)) | |
| end | |
| # Make sure we're back to an array of ones | |
| fill!(box, true) | |
| end | |
| return solns | |
| end | |
| """ | |
| Check if even number in each row and each column | |
| """ | |
| function all_even(box::BitMatrix)::Bool | |
| if any(isodd(count(r)) for r in eachrow(box)) | |
| return false | |
| end | |
| if any(isodd(count(c)) for c in eachcol(box)) | |
| return false | |
| end | |
| true | |
| end | |
| @testset "all_even" begin | |
| b = BitMatrix(1 for _ = 1:4, _ = 1:4) | |
| @test all_even(b) == true | |
| b[1] = false | |
| @test all_even(b) == false | |
| b[1:2, 1:2] .= false | |
| @test all_even(b) == true | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment