move everything to single crate

This commit is contained in:
Antoine Martin 2019-11-15 00:14:07 +01:00
parent 1ee14b228c
commit b4c1f36767
10 changed files with 38 additions and 54 deletions

147
aoc2018/src/day01.rs Normal file
View file

@ -0,0 +1,147 @@
use std::collections::HashSet;
use super::Result;
const INPUT: &str = include_str!("../input/day01.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
Ok(())
}
fn part1(input: &str) -> Result<i32> {
let freq = input
.lines()
.map(|line| line.parse::<i32>().unwrap())
.sum::<i32>();
Ok(freq)
}
fn part2(input: &str) -> Result<i32> {
let mut freqs = HashSet::new();
let mut freq = 0;
loop {
for line in input.lines() {
if freqs.contains(&freq) {
return Ok(freq);
} else {
freqs.insert(freq);
}
freq += line.parse::<i32>()?;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part1_provided1() {
let input = "+1
-2
+3
+1
";
assert_eq!(part1(input).unwrap(), 3);
}
#[test]
fn part1_provided2() {
let input = "+1
+1
+1
";
assert_eq!(part1(input).unwrap(), 3);
}
#[test]
fn part1_provided3() {
let input = "+1
+1
-2
";
assert_eq!(part1(input).unwrap(), 0);
}
#[test]
fn part1_provided4() {
let input = "-1
-2
-3
";
assert_eq!(part1(input).unwrap(), -6);
}
#[test]
fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 427);
}
#[test]
fn part2_provided1() {
let input = "+1
-2
+3
+1
";
assert_eq!(part2(input).unwrap(), 2);
}
#[test]
fn part2_provided2() {
let input = "+1
-1
";
assert_eq!(part2(input).unwrap(), 0);
}
#[test]
fn part2_provided3() {
let input = "+3
+3
+4
-2
-4
";
assert_eq!(part2(input).unwrap(), 10);
}
#[test]
fn part2_provided4() {
let input = "-6
+3
+8
+5
-6
";
assert_eq!(part2(input).unwrap(), 5);
}
#[test]
fn part2_provided5() {
let input = "+7
+7
-2
-7
-4
";
assert_eq!(part2(input).unwrap(), 14);
}
#[test]
fn part2_real() {
assert_eq!(part2(INPUT).unwrap(), 341);
}
}

57
aoc2018/src/day02.rs Normal file
View file

@ -0,0 +1,57 @@
use std::collections::HashMap;
use super::Result;
const INPUT: &str = include_str!("../input/day02.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
Ok(())
}
fn part1(input: &str) -> Result<u32> {
let mut twice = 0;
let mut thrice = 0;
for line in input.lines() {
let mut seen: HashMap<char, u32> = HashMap::new();
for c in line.chars() {
*seen.entry(c).or_default() += 1;
}
if seen.values().any(|x| *x == 2) {
twice += 1;
}
if seen.values().any(|x| *x == 3) {
thrice += 1;
}
}
Ok(twice * thrice)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part1_provided() {
let input = "abcdef
bababc
abbcde
abcccd
aabcdd
abcdee
ababab
";
assert_eq!(part1(input).unwrap(), 12);
}
#[test]
fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 5750);
}
}

4
aoc2018/src/lib.rs Normal file
View file

@ -0,0 +1,4 @@
pub mod day01;
pub mod day02;
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

19
aoc2018/src/main.rs Normal file
View file

@ -0,0 +1,19 @@
use std::env;
use aoc2018::day01;
use aoc2018::day02;
use aoc2018::Result;
fn main() -> Result<()> {
let days = [day01::run, day02::run];
let mut args = env::args();
args.next();
let day = args
.next()
.expect("Please provide a day to launch")
.parse::<usize>()?;
days[day - 1]()
}