#letter #ascii #advent-of-code #puzzle #representation #containing #convert

advent-ocr

Converts ASCII-art representations of letters generated by Advent of Code puzzles into a String containing those letters

2 releases

0.1.3 Oct 29, 2024
0.1.2 Oct 29, 2024
0.1.1 Oct 29, 2024
0.1.0 Oct 29, 2024

#890 in Text processing

Download history 347/week @ 2024-10-28 25/week @ 2024-11-04 3/week @ 2024-11-18 22/week @ 2024-12-02 26/week @ 2024-12-09

51 downloads per month

MIT/Apache

19KB
301 lines

advent-ocr

A Rust function to convert ASCII-art representations of letters generated by Advent of Code puzzles into a String containing those letters. Through 2023, these puzzles are:

  • 2016, Day 8
  • 2018, Day 10
  • 2019, Days 8 and 11
  • 2011, Day 13
  • 2022, Day 10

Installation

Add this to your Cargo.toml file:

[dependencies]
advent-ocr = "0.1.3"

Usage

There is one function, ocr(), that takes one argument, image.

image can take the following types:

  • &str
  • (&Vec<bool>, usize), a tuple consisting of a Vec of bools and the width of a line.
  • (&Vec<bool>, usize), a tuple consisting of a Vec of chars and the width of a line.
  • &Vec<Vec<bool>>, a Vec of a Vec of bools.
  • &Vec<Vec<char>>, a Vec of a Vec of chars.

For &str and the char-based Vecs, '#' is considered part of a letter and all other chars are considered blank space. For the bool-based Vecs, true is considered part of a letter and false is considered blank space.

use advent_ocr::ocr

let image = r"
.##..###...##.
#..#.#..#.#..#
#..#.###..#...
####.#..#.#...
#..#.#..#.#..#
#..#.###...##.
    ";

let s = ocr(image);
println!("{s}"); // prints "ABC"

Warning/Credits

This library recognizes the two font sizes used in Advent of Code puzzles, but neither font alphabet is complete. This draws on the efforts of mstksg and possibly others. If the function does not recognize a letter, send me a message with the image that failed to render, and I will add it!

License

Licensed under either of

at your option.

No runtime deps