adventofcode2022/day13/part2.rs

39 lines
1.3 KiB
Rust

// Copyright 2022 Christian Ulrich
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// usage: ./part1 < input
pub mod common;
use common::Token;
fn main() -> Result<(), &'static str> {
let mut packets = Vec::new();
for pair in common::packet_pairs() {
let (left, right) = pair?;
packets.push(left);
packets.push(right);
}
let divider1 = Token::from_str("[[2]]")?;
let divider2 = Token::from_str("[[6]]")?;
packets.push(divider1.clone());
packets.push(divider2.clone());
packets.sort();
let divider1_pos = packets.iter().position(|p| p == &divider1).unwrap() + 1;
let divider2_pos = packets.iter().position(|p| p == &divider2).unwrap() + 1;
println!("{}", divider1_pos * divider2_pos);
Ok(())
}