Solutions for 2022, as well as 2015-2018 and 2019 up to day 11
This commit is contained in:
commit
1895197c49
722 changed files with 375457 additions and 0 deletions
8
2015/day01-not_quite_lisp/Cargo.toml
Normal file
8
2015/day01-not_quite_lisp/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day01-not_quite_lisp"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
46
2015/day01-not_quite_lisp/challenge.txt
Normal file
46
2015/day01-not_quite_lisp/challenge.txt
Normal file
|
@ -0,0 +1,46 @@
|
|||
\--- Day 1: Not Quite Lisp ---
|
||||
----------
|
||||
|
||||
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.
|
||||
|
||||
For example:
|
||||
|
||||
* `(())` and `()()` both result in floor `0`.
|
||||
* `(((` and `(()(()(` both result in floor `3`.
|
||||
* `))(((((` also results in floor `3`.
|
||||
* `())` and `))(` both result in floor `-1` (the first basement level).
|
||||
* `)))` and `)())())` both result in floor `-3`.
|
||||
|
||||
To *what floor* do the instructions take Santa?
|
||||
|
||||
Your puzzle answer was `232`.
|
||||
|
||||
\--- Part Two ---
|
||||
----------
|
||||
|
||||
Now, given the same instructions, find the *position* of the first character that causes him to enter the basement (floor `-1`). The first character in the instructions has position `1`, the second character has position `2`, and so on.
|
||||
|
||||
For example:
|
||||
|
||||
* `)` causes him to enter the basement at character position `1`.
|
||||
* `()())` causes him to enter the basement at character position `5`.
|
||||
|
||||
What is the *position* of the character that causes Santa to first enter the basement?
|
||||
|
||||
Your puzzle answer was `1783`.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: \*\*
|
||||
|
||||
At this point, all that is left is for you to [admire your Advent calendar](/2015).
|
||||
|
||||
If you still want to see it, you can [get your puzzle input](1/input).
|
48
2015/day01-not_quite_lisp/src/lib.rs
Normal file
48
2015/day01-not_quite_lisp/src/lib.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
use std::fs::read_to_string;
|
||||
|
||||
fn read_file(name: &str) -> String {
|
||||
read_to_string(name).expect(&format!("Unable to read file: {}", name)[..])
|
||||
}
|
||||
|
||||
fn char_to_floor(c: char) -> isize {
|
||||
match c {
|
||||
'(' => 1,
|
||||
')' => -1,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn final_floor(input: &str) -> isize {
|
||||
input.chars().map(char_to_floor).sum()
|
||||
}
|
||||
|
||||
pub fn first_basement_pos(input: &str) -> usize {
|
||||
let mut floors = input.chars().scan(0, |curr_floor, c| { *curr_floor += char_to_floor(c);Some(*curr_floor) } );
|
||||
floors.position(|i| i==-1).expect("Never reached Floor -1") + 1
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = read_file("tests/sample_input");
|
||||
let expected = [0, 0, 3, 3, 3, -1, -1, -3, -3];
|
||||
for (line_number, line) in sample_input.lines().enumerate() {
|
||||
assert_eq!(final_floor(line), expected[line_number]);
|
||||
}
|
||||
let samples_2 = [")", "()())"];
|
||||
let expected_2 = [1, 5];
|
||||
for (sample_number, sample) in samples_2.iter().enumerate() {
|
||||
assert_eq!(first_basement_pos(sample), expected_2[sample_number]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = read_file("tests/challenge_input");
|
||||
assert_eq!(final_floor(&challenge_input), 232);
|
||||
assert_eq!(first_basement_pos(&challenge_input), 1783);
|
||||
}
|
||||
}
|
1
2015/day01-not_quite_lisp/tests/challenge_input
Normal file
1
2015/day01-not_quite_lisp/tests/challenge_input
Normal file
File diff suppressed because one or more lines are too long
9
2015/day01-not_quite_lisp/tests/sample_input
Normal file
9
2015/day01-not_quite_lisp/tests/sample_input
Normal file
|
@ -0,0 +1,9 @@
|
|||
(())
|
||||
()()
|
||||
(((
|
||||
(()(()(
|
||||
))(((((
|
||||
())
|
||||
))(
|
||||
)))
|
||||
)())())
|
Loading…
Add table
Add a link
Reference in a new issue