Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub struct Day2 {
- input: Vec<(String, i64)>,
- }
- impl Day2 {
- pub async fn new() -> Result<Self, Box<dyn Error>> {
- let content = aoc_input::create_input(2021, 2).await?;
- Ok(Day2 {
- input: content
- .lines()
- .map(|x| x.split_whitespace().collect::<Vec<&str>>())
- .map(|x| (x[0].to_string(), x[1].parse::<i64>().unwrap()))
- .collect(),
- })
- }
- }
- impl Day for Day2 {
- fn part1(&self) -> i64 {
- let mut pos: (i64, i64) = (0, 0);
- for (dir, len) in self.input.iter() {
- match &dir[..] {
- "forward" => pos.0 += len,
- "up" => pos.1 -= len,
- "down" => pos.1 += len,
- _ => continue,
- }
- }
- pos.0 * pos.1
- }
- fn part2(&self) -> i64 {
- let mut aim: i64 = 0;
- let mut pos: (i64, i64) = (0, 0);
- for (dir, len) in self.input.iter() {
- match &dir[..] {
- "forward" => {
- pos.0 += len;
- pos.1 += len * aim;
- },
- "up" => aim -= len,
- "down" => aim += len,
- _ => continue,
- }
- }
- pos.0 * pos.1
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement