From be489d9b3bfe62d53807e489910be0d1fd45ef98 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Fri, 4 Dec 2020 21:57:15 -0800 Subject: 05, and some stray stuff from 04, oops --- 04/Cargo.toml | 9 ++++ 04/src/main.rs | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 05/Cargo.toml | 8 ++++ 05/src/main.rs | 92 +++++++++++++++++++++++++++++++++++++++ Cargo.lock | 7 +++ Cargo.toml | 1 + 6 files changed, 251 insertions(+) create mode 100644 04/Cargo.toml create mode 100644 04/src/main.rs create mode 100644 05/Cargo.toml create mode 100644 05/src/main.rs diff --git a/04/Cargo.toml b/04/Cargo.toml new file mode 100644 index 0000000..00ee1d2 --- /dev/null +++ b/04/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "advent_04" +version = "0.1.0" +authors = ["Irene Knapp "] +edition = "2018" + +[dependencies] +advent_lib = { path = "../lib" } +regex = "1" diff --git a/04/src/main.rs b/04/src/main.rs new file mode 100644 index 0000000..701f60d --- /dev/null +++ b/04/src/main.rs @@ -0,0 +1,134 @@ +use advent_lib::prelude::*; + +use regex::Regex; +use std::collections::BTreeSet; + + +fn main() -> Result<()> { + let mut args = std::env::args(); + if args.len() != 2 { + eprintln!("Usage: advent input"); + } + let _ = args.next(); + let filename = args.next().unwrap(); + + let input = advent_lib::read_lines_file(&filename)?; + + let overall_regex = Regex::new(r"([a-z][a-z][a-z]):([^ \n]*)").unwrap(); + let year_regex = Regex::new(r"^[0-9][0-9][0-9][0-9]$").unwrap(); + let height_regex = Regex::new(r"^([0-9]+)(cm|in)$").unwrap(); + let rgb_regex = Regex::new(r"^#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$").unwrap(); + let eye_regex = Regex::new(r"^(amb|blu|brn|gry|grn|hzl|oth)$").unwrap(); + let id_regex = Regex::new(r"^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$").unwrap(); + + let mut n_valid: i64 = 0; + let mut current_fields: BTreeSet = BTreeSet::new(); + let mut current_is_valid = true; + + for line in &input { + if line.trim().len() == 0 { + //println!("{:?}", current_fields); + + if current_is_valid + && current_fields.contains("byr") + && current_fields.contains("iyr") + && current_fields.contains("eyr") + && current_fields.contains("hgt") + && current_fields.contains("hcl") + && current_fields.contains("ecl") + && current_fields.contains("pid") { + n_valid += 1; + } + + current_fields = BTreeSet::new(); + current_is_valid = true; + } + + for captures in overall_regex.captures_iter(line) { + let mut field_name = String::new(); + field_name.push_str(&captures[1]); + + let mut field_value = String::new(); + field_value.push_str(&captures[2]); + + current_fields.insert(field_name); + + match &captures[1] { + "byr" => { + if year_regex.is_match(&field_value) { + let year = field_value.parse::()?; + if year < 1920 || year > 2002 { + current_is_valid = false; + } + } else { + current_is_valid = false; + } + }, + "iyr" => { + if year_regex.is_match(&field_value) { + let year = field_value.parse::()?; + if year < 2010 || year > 2020 { + current_is_valid = false; + } + } else { + current_is_valid = false; + } + }, + "eyr" => { + if year_regex.is_match(&field_value) { + let year = field_value.parse::()?; + if year < 2020 || year > 2030 { + current_is_valid = false; + } + } else { + current_is_valid = false; + } + }, + "hgt" => { + if height_regex.is_match(&field_value) { + let captures = height_regex.captures(&field_value).unwrap(); + let number = captures[1].parse::()?; + + match &captures[2] { + "cm" => { + if number < 150 || number > 193 { + current_is_valid = false; + } + }, + "in" => { + if number < 59 || number > 76 { + current_is_valid = false; + } + }, + _ => { }, + } + } else { + current_is_valid = false; + } + }, + "hcl" => { + if !rgb_regex.is_match(&field_value) { + current_is_valid = false; + } + }, + "ecl" => { + if !eye_regex.is_match(&field_value) { + current_is_valid = false; + } + }, + "pid" => { + if !id_regex.is_match(&field_value) { + current_is_valid = false; + } + }, + _ => { + }, + }; + } + //println!("end of line"); + } + + println!("{}", n_valid); + + Ok(()) +} diff --git a/05/Cargo.toml b/05/Cargo.toml new file mode 100644 index 0000000..4e5fdbd --- /dev/null +++ b/05/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "advent_05" +version = "0.1.0" +authors = ["Irene Knapp "] +edition = "2018" + +[dependencies] +advent_lib = { path = "../lib" } diff --git a/05/src/main.rs b/05/src/main.rs new file mode 100644 index 0000000..3773be8 --- /dev/null +++ b/05/src/main.rs @@ -0,0 +1,92 @@ +use advent_lib::prelude::*; + +use std::collections::BTreeSet; + + +fn main() -> Result<()> { + let mut args = std::env::args(); + if args.len() != 2 { + eprintln!("Usage: advent input"); + } + let _ = args.next(); + let filename = args.next().unwrap(); + + let input = advent_lib::read_lines_file(&filename)?; + + let mut max_id = 0; + let mut found_ids = BTreeSet::new(); + + for line in &input { + let (row, column) = decode_seat(&line); + let id = seat_id(row, column); + + found_ids.insert(id); + + if id > max_id { + max_id = id; + } + + } + + println!("{}", max_id); + + let mut is_started = false; + + for i in 0 .. max_id { + if !is_started { + if found_ids.contains(&i) { + is_started = true; + } + } else { + if !found_ids.contains(&i) { + println!("{}", i); + break; + } + } + } + + Ok(()) +} + +fn decode_seat(seat_spec: &str) -> (i64, i64) { + let mut row: i64 = 0; + let mut column: i64 = 0; + + if seat_spec.chars().nth(0) == Some('B') { + row += 64; + } + if seat_spec.chars().nth(1) == Some('B') { + row += 32; + } + if seat_spec.chars().nth(2) == Some('B') { + row += 16; + } + if seat_spec.chars().nth(3) == Some('B') { + row += 8; + } + if seat_spec.chars().nth(4) == Some('B') { + row += 4; + } + if seat_spec.chars().nth(5) == Some('B') { + row += 2; + } + if seat_spec.chars().nth(6) == Some('B') { + row += 1; + } + if seat_spec.chars().nth(7) == Some('R') { + column += 4; + } + if seat_spec.chars().nth(8) == Some('R') { + column += 2; + } + if seat_spec.chars().nth(9) == Some('R') { + column += 1; + } + + (row, column) +} + + +fn seat_id(row: i64, column: i64) -> i64 { + row * 8 + column +} diff --git a/Cargo.lock b/Cargo.lock index 130b90f..8ccce11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,6 +30,13 @@ dependencies = [ "regex", ] +[[package]] +name = "advent_05" +version = "0.1.0" +dependencies = [ + "advent_lib", +] + [[package]] name = "advent_lib" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 8b59a0a..682e0be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,5 @@ members = [ "02", "03", "04", + "05", ] -- cgit 1.4.1