add day 13, part 2

This commit is contained in:
Christian Ulrich 2022-12-14 02:01:59 +01:00
parent a8e74eae93
commit 3c0336b872
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
2 changed files with 39 additions and 1 deletions

View File

@ -16,7 +16,7 @@
use std::cmp::{Ordering, PartialOrd};
use std::io;
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Eq, Ord)]
pub enum Token {
Integer(u64),
List(Vec<Token>),

38
day13/part2.rs Normal file
View File

@ -0,0 +1,38 @@
// 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(())
}