Malerwerkst.at

Rust - Advent of Code 2015 - Tag 1

Introduction

R.


Rust AoC-2015

Rust - Advent of Code 2015 - Tag 1

Posted by R. on .
Featured

Rust AoC-2015

Rust - Advent of Code 2015 - Tag 1

Posted by R. on .

So, bevor das diesjährige Advent of Code im Dezember startet, will ich versuchen, noch ein paar Tage des letzten Jahres zu lösen. Und zwar diesmal nicht ein einer Scriptsprache wie Ruby oder Python (welche ich beide außerordentlich mag!), sondern in Rust. Hiermit lade ich jeden ein, meine "Lösungen" zu kommentieren, zu kritisieren und sie zu verbessern - denn dafür dürfte erhebliches Potential vorhanden sein... :-)

Aber genug geschwafelt, auf geht's!

Aufgabenstellung:

Santa was hoping for a white Christmas, but his weather machine's "snow" function is powered by stars, and he's fresh out! To save Christmas, he needs you to collect fifty stars by December 25th.

Collect stars by helping Santa solve puzzles. Two puzzles will be made available on each day in the advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

Here's an easy puzzle to warm you up.

Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time.

An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor.

The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors.

Code:

fn main() {  
  let input_str="((((()(()...";

  let mut floor=0;

  for ch in input_str.chars() {
    floor += match ch {
        '(' => 1,
        ')' => -1,
        _ => 0
    }
  }

  println!("{}",floor);
}

R.

View Comments...