2015: day03: finish

This commit is contained in:
Antoine Martin 2019-12-02 17:25:12 +01:00
parent ca1952819f
commit 3e1f2f68ba
4 changed files with 102 additions and 1 deletions

1
aoc2015/input/day03.txt Normal file

File diff suppressed because one or more lines are too long

98
aoc2015/src/day03.rs Normal file
View file

@ -0,0 +1,98 @@
use std::collections::HashSet;
use aoc::err;
use aoc::Result;
const INPUT: &str = include_str!("../input/day03.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
Ok(())
}
fn part1(input: &str) -> Result<usize> {
let mut houses = HashSet::new();
let mut x = 0;
let mut y = 0;
houses.insert((x, y));
for c in input.trim_end().chars() {
match c {
'>' => x += 1,
'<' => x -= 1,
'^' => y -= 1,
'v' => y += 1,
_ => return Err(err!("unidentified move: `{}`", c)),
}
houses.insert((x, y));
}
Ok(houses.len())
}
fn part2(input: &str) -> Result<usize> {
let mut houses = HashSet::new();
let mut santa_x = 0;
let mut santa_y = 0;
let mut robot_x = 0;
let mut robot_y = 0;
houses.insert((0, 0));
for (i, c) in input.trim_end().chars().enumerate() {
if i % 2 == 0 {
match c {
'>' => santa_x += 1,
'<' => santa_x -= 1,
'^' => santa_y -= 1,
'v' => santa_y += 1,
_ => return Err(err!("unidentified move: `{}`", c)),
}
houses.insert((santa_x, santa_y));
} else {
match c {
'>' => robot_x += 1,
'<' => robot_x -= 1,
'^' => robot_y -= 1,
'v' => robot_y += 1,
_ => return Err(err!("unidentified move: `{}`", c)),
}
houses.insert((robot_x, robot_y));
}
}
Ok(houses.len())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part1_provided() {
assert_eq!(part1(">").unwrap(), 2);
assert_eq!(part1("^>v<").unwrap(), 4);
assert_eq!(part1("^v^v^v^v^v").unwrap(), 2);
}
#[test]
fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 2565);
}
#[test]
fn part2_provided() {
assert_eq!(part2("^v").unwrap(), 3);
assert_eq!(part2("^>v<").unwrap(), 3);
assert_eq!(part2("^v^v^v^v^v").unwrap(), 11);
}
}

View file

@ -1,2 +1,3 @@
pub mod day01; pub mod day01;
pub mod day02; pub mod day02;
pub mod day03;

View file

@ -2,9 +2,10 @@ use aoc::Result;
use aoc2015::day01; use aoc2015::day01;
use aoc2015::day02; use aoc2015::day02;
use aoc2015::day03;
fn main() -> Result<()> { fn main() -> Result<()> {
let days: &[fn() -> Result<()>] = &[day01::run, day02::run]; let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run];
aoc::run(days) aoc::run(days)
} }