diff options
Diffstat (limited to '05')
-rw-r--r-- | 05/Cargo.toml | 8 | ||||
-rw-r--r-- | 05/src/main.rs | 92 |
2 files changed, 100 insertions, 0 deletions
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 <ireneista@gmail.com>"] +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 +} |