Malerwerkst.at

Rust - Advent of Code 2015 - Tag 3 / Teil 2

Introduction

R.


Rust AoC-2015

Rust - Advent of Code 2015 - Tag 3 / Teil 2

Posted by R. on .
Featured

Rust AoC-2015

Rust - Advent of Code 2015 - Tag 3 / Teil 2

Posted by R. on .

Die Advent of Code Aufgaben bestehen ja immer aus zwei Teilen, wobei Teil zwei erst erscheint, wenn Teil 1 erfolgreich gelöst wurde. In unregelmäßigen Abständen werde ich auch die zweiten Teile versuchen zu lösen und die Lösungen hier präsentieren.

Aufgabenstellung

The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him.

Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year.

This year, how many houses receive at least one present?

For example:

^v delivers presents to 3 houses, because Santa goes north, and then Robo-Santa goes south.

^>v< now delivers presents to 3 houses, and Santa and Robo-Santa end up back where they started.

^v^v^v^v^v now delivers presents to 11 houses, with Santa going one direction and Robo-Santa going the other.

Code

use std::fs::File;  
use std::io::Read;  
use std::collections::HashSet;


fn get_data(fname: &str) -> String {  
    let mut file = match File::open(fname) {
        Err(e) => panic!("file error: {}",e),
        Ok(file) => file,
    };

    let mut data = String::new();

    match file.read_to_string(&mut data) {
        Err(e) => panic!("read error: {}", e),
        Ok(_) => {},
    }
    data
}

fn part1(data: &str) -> usize {  
    let mut houses = HashSet::new();
    let mut pos=(0,0);

    for ch in data.chars() {
        match ch {
            '^' => pos.1+=1,
            '>' => pos.0+=1,
            'v' => pos.1-=1,
            '<' => pos.0-=1,
            _ => {},
        }
        houses.insert(pos);
    }
    houses.len()
}

fn part2(data: &str) -> usize {  
    let mut houses = HashSet::new();
    let mut pos_santa=(0,0);
    let mut pos_robo=(0,0);

    for (i,ch) in data.chars().enumerate() {
        if i % 2 == 0 {
            match ch {
                '^' => pos_santa.1+=1,
                '>' => pos_santa.0+=1,
                'v' => pos_santa.1-=1,
                '<' => pos_santa.0-=1,
                _ => {},
            }
            houses.insert(pos_santa);
        } else {
            match ch {
                '^' => pos_robo.1+=1,
                '>' => pos_robo.0+=1,
                'v' => pos_robo.1-=1,
                '<' => pos_robo.0-=1,
                _ => {},
            }
            houses.insert(pos_robo);
        }    
    }
    houses.len()
}

fn main() {  
    let input_str=get_data("input.txt");

      println!("Part 1: {}",part1(&input_str));
      println!("Part 2: {}",part2(&input_str));
}

Bin mit dem zweiten Teil irgendwie nicht ganz zufrieden und denke, dass man es "funktionaler" gestalten könnte. Fragt sich nur wie?

R.

View Comments...