Implement day 1 2018.

We've started.
This commit is contained in:
2018-12-01 11:03:48 +01:00
parent c41099d9d5
commit 3bbe6a479a
5 changed files with 1113 additions and 33 deletions

View File

@@ -13,4 +13,4 @@ cache: cargo
# Custom directory, for the correct year # Custom directory, for the correct year
before_script: before_script:
- cd 2016 - cd 2018

1025
2018/inputs/01 Normal file

File diff suppressed because it is too large Load Diff

79
2018/src/day01.rs Normal file
View File

@@ -0,0 +1,79 @@
use std::collections::HashSet;
use std::io;
use std::io::prelude::*;
use common;
pub struct Day01 {}
impl Day01 {
pub fn new() -> Day01 {
Day01 {}
}
}
impl common::Solution for Day01 {
fn part1(&mut self, input: &mut io::Read) -> String {
let reader = io::BufReader::new(input);
let sum: i32 = reader.lines()
.map(|x| x.unwrap().parse::<i32>().unwrap())
.sum();
format!("{}", sum)
}
fn part2(&mut self, input: &mut io::Read) -> String {
let reader = io::BufReader::new(input);
let mut freqs = HashSet::new();
freqs.insert(0);
let mut sum = 0;
let nums: Vec<i32> = reader.lines()
.map(|x| x.unwrap().parse().unwrap())
.collect();
loop {
for amount in &nums {
sum += amount;
if freqs.contains(&sum) {
return format!("{}", sum);
} else {
freqs.insert(sum);
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use common::Solution;
#[test]
fn samples_part1() {
let mut instance = Day01::new();
let sample1 = "+1\n+1\n+1";
let sample2 = "+1\n+1\n-2";
let sample3 = "-1\n-2\n-3";
assert_eq!("3", instance.part1(&mut sample1.as_bytes()));
assert_eq!("0", instance.part1(&mut sample2.as_bytes()));
assert_eq!("-6", instance.part1(&mut sample3.as_bytes()));
}
#[test]
fn samples_part2() {
let mut instance = Day01::new();
let sample1 = "+1\n-1";
let sample2 = "+3\n+3\n+4\n-2\n-4";
let sample3 = "-6\n+3\n+8\n+5\n-6";
let sample4 = "+7\n+7\n-2\n-7\n-4";
assert_eq!("0", instance.part2(&mut sample1.as_bytes()));
assert_eq!("10", instance.part2(&mut sample2.as_bytes()));
assert_eq!("5", instance.part2(&mut sample3.as_bytes()));
assert_eq!("14", instance.part2(&mut sample4.as_bytes()));
}
}

View File

@@ -1,21 +0,0 @@
use std::io;
use common;
pub struct Day1 {
}
impl Day1 {
pub fn new() -> Day1 {
Day1{}
}
}
impl common::Solution for Day1 {
fn part1(&mut self, input: &mut io::Read) -> String {
panic!("Not implemented");
}
fn part2(&mut self, input: &mut io::Read) -> String {
panic!("Not implemented");
}
}

View File

@@ -4,14 +4,13 @@ use std::fs;
use std::io; use std::io;
pub mod common; pub mod common;
pub mod day1; pub mod day01;
fn get_impl(day: i32) -> Box<common::Solution> { fn get_impl(day: &str) -> Box<common::Solution> {
match day { match day.parse() {
1 => { Box::new(day1::Day1::new()) } Ok(1) => { Box::new(day01::Day01::new()) }
_ => { Ok(val) => panic!("Unimplemented day {}", val),
panic!("Unimplemented day {}", day) _ => panic!("Invalid number"),
}
} }
} }
@@ -35,9 +34,7 @@ fn main() {
.takes_value(true)) .takes_value(true))
.get_matches(); .get_matches();
let day: i32 = (&matches.value_of("day").unwrap()).parse() let mut implementation = get_impl(matches.value_of("day").unwrap());
.expect("Invalid int");
let mut implementation = get_impl(day);
let mut data: Box<io::Read> = match matches.value_of("input") { let mut data: Box<io::Read> = match matches.value_of("input") {
Some(filename) => { Box::new(fs::File::open(filename).unwrap()) } Some(filename) => { Box::new(fs::File::open(filename).unwrap()) }
None => { Box::new(io::stdin()) } None => { Box::new(io::stdin()) }
@@ -59,7 +56,7 @@ mod tests {
// Verify that we can load all days // Verify that we can load all days
let last_implemented = 1; let last_implemented = 1;
for d in 1..(last_implemented + 1) { for d in 1..(last_implemented + 1) {
get_impl(d); get_impl(&format!("{}", d));
} }
} }
} }