From 3e1f2f68ba951ff2984b26b61e45f54907e56d68 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Mon, 2 Dec 2019 17:25:12 +0100 Subject: [PATCH] 2015: day03: finish --- aoc2015/input/day03.txt | 1 + aoc2015/src/day03.rs | 98 +++++++++++++++++++++++++++++++++++++++++ aoc2015/src/lib.rs | 1 + aoc2015/src/main.rs | 3 +- 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 aoc2015/input/day03.txt create mode 100644 aoc2015/src/day03.rs diff --git a/aoc2015/input/day03.txt b/aoc2015/input/day03.txt new file mode 100644 index 0000000..e69cd4f --- /dev/null +++ b/aoc2015/input/day03.txt @@ -0,0 +1 @@ +^^<<>^^>^^^><^>v^>v><><><^^v>v^v>>>^<>v<^<^>>>>><>^>>^>v^>><<^>v>v<>^v^v^vvv><>^^>v><>^><^^^v>>^v^>v><>v^^><vv^<<>v>>><<<>>^^^vv>>>^><<<>><><^>v<>^>v<^v^><<<<>^<>v>^v>vv<^<<>>>>^^v>vv^^<>^<>^^^^<^^^vv<^^v^^>v>^v^^^^>><v<>^v^><>^^><<^^<^^>vv<>v^<^v^>^^>^<>v^^vv<>>v><<<>vvv<>v<>><^<^v<>^vv>^^v<^<>>vv<^>>^>>vv^v>^v^<>^>>>>vv>^^>v>vv>v><^vv^<^<<^^vv^^v>^>>v><^<>v<><>^^<>v>><>^^>^^>>vvv^><<<<<^<^vv<^<>^^^<<<^>^^^vv<>^<>v<^v>^<<>^v<>>v<<^<^<<<><><>^>>>>^>v^v<>vv<^vvv^^^^vv>^v^^v^<^vv<^vv>v<^>vv<>>^>^><^>v>^v>vvv<>^>^v<><>vv>><^v^<><>>v^v^><^<^>vv>v<^>vvv>v<<<<<^>^vv>^><><>^<v^>^><><>>^>^>><^^^>^^>^^v^^<^v^^>v^^>>><<><><>^^<<^^v^>v>><>^^^><^vvv<^^^^^v><<><><>>^>vv>>^vv^^><v<^^>^<^^<^>>>^v<>v<^^^>vvv^v<<^><>>>>v>>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v^^v^<^^^v^vvv^>><>>v<>^<^vvv<<^^>vv^^<<>>><^^vvv<<<^>^<><^>vv^><^<<>vv<>vv>v>v^<<>^^^^v^^^^^<<^><><^^v^>v>^>><^><<>v^>>^vvv>>^<^<>^^v^vv^^v><>>v<<<>v>^<>v<<>v^>^<<><<>v^>v<><^^>^<^v^^><^>vv>^>vvvv>^^><<>vv^>^v<<^<<^<<>vv>>^>>>>>v^v<^v>v>^^^vv^v<^<>v><>>vv>v><>v>^v<><<<<<>v^vv<<<<^<>^>><>^^vv>^<^<<>vv>>vv><>><^^><^<>^><>v^^^^v^^vv<>v<>v>^vv^>^<>^^^>v^>>>v><<^>>v<^v<>^^v<><<>v<^<^>v<>v^>v>^^<<<^^vv^<><<<>>v>^^<>v>>>><<>v^v<>v>><<<<^<<^>^>v^vv^><^v^^<>^^><>vv>^>vvv<^v^>>^>^>^^<<^>^>^v><>>^<^^v>^>>^^<><>>>^^>^^vvv>v<^^<>v^v^^v^v>><<^^^>>v>^vv>^>^^v<>^^<>v^^<>v^><<>vv<<^vvvv><<v>v^>v^<>v^>^^<>^>^^v<>><<<>^v^^v^v<<<^v^<>^<>v>^^>vv>^^<<<><<^>v<^^<^<<>^>>>>>^v^v<vvv<<>v>v>>^v^v^>><<<<>v^<<>>>^>>^>>< diff --git a/aoc2015/src/day03.rs b/aoc2015/src/day03.rs new file mode 100644 index 0000000..4eb4bf5 --- /dev/null +++ b/aoc2015/src/day03.rs @@ -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 { + 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 { + 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); + } +} diff --git a/aoc2015/src/lib.rs b/aoc2015/src/lib.rs index 28326d5..89345b1 100644 --- a/aoc2015/src/lib.rs +++ b/aoc2015/src/lib.rs @@ -1,2 +1,3 @@ pub mod day01; pub mod day02; +pub mod day03; diff --git a/aoc2015/src/main.rs b/aoc2015/src/main.rs index 7e6db46..68fad4b 100644 --- a/aoc2015/src/main.rs +++ b/aoc2015/src/main.rs @@ -2,9 +2,10 @@ use aoc::Result; use aoc2015::day01; use aoc2015::day02; +use aoc2015::day03; fn main() -> Result<()> { - let days: &[fn() -> Result<()>] = &[day01::run, day02::run]; + let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run]; aoc::run(days) }