diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d913617 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target + diff --git a/README.md b/README.md index 57d79d6..68fb244 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ These are my attempts on [AoC 2022](https://adventofcode.com/2022/) |--------------|--------|--------|----------| | [01](day01) | ⭐ | ⭐ | Python | | [02](day02) | ⭐ | ⭐ | C | -| 03 | | | | +| [03](day03) | ⭐ | ⭐ | Rust | | 04 | | | | | 05 | | | | | 06 | | | | diff --git a/day03/Cargo.lock b/day03/Cargo.lock new file mode 100644 index 0000000..21c74cb --- /dev/null +++ b/day03/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day03" +version = "0.1.0" diff --git a/day03/Cargo.toml b/day03/Cargo.toml new file mode 100644 index 0000000..6c3913d --- /dev/null +++ b/day03/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "day03" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[[bin]] +name = "day03" +path = "main.rs" diff --git a/day03/input.txt b/day03/input.txt new file mode 100644 index 0000000..adf173d --- /dev/null +++ b/day03/input.txt @@ -0,0 +1,300 @@ +RCMRQjLLWGTjnlnZwwnZJRZH +qnvfhpSbvSppNddNdSqbbmmdPrwttJVrVPDVrJtHtwPZhrPJ +BFpFzSSqSFFSvQsnWgCMjTLzng +DbWVcVRRdlLffvtqjTWNgQ +mJJMpsmrMrJSHJpsHrFHvBvgHvqfNvzffgTvfj +mMhPjmjmFPJhMSGGcDRlwRdcLGPc +qFcbmWFJqqWpRJcQWpqsQQQwSPCPrHRHCPdNZtSrSHwrNZ +jGMjGLhhhgTvghgtGVjnNCrPVwZSZffSNSwHZZdH +DvzDlvvhnjlMlglglGGhDLpqqcJWWtsmszpWbBBBmQmb +SPLPHQbJSbPsvTLmfDvVDctv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new file mode 100644 index 0000000..f17e726 --- /dev/null +++ b/day03/input_demo.txt @@ -0,0 +1,6 @@ +vJrwpWtwJgWrhcsFMMfFFhFp +jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL +PmmdzqPrVvPwwTWBwg +wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn +ttgJtRGJQctTZtZT +CrZsJsPPZsGzwwsLwLmpwMDw diff --git a/day03/main.rs b/day03/main.rs new file mode 100644 index 0000000..ec23c69 --- /dev/null +++ b/day03/main.rs @@ -0,0 +1,100 @@ +use std::env; +use std::fs::File; +use std::io::{self, prelude::*, BufReader}; + +fn main() -> io::Result<()> { + // Usage: cargo run -- + let args: Vec = env::args().collect(); + if args.len() != 2 { + println!("Usage: {} ", args[0]); + return Ok(()); + } + + let file = File::open(&args[1])?; + let reader = BufReader::new(file); + + // Could use lines().collect() directly for a Vec> + let lines: Vec = reader.lines().map(|l| l.expect("Could not parse line")).collect(); + + part1(&lines).unwrap(); + part2(&lines).unwrap(); + + Ok(()) +} + +fn part1(lines: &Vec) -> io::Result<()> { + // All misplaced characters, one char per line of input + let mut misplaced = "".to_string(); + + for line in lines { + let (left, right) = line.split_at(line.len() / 2); + + // Find the character that occurs in both halves + for c in left.chars() { + if right.contains(c) { + // The character exists in both halves. + misplaced.push(c); + break; + } + } + } + + println!("== Part 1 =="); + println!("Misplaced characters: {}", misplaced); + + let score = find_score(misplaced); + + println!("Score: {}\n", score); + + Ok(()) +} + +fn part2(lines: &Vec) -> io::Result<()> { + let mut badges = "".to_string(); + + let mut chunk = Vec::new(); + for line in lines { + let line = line; + chunk.push(line); + + // After reading 3 lines, find the recurring character and clear the chunk + if chunk.len() == 3 { + for c in chunk[0].chars() { + if chunk[1].contains(c) && chunk[2].contains(c) { + // The character exists in all 3 lines. + badges.push(c); + break; + } + } + + chunk = Vec::new(); + } + } + + println!("== Part 2 =="); + println!("Badges: {}", badges); + + let score = find_score(badges); + + println!("Score: {}\n", score); + + Ok(()) +} + +fn find_score(badges: String) -> u32 { + // Returns the sum of points. + // Lower case letters are mapped a-z -> 1-26 + // Upper case letters are mapped A-Z -> 27-52 + + let mut score = 0; + + for c in badges.chars() { + if c.is_uppercase() { + score += (c as u8 - b'A') as u32 + 27; + } else { + score += (c as u8 - b'a') as u32 + 1; + } + } + + score +}