Created
December 2, 2017 04:36
-
-
Save bearcage/edb99eab9da20b9ab6941b64450e26ef to your computer and use it in GitHub Desktop.
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
| // I got mad at the typesystem while I was learning, and when it told me I | |
| // wasn't allowed to add a u32 and a u64 together and save the result in a u64, | |
| // I decided to force the issue instead of learning that the syntax for that | |
| // cast was spelled "as". Since my first working puzzle implementation in rust | |
| // involved inline assembly, I thought I should own that shame and keep it here. | |
| #![feature(asm)] | |
| extern crate clap; | |
| fn fucking_add(a: u64, b: u32) -> u64 { | |
| let out: u64; | |
| unsafe { | |
| asm!("mov edx, $2\nadd $0, rdx" | |
| : "=r"(out) | |
| : "0"(a), "r"(b) | |
| : "rdx" | |
| : "intel"); | |
| } | |
| out | |
| } | |
| pub fn sum_runs(input: &str, halfskip_on: bool) -> u64 { | |
| let NUMERIC_BASE = 10; | |
| if input.len() < 2 { return 0; } | |
| let mut running_total: u64 = 0; | |
| for (i, c) in input.chars().enumerate() { | |
| let compare_index = match(halfskip_on) { | |
| true => (i + input.len() / 2), | |
| false => i + 1 | |
| } % input.len(); | |
| if c == input.chars().nth(compare_index).unwrap() { | |
| match c.to_digit(NUMERIC_BASE) { | |
| Some(digit) => { | |
| running_total = fucking_add(running_total, digit) | |
| }, | |
| None => println!("Ignoring non-digit {}.", c), | |
| } | |
| } | |
| } | |
| running_total | |
| } | |
| fn main() { | |
| let cli = clap::App::new("day-one") | |
| .version("0.0.0") | |
| .about("Solves day 1 problems in adventofcode") | |
| .arg(clap::Arg::with_name("captcha-number") | |
| .required(true) | |
| .index(1)) | |
| .arg(clap::Arg::with_name("skip-halfway-in-list") | |
| .short("s") | |
| .long("halfskip")) | |
| .get_matches(); | |
| let halfskip_on: bool = cli.is_present("skip-halfway-in-list"); | |
| let input = cli.value_of("captcha-number").unwrap(); | |
| println!("Sum of runs: {}", sum_runs(input, halfskip_on)); | |
| } | |
| #[cfg(test)] | |
| mod part_one_tests { | |
| use super::*; | |
| #[test] | |
| fn given_1() { | |
| assert_eq!(sum_runs("1122", false), 3); | |
| } | |
| #[test] | |
| fn given_2() { | |
| assert_eq!(sum_runs("1111", false), 4); | |
| } | |
| #[test] | |
| fn given_3() { | |
| assert_eq!(sum_runs("1234", false), 0); | |
| } | |
| #[test] | |
| fn given_4() { | |
| assert_eq!(sum_runs("91212129", false), 9); | |
| } | |
| } | |
| #[cfg(test)] | |
| mod part_two_tests { | |
| use super::*; | |
| #[test] | |
| fn given_1() { | |
| assert_eq!(sum_runs("1212", true), 6); | |
| } | |
| #[test] | |
| fn given_2() { | |
| assert_eq!(sum_runs("1221", true), 0); | |
| } | |
| #[test] | |
| fn given_3() { | |
| assert_eq!(sum_runs("123425", true), 4); | |
| } | |
| #[test] | |
| fn given_4() { | |
| assert_eq!(sum_runs("123123", true), 12); | |
| } | |
| #[test] | |
| fn given_5() { | |
| assert_eq!(sum_runs("12131415", true), 4); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment