Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,104 @@
with Ada.Text_IO, Ada.Command_Line, Ada.Numerics.Discrete_Random;
procedure Flip_Bits is
subtype Letter is Character range 'a' .. 'z';
Last_Col: constant letter := Ada.Command_Line.Argument(1)(1);
Last_Row: constant Positive := Positive'Value(Ada.Command_Line.Argument(2));
package Boolean_Rand is new Ada.Numerics.Discrete_Random(Boolean);
Gen: Boolean_Rand.Generator;
type Matrix is array
(Letter range 'a' .. Last_Col, Positive range 1 .. Last_Row) of Boolean;
function Rand_Mat return Matrix is
M: Matrix;
begin
for I in M'Range(1) loop
for J in M'Range(2) loop
M(I,J) := Boolean_Rand.Random(Gen);
end loop;
end loop;
return M;
end Rand_Mat;
function Rand_Mat(Start: Matrix) return Matrix is
M: Matrix := Start;
begin
for I in M'Range(1) loop
if Boolean_Rand.Random(Gen) then
for J in M'Range(2) loop
M(I,J) := not M(I, J);
end loop;
end if;
end loop;
for I in M'Range(2) loop
if Boolean_Rand.Random(Gen) then
for J in M'Range(1) loop
M(J,I) := not M(J, I);
end loop;
end if;
end loop;
return M;
end Rand_Mat;
procedure Print(Message: String; Mat: Matrix) is
package NIO is new Ada.Text_IO.Integer_IO(Natural);
begin
Ada.Text_IO.New_Line;
Ada.Text_IO.Put_Line(Message);
Ada.Text_IO.Put(" ");
for Ch in Matrix'Range(1) loop
Ada.Text_IO.Put(" " & Ch);
end loop;
Ada.Text_IO.New_Line;
for I in Matrix'Range(2) loop
NIO.Put(I, Width => 3);
for Ch in Matrix'Range(1) loop
Ada.Text_IO.Put(if Mat(Ch, I) then " 1" else " 0");
end loop;
Ada.Text_IO.New_Line;
end loop;
end Print;
Current, Target: Matrix;
Moves: Natural := 0;
begin
-- choose random Target and start ("Current") matrices
Boolean_Rand.Reset(Gen);
Target := Rand_Mat;
loop
Current := Rand_Mat(Target);
exit when Current /= Target;
end loop;
Print("Target:", Target);
-- print and modify Current matrix, until it is identical to Target
while Current /= Target loop
Moves := Moves + 1;
Print("Current move #" & Natural'Image(Moves), Current);
Ada.Text_IO.Put_Line("Flip row 1 .." & Positive'Image(Last_Row) &
" or column 'a' .. '" & Last_Col & "'");
declare
S: String := Ada.Text_IO.Get_Line;
function Let(S: String) return Character is (S(S'First));
function Val(Str: String) return Positive is (Positive'Value(Str));
begin
if Let(S) in 'a' .. Last_Col then
for I in Current'Range(2) loop
Current(Let(S), I) := not Current(Let(S), I);
end loop;
else
for I in Current'Range(1) loop
Current(I, Val(S)) := not Current(I, Val(S));
end loop;
end if;
end;
end loop;
-- summarize the outcome
Ada.Text_IO.Put_Line("Done after" & Natural'Image(Moves) & " Moves.");
end Flip_Bits;

View file

@ -0,0 +1,110 @@
import rand
import os
const size = 3
struct Board {
mut:
target [][]int
board [][]int
}
fn new_board() Board {
mut brd := Board{
target: [][]int{len: size, init: []int{len: size}},
board: [][]int{len: size, init: []int{len: size}},
}
for ial in 0 .. size {
for jal in 0 .. size {
brd.target[ial][jal] = rand.intn(2) or {panic("No Random!")}
}
}
return brd
}
fn (mut brd Board) flip_row(rir int) {
for cal in 0 .. size {
brd.board[rir][cal] = if brd.board[rir][cal] == 0 { 1 } else { 0 }
}
}
fn (mut brd Board) flip_col(cir int) {
for ral in 0 .. size {
brd.board[ral][cir] = if brd.board[ral][cir] == 0 { 1 } else { 0 }
}
}
fn (mut brd Board) init_board() {
for ial in 0 .. size {
for jal in 0 .. size {
brd.board[ial][jal] = brd.target[ial][jal]
}
}
for _ in 0 .. 9 {
rc := rand.intn(2) or {panic("No Random!")}
if rc == 0 { brd.flip_row(rand.intn(size) or {panic("No Random!")}) }
else { brd.flip_col(rand.intn(size) or {panic("No Random!")}) }
}
}
fn print_board(label string, a [][]int) {
println("$label:")
println(" | a b c")
println("---------")
for ral in 0 .. size {
print("${ral + 1} |")
for cal in 0 .. size {
print(" ${a[ral][cal]}")
}
println("")
}
println("")
}
fn (brd Board) game_over() bool {
for ral in 0 .. size {
for cal in 0 .. size {
if brd.board[ral][cal] != brd.target[ral][cal] { return false }
}
}
return true
}
fn main() {
mut brd := new_board()
mut flips, mut nir := 0, -1
mut is_row := true
// initialize board and ensure it differs from target
for {
brd.init_board()
if !brd.game_over() { break }
}
print_board("TARGET", brd.target)
print_board("OPENING BOARD", brd.board)
for {
is_row = true
nir = -1
for nir == -1 {
input := os.input("Enter row number or column letter to be flipped: ").str()
if input.len == 0 { continue }
ch := input[0].ascii_str().to_lower()
if ch !in ["1", "2", "3", "a", "brd", "c"] {
println("Must be 1, 2, 3, a, brd or c")
continue
}
if ch in ["1", "2", "3"] { nir = int(ch[0]) - int(`1`) }
else {
is_row = false
nir = int(ch[0]) - int(`a`)
}
}
flips++
if is_row { brd.flip_row(nir) }
else { brd.flip_col(nir) }
plural := if flips == 1 { "" } else { "S" }
print_board("\nBOARD AFTER $flips FLIP$plural", brd.board)
if brd.game_over() { break }
}
plural := if flips == 1 { "" } else { "s" }
println("You\'ve succeeded in $flips flip$plural")
}