Skip to content

Commit

Permalink
aoc day3
Browse files Browse the repository at this point in the history
  • Loading branch information
AAwouters committed Dec 12, 2022
1 parent 73baf1c commit fd66155
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 1 deletion.
89 changes: 89 additions & 0 deletions src/days/day3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use aoc_runner_derive::*;

type Input = String;
type Output = u32;

fn find_common(contents: &str) -> char {
let split = contents.split_at(contents.len() / 2);
for char in split.0.chars() {
if split.1.contains(char) {
return char;
}
}
unreachable!()
}

fn find_tripe_common(line1: &String, line2: &String, line3: &String) -> char {
for char in line1.chars() {
if line2.contains(char) && line3.contains(char) {
return char;
}
}
unreachable!()
}

fn priority(item: &char) -> u32 {
if item.is_lowercase() {
return (*item as u32) - 96;
} else if item.is_uppercase() {
return (*item as u32) - 38;
}
unreachable!()
}

#[aoc_generator(day3)]
pub fn generator(input: &str) -> Vec<Input> {
input.lines().map(|line| line.to_owned()).collect()
}

#[aoc(day3, part1)]
pub fn part1(input: &[Input]) -> Output {
input
.iter()
.map(|line| {
let common = find_common(line);
priority(&common)
})
.sum()
}

#[aoc(day3, part2)]
pub fn part2(input: &[Input]) -> Output {
let mut iterator = input.iter().peekable();
let mut sum = 0;

while iterator.peek().is_some() {
let line1 = iterator.next().unwrap();
let line2 = iterator.next().unwrap();
let line3 = iterator.next().unwrap();

let char = find_tripe_common(line1, line2, line3);
sum += priority(&char);
}

sum
}

#[cfg(test)]
mod tests {
use super::*;

static TEST_INPUT: &'static str = "vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw";

#[test]
fn test_part1() {
let input = generator(&TEST_INPUT);
assert_eq!(part1(&input), 157);
}

#[test]
fn test_part2() {
let input = generator(&TEST_INPUT);
assert_eq!(part2(&input), 70);
}
}
51 changes: 51 additions & 0 deletions src/days/dayTemplate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use aoc_runner_derive::*;

type Input = u32;
type Output = u32;

#[aoc_generator(dayx)]
pub fn generator(input: &str) -> Vec<Input> {
todo!()
}

#[aoc(dayx, part1)]
pub fn part1(input: &[Input]) -> Output {
todo!()
}

#[aoc(dayx, part2)]
pub fn part2(input: &[Input]) -> Output {
todo!()
}

#[cfg(test)]
mod tests {
use super::*;

static TEST_INPUT: &'static str = "1000
2000
3000
4000
5000
6000
7000
8000
9000
10000";

#[test]
fn test_part1() {
let input = generator(&TEST_INPUT);
assert_eq!(part1(&input), 24000);
}

#[test]
fn test_part2() {
let input = generator(&TEST_INPUT);
assert_eq!(part2(&input), 45000);
}
}
3 changes: 2 additions & 1 deletion src/days/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod day1;
mod day2;
mod day2;
mod day3;

0 comments on commit fd66155

Please sign in to comment.