Challenge one
This commit is contained in:
commit
570172f32f
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/target
|
||||
**/*.rs.bk
|
4
Cargo.lock
generated
Normal file
4
Cargo.lock
generated
Normal file
@ -0,0 +1,4 @@
|
||||
[[package]]
|
||||
name = "AoC-2018"
|
||||
version = "0.1.0"
|
||||
|
14
Cargo.toml
Normal file
14
Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "AoC-2018"
|
||||
version = "0.1.0"
|
||||
authors = ["Kevin Cotugno <kevin@kevincotugno.com>"]
|
||||
|
||||
[[bin]]
|
||||
name = "AoC-2018"
|
||||
path = "src/bin/main.rs"
|
||||
|
||||
[lib]
|
||||
name = "aoc"
|
||||
path = "src/aoc/lib.rs"
|
||||
|
||||
[dependencies]
|
1018
input/01.txt
Normal file
1018
input/01.txt
Normal file
File diff suppressed because it is too large
Load Diff
75
src/aoc/c01.rs
Normal file
75
src/aoc/c01.rs
Normal file
@ -0,0 +1,75 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub fn run(input: &[String], part: u8) -> String {
|
||||
let encoded: Vec<_> = input.iter().map(|i| i.parse::<i32>().unwrap()).collect();
|
||||
match part {
|
||||
1 => c_01_1(&encoded),
|
||||
2 => c_01_2(&encoded),
|
||||
_ => panic!("unknown part"),
|
||||
}.to_string()
|
||||
}
|
||||
|
||||
fn c_01_1(input: &[i32]) -> i32 {
|
||||
let mut n = 0;
|
||||
for x in input {
|
||||
n += x;
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
fn c_01_2(input: &[i32]) -> i32 {
|
||||
let mut freq = HashSet::new();
|
||||
freq.insert(0);
|
||||
let mut n = 0;
|
||||
loop {
|
||||
for x in input {
|
||||
n += x;
|
||||
if freq.contains(&n) {
|
||||
return n;
|
||||
} else {
|
||||
freq.insert(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn c_01_1_examples() -> Result<(), String> {
|
||||
let mut examples: HashMap<&[i32], i32> = HashMap::new();
|
||||
examples.insert(&[1, 1, 1], 3);
|
||||
examples.insert(&[1, 1, -2], 0);
|
||||
examples.insert(&[-1, -2, -3], -6);
|
||||
|
||||
for (input, out) in examples.iter() {
|
||||
let actual = c_01_1(input);
|
||||
if &actual != out {
|
||||
return Err(format!("Got: {}, Want: {}", actual, out));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn c_01_2_examples() -> Result<(), String> {
|
||||
let mut examples: HashMap<&[i32], i32> = HashMap::new();
|
||||
examples.insert(&[1, -1], 0);
|
||||
examples.insert(&[3, 3, 4, -2, -4], 10);
|
||||
examples.insert(&[-6, 3, 8, 5, -6], 5);
|
||||
examples.insert(&[7, 7, -2, -7, -4], 14);
|
||||
|
||||
for (input, out) in examples.iter() {
|
||||
let actual = c_01_2(input);
|
||||
if &actual != out {
|
||||
return Err(format!("Got: {}, Want: {}", actual, out));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
8
src/aoc/lib.rs
Normal file
8
src/aoc/lib.rs
Normal file
@ -0,0 +1,8 @@
|
||||
mod c01;
|
||||
|
||||
pub fn run(input: &[String], challenge: u8, part: u8) -> String {
|
||||
match challenge {
|
||||
1 => c01::run(input, part),
|
||||
_ => panic!("challenge not implemented"),
|
||||
}
|
||||
}
|
24
src/bin/main.rs
Normal file
24
src/bin/main.rs
Normal file
@ -0,0 +1,24 @@
|
||||
extern crate aoc;
|
||||
|
||||
use std::env;
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let mut args = env::args();
|
||||
if args.len() < 3 {
|
||||
eprintln!("not enough args");
|
||||
}
|
||||
|
||||
args.next();
|
||||
let challenge: u8 = args.next().unwrap().parse().unwrap();
|
||||
let part: u8 = args.next().unwrap().parse().unwrap();
|
||||
|
||||
let lines: Vec<String> = fs::read_to_string(format!("input/{:02}.txt", challenge))
|
||||
.unwrap()
|
||||
.lines()
|
||||
.filter(|v| !v.trim().is_empty())
|
||||
.map(|v| v.to_string())
|
||||
.collect();
|
||||
|
||||
println!("{}", aoc::run(&lines, challenge, part));
|
||||
}
|
Loading…
Reference in New Issue
Block a user