From: Greg Burri Date: Wed, 1 Jan 2020 19:18:28 +0000 (+0100) Subject: Day 4 part 1&2 X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=53944733c3d9d9d2cabbb1ddb07fe29fbda10aa3;p=advent_of_code_2019.git Day 4 part 1&2 --- diff --git a/src/common.rs b/src/common.rs index b3db167..f22fbfd 100644 --- a/src/common.rs +++ b/src/common.rs @@ -9,7 +9,7 @@ where T::Err: std::fmt::Debug { - fs::read_to_string(file).unwrap().split(sep).map(|line| line.parse::().unwrap()).collect() + fs::read_to_string(file).unwrap().split(sep).map(|line| line.trim().parse::().unwrap()).collect() } pub fn layer_to_printable_string(layer: &[u8], width: usize) -> String { diff --git a/src/day04.rs b/src/day04.rs index 8a5064f..42fb047 100644 --- a/src/day04.rs +++ b/src/day04.rs @@ -1,16 +1,90 @@ +use std::cmp::Ordering; +pub fn parse_range(raw: &str) -> (i32, i32) { + let nums: Vec = raw.trim().split('-').map(|n| n.parse::().unwrap()).collect(); + (nums[0], nums[1]) +} + +type Digits = Vec; + +fn get_digits(value: i32) -> Digits { + let mut digits = Vec::::new(); + let mut value = value; + while value > 0 { + digits.push((value % 10) as u8); + value /= 10; + } + digits +} -fn find_password(min: i32) { - ; +pub fn nb_passwords_part1(min: i32, max: i32) -> i32 { + nb_passwords( + min, + max, + &|digits: &Digits| { + for i in 1 .. digits.len() { + if digits[i - 1] == digits[i] { return true; } + } + false + } + ) } +pub fn nb_passwords_part2(min: i32, max: i32) -> i32 { + nb_passwords( + min, + max, + &|digits: &Digits| { + let mut last = digits[0]; + let mut n = 1; + for i in 1 .. digits.len() { + if digits[i] == last { + n += 1; + } else { + if n == 2 { return true; } + n = 1; + } + last = digits[i]; + } + n == 2 + } + ) +} + +fn nb_passwords(min: i32, max: i32, valid_password: &dyn Fn(&Digits) -> bool) -> i32 { + let mut digits = get_digits(min); + let digits_max = get_digits(max); + let l = digits.len(); + + fn set_range(from: usize, to: usize, value: u8, digits: &mut Digits) { + for i in from .. to { digits[i] = value; } + }; + + for i in (1 .. l).rev() { + if digits[i - 1] < digits[i] { + set_range(0, i, digits[i], &mut digits); + break; + } + } + + let mut n = 0; + loop { + if valid_password(&digits) { n += 1; } -#[cfg(test)] -mod tests { - use super::*; + for i in 0 .. l { + if i == l - 1 || digits[i + 1] <= digits[i] && digits[i] != 9 { + set_range(0, i + 1, digits[i] + 1, &mut digits); + break; + } + } - #[test] - fn part1() { + for i in (0 .. l).rev() { + match digits[i].cmp(&digits_max[i]) { + Ordering::Greater => return n, + Ordering::Less => break, + Ordering::Equal => () + } + } } } \ No newline at end of file diff --git a/src/day17.rs b/src/day17.rs index f120506..52d5988 100644 --- a/src/day17.rs +++ b/src/day17.rs @@ -139,7 +139,7 @@ impl RobotTrackingSystem { } impl intcode::IO for RobotTrackingSystem { - // May block. + // Read instructions fn read(&mut self) -> i64 { self.build_board_from_output(); 42 // TODO: part2. diff --git a/src/day18.rs b/src/day18.rs new file mode 100644 index 0000000..ff90b85 --- /dev/null +++ b/src/day18.rs @@ -0,0 +1,19 @@ + +pub fn parse(input: &str) { + +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_sample1() { + let input = + "######### + #b.A.@.a# + #########"; + + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index c66dfc9..71eec79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod intcode; mod day01; mod day02; mod day03; +mod day04; mod day06; mod day07; mod day08; @@ -18,6 +19,7 @@ mod day14; mod day15; mod day16; mod day17; +mod day18; fn day01() -> String { let masses = common::read_list_of_numbers("data/day01.input", "\n"); @@ -40,7 +42,9 @@ fn day03() -> String { } fn day04() -> String { - format!("") + let raw = fs::read_to_string("data/day04.input").unwrap(); + let (min, max) = day04::parse_range(&raw); + format!("part1: {:?}, part2: {}", day04::nb_passwords_part1(min, max), day04::nb_passwords_part2(min, max)) } fn day05() -> String { @@ -136,6 +140,10 @@ fn day17() -> String { } +fn day18() -> String { + format!("part1: {}, part2: {}", "", "") +} + fn format_micros(t: u128) -> String { if t < 10_000 { format!("{} μs", t) @@ -172,6 +180,7 @@ fn main() { day15, day16, day17, + day18, ); let args: Vec = env::args().skip(1).collect();