diff options
author | Martin Ashby <martin@ashbysoft.com> | 2022-12-03 10:37:08 +0000 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2022-12-03 10:37:08 +0000 |
commit | a1e913e617bf6b2637c0cac56adabe980d27387f (patch) | |
tree | 7011976e8c34b2a02dd2beeef241829bedeb06e1 /src | |
download | aoc2022-a1e913e617bf6b2637c0cac56adabe980d27387f.tar.gz aoc2022-a1e913e617bf6b2637c0cac56adabe980d27387f.tar.bz2 aoc2022-a1e913e617bf6b2637c0cac56adabe980d27387f.tar.xz aoc2022-a1e913e617bf6b2637c0cac56adabe980d27387f.zip |
Days 1-3
Diffstat (limited to 'src')
-rw-r--r-- | src/day1.rs | 27 | ||||
-rw-r--r-- | src/day2.rs | 124 | ||||
-rw-r--r-- | src/day3.rs | 46 | ||||
-rw-r--r-- | src/main.rs | 11 |
4 files changed, 208 insertions, 0 deletions
diff --git a/src/day1.rs b/src/day1.rs new file mode 100644 index 0000000..8aa966b --- /dev/null +++ b/src/day1.rs @@ -0,0 +1,27 @@ +struct Foo { + acc: u32, + max: [u32;3], +} + +pub fn run(input: String) { + let init = Foo{ + acc: 0, + max: [0,0,0], + }; + let res = input.lines().fold(init, |foo, line| { + return if line.is_empty() { + let mut arr = [foo.acc, foo.max[0], foo.max[1], foo.max[2]]; + arr.sort(); + Foo { + acc: 0, + max: [arr[1], arr[2], arr[3]], + } + } else { + Foo { + acc: foo.acc + line.parse::<u32>().expect("input line wasn't a u32!"), + max: foo.max, + } + } + }); + println!("day 1: {}", res.max.into_iter().sum::<u32>()); +}
\ No newline at end of file diff --git a/src/day2.rs b/src/day2.rs new file mode 100644 index 0000000..15a99de --- /dev/null +++ b/src/day2.rs @@ -0,0 +1,124 @@ +// Accumulator structure... +// parse each line + +use simple_error::{SimpleError,bail}; + +#[derive(PartialEq)] +enum Move{ Rock, Paper, Scissors } +enum Res { Win, Draw, Loss } + +fn parse_m1(c: char) -> Result<Move, SimpleError> { + match c { + 'A' => Ok(Move::Rock), + 'B' => Ok(Move::Paper), + 'C' => Ok(Move::Scissors), + _ => bail!("invalid character {}", c) + } +} +// fn parse_m2(c: char) -> Result<Move, SimpleError> { +// match c { +// 'X' => Ok(Move::Rock), +// 'Y' => Ok(Move::Paper), +// 'Z' => Ok(Move::Scissors), +// _ => bail!("invalid character {}", c) +// } +// } +fn parse_req_res(c: char) -> Result<Res, SimpleError> { + match c { + 'X' => Ok(Res::Loss), + 'Y' => Ok(Res::Draw), + 'Z' => Ok(Res::Win), + _ => bail!("invalid character {}", c) + } +} +// fn win(opp: Move, you: Move) -> Res { +// match opp { +// Move::Rock => match you { +// Move::Rock => Res::Draw, +// Move::Paper => Res::Win, +// Move::Scissors => Res::Loss, +// }, +// Move::Paper => match you { +// Move::Rock => Res::Loss, +// Move::Paper => Res::Draw, +// Move::Scissors => Res::Win, +// }, +// Move::Scissors => match you { +// Move::Rock => Res::Win, +// Move::Paper => Res::Loss, +// Move::Scissors => Res::Draw, +// } +// } +// } +fn calc_req_move(opp: Move, req_res: Res) -> Move { + match req_res { + Res::Win => match opp { + Move::Rock => Move::Paper, + Move::Paper => Move::Scissors, + Move::Scissors => Move::Rock + } + Res::Draw => opp, + Res::Loss => match opp { + Move::Rock => Move::Scissors, + Move::Paper => Move::Rock, + Move::Scissors => Move::Paper + } + } +} + +// fn score(opp: Move, you: Move) -> u32 { +// let ms: u32 = match you { +// Move::Rock => 1, +// Move::Paper => 2, +// Move::Scissors => 3, +// }; +// let ws: u32 = match win(opp, you) { +// Res::Win => 6, +// Res::Draw => 3, +// Res::Loss => 0, +// }; +// return ms+ws; +// } +fn score(opp: Move, req_res: Res) -> u32 { + let ws: u32 = match req_res { + Res::Win => 6, + Res::Draw => 3, + Res::Loss => 0, + }; + let mm = calc_req_move(opp, req_res); + let ms: u32 = match mm { + Move::Rock => 1, + Move::Paper => 2, + Move::Scissors => 3, + }; + return ms+ws; +} + +// fn parse_turn(line: &str) -> Result<(Move, Move), SimpleError> { +// let mut lc = line.chars(); +// let e1: SimpleError = SimpleError::new("not enough characters in input!"); +// let c1 = lc.next().ok_or(e1.clone())?; +// lc.next().ok_or(e1.clone())?; +// let c2 = lc.next().ok_or(e1.clone())?; +// let m_opp = parse_m1(c1)?; +// let m_you = parse_m2(c2)?; +// Ok((m_opp, m_you)) +// } +fn parse_turn(line: &str) -> Result<(Move, Res), SimpleError> { + let mut lc = line.chars(); + let e1: SimpleError = SimpleError::new("not enough characters in input!"); + let c1 = lc.next().ok_or(e1.clone())?; + lc.next().ok_or(e1.clone())?; + let c2 = lc.next().ok_or(e1.clone())?; + let m_opp = parse_m1(c1)?; + let req_res = parse_req_res(c2)?; + Ok((m_opp, req_res)) +} + +pub fn run(input: String) { + let score: u32 = input.lines().map(|line| { + let (m_opp, m_you) = parse_turn(line).expect("invalid input line!"); + score(m_opp, m_you) + }).sum::<u32>(); + println!("day 2: {}", score); +}
\ No newline at end of file diff --git a/src/day3.rs b/src/day3.rs new file mode 100644 index 0000000..5c01b7a --- /dev/null +++ b/src/day3.rs @@ -0,0 +1,46 @@ +use std::{collections::{HashSet, HashMap}}; + +pub fn run(input: String) { + if !input.is_ascii() { + panic!("input wasn't ascii, character calculations won't work!"); + } + + let scores_table = make_scores_table(); + + let ll: Vec<&str> = input.lines().collect(); + + let score = ll.chunks(3).map(|gm| { + let init: Option<HashSet<char>> = None; + let lb: Vec<char> = gm.into_iter().fold(init, |maybe_acc, m| { + let cs: HashSet<char> = m.chars().collect(); + if let Some(acc) = maybe_acc { + let rr: HashSet<char> = acc.intersection(&cs).map(|x| {x.clone()}).collect(); + Some(rr) + } else { + Some(cs) + } + }) + .expect("didn't get any group members??!") + .into_iter().collect(); + let ct = lb.len(); + if ct != 1 { + panic!("Couldn't find exactly one badge for group! found {}, {:?}", ct, gm); + } + let ch = lb.into_iter().next().expect("foo"); + scores_table.get(&ch).expect(format!("couldn't find score for char {}", ch).as_str()) + }).sum::<u32>(); + println!("Day 3: {}", score); +} + +fn make_scores_table() -> HashMap<char, u32> { + let mut scores_table: HashMap<char,u32> = HashMap::new(); + let r1 = 'a'..='z'; + for (ix, ch) in r1.enumerate() { + scores_table.insert(ch, (ix as u32)+1); + } + let r2 = 'A'..='Z'; + for (ix, ch) in r2.enumerate() { + scores_table.insert(ch, (ix as u32)+27); + } + scores_table +}
\ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..79c5970 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,11 @@ +use std::fs; + +mod day1; +mod day2; +mod day3; + +fn main() { + day1::run(fs::read_to_string("input/day1.txt").expect("Failed to read input file!")); + day2::run(fs::read_to_string("input/day2.txt").expect("Failed to read input file!")); + day3::run(fs::read_to_string("input/day3.txt").expect("Failed to read input file!")); +} |