Code Monkey home page Code Monkey logo

advent-of-code-2018's Introduction

Advent of Code 2018, in Rust

My solutions to Advent of Code 2018, which I'm doing in Rust (again).

I've been playing with Rust on and off for about a year, mostly off. I'm definitely still a beginner, and overall my programming logic is... spotty and informal. In general I strive for readability over brevity, and I rarely use exotic methods when a simpler one will do (even if it takes a few more lines).

(I used Rust for the 2017 AoC, too, but didn't get super far. I'm hoping to get 20 stars (out of a possible 50) by Christmas this year.)

How this repo is organized

Each day's challenge (1 through, hopefully-but-probably-not-all-the-way-to, 25) is a Rust executable in src/bin. Thus the code for, say, Day 2's executable is located in src/bin/day02.rs. To run the Day 2 executable, from the root directory run cargo run --bin day02. To run tests, if there are any, run cargo test --bin day02.

The input for each challenge is located in inputs and named by the day (so for example, inputs/day02.txt).

Blog posts I've written about Advent of Code 2018

  1. 9 (or So) Lessons from Days 1 and 2

Other folks using Rust for AoC 2018

Other folks posting their Rust solutions to GitHub. Plenty more discussion on r/adventofcode.

Some useful helper functions

A lot of the Advent of Code puzzles require similar io tasks. Here are some functions I use often, sometimes tweaking them. I may throw some or all of them into a file in src/lib and import them as needed, but for now I've just been copy-and-pasting them into the executables as I need them.

This one reads a multi-line text file into a Vector. The Vector will be whatever Rust type that Rust's parse function gets from the file.

// from https://github.com/sts10/eyeoh/blob/master/src/lib.rs#L33
use std::fs::File;
use std::io;
use std::io::BufRead;
use std::io::BufReader;
use std::str::FromStr;

fn read_by_line<T: FromStr>(file_path: &str) -> io::Result<Vec<T>> {
    let mut vec = Vec::new();
    let f = match File::open(file_path.trim_matches(|c| c == '\'' || c == ' ')) {
        Ok(res) => res,
        Err(e) => return Err(e),
    };
    let file = BufReader::new(&f);
    for line in file.lines() {
        match line?.parse() {
            Ok(l) => vec.push(l),
            Err(_e) => {
                panic!("Error reading a line of the file");
            }
        }
    }
    Ok(vec)
}

If the input file is only one line with tons of characters we're going to need to iterate through, this function is handy. It reads a text file into a Vector of chars (characters), which is usually what we want when doing AoC challenges.

use std::fs::File;
use std::io;
use std::io::prelude::*;

fn read_string_from_file_to_vector(file_path: &str) -> io::Result<Vec<char>> {
  let mut f = match File::open(file_path.trim_matches(|c| c == '\'' || c == ' ')) {
    Ok(res) => res,
    Err(e) => return Err(e),
  };
  let mut string_from_file = String::new();
  f.read_to_string(&mut string_from_file)
    .expect("something went wrong reading the file");

  let mut vector_of_chars = Vec::new();
  for c in string_from_file.chars() {
    vector_of_chars.push(c);
  }
  Ok(vector_of_chars)
}

advent-of-code-2018's People

Contributors

sts10 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.