2018: day05: part 1
This commit is contained in:
parent
a6ff14b260
commit
d17e26540f
1
aoc2018/input/day05.txt
Normal file
1
aoc2018/input/day05.txt
Normal file
File diff suppressed because one or more lines are too long
63
aoc2018/src/day05.rs
Normal file
63
aoc2018/src/day05.rs
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
use super::Result;
|
||||||
|
|
||||||
|
const INPUT: &str = include_str!("../input/day05.txt");
|
||||||
|
|
||||||
|
pub fn run() -> Result<()> {
|
||||||
|
println!("part 1: {}", part1(INPUT)?);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn same_type(a: char, b: char) -> bool {
|
||||||
|
a.to_ascii_lowercase() == b.to_ascii_lowercase()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(input: &str) -> Result<usize> {
|
||||||
|
let mut res = String::with_capacity(input.len());
|
||||||
|
|
||||||
|
// tracks last character of string
|
||||||
|
let mut last: Option<char> = None;
|
||||||
|
|
||||||
|
for next in input.chars() {
|
||||||
|
match last {
|
||||||
|
Some(elem) => {
|
||||||
|
// if same type but different polarity
|
||||||
|
if same_type(elem, next) && elem != next {
|
||||||
|
// drop both elem and next
|
||||||
|
last = res.pop();
|
||||||
|
} else {
|
||||||
|
// consider elem "safe" to push
|
||||||
|
res.push(elem);
|
||||||
|
last = Some(next);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
last = Some(next);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// add last character to string if exists
|
||||||
|
if let Some(c) = last {
|
||||||
|
res.push(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(res.len())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const PROVIDED: &str = "dabAcCaCBAcCcaDA";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_provided() {
|
||||||
|
assert_eq!(part1(PROVIDED).unwrap(), 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_real() {
|
||||||
|
assert_eq!(part1(INPUT).unwrap(), 10638);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
pub mod day01;
|
pub mod day01;
|
||||||
pub mod day02;
|
pub mod day02;
|
||||||
pub mod day03;
|
pub mod day03;
|
||||||
|
pub mod day05;
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||||
|
|
||||||
|
|
|
@ -3,11 +3,12 @@ use std::env;
|
||||||
use aoc2018::day01;
|
use aoc2018::day01;
|
||||||
use aoc2018::day02;
|
use aoc2018::day02;
|
||||||
use aoc2018::day03;
|
use aoc2018::day03;
|
||||||
|
use aoc2018::day05;
|
||||||
|
|
||||||
use aoc2018::Result;
|
use aoc2018::Result;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run];
|
let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run, day05::run];
|
||||||
|
|
||||||
let mut args = env::args();
|
let mut args = env::args();
|
||||||
args.next();
|
args.next();
|
||||||
|
|
Loading…
Reference in a new issue