Solutions for 2022, as well as 2015-2018 and 2019 up to day 11

This commit is contained in:
Chris Alge 2023-03-12 15:20:02 +01:00
commit 1895197c49
722 changed files with 375457 additions and 0 deletions

View file

@ -0,0 +1,8 @@
[package]
name = "day03-squares_with_three_sides"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,41 @@
\--- Day 3: Squares With Three Sides ---
----------
Now that you can think clearly, you move deeper into the labyrinth of hallways and office furniture that makes up this part of Easter Bunny HQ. This must be a graphic design department; the walls are covered in specifications for triangles.
Or are they?
The design document gives the side lengths of each triangle it describes, but... `5 10 25`? Some of these aren't triangles. You can't help but mark the impossible ones.
In a valid triangle, the sum of any two sides must be larger than the remaining side. For example, the "triangle" given above is impossible, because `5 + 10` is not larger than `25`.
In your puzzle input, *how many* of the listed triangles are *possible*?
Your puzzle answer was `1050`.
\--- Part Two ---
----------
Now that you've helpfully marked up their design documents, it occurs to you that triangles are specified in groups of three *vertically*. Each set of three numbers in a column specifies a triangle. Rows are unrelated.
For example, given the following specification, numbers with the same hundreds digit would be part of the same triangle:
```
101 301 501
102 302 502
103 303 503
201 401 601
202 402 602
203 403 603
```
In your puzzle input, and instead reading by columns, *how many* of the listed triangles are *possible*?
Your puzzle answer was `1921`.
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](/2016).
If you still want to see it, you can [get your puzzle input](3/input).

View file

@ -0,0 +1,36 @@
pub fn run(input: &str) -> (usize, usize) {
let first = input.lines().filter(|t| check_trinangle(t)).count();
let collected_lines: Vec<_> = input.lines().map(|l| l.split_whitespace().collect::<Vec<_>>()).collect();
let second = collected_lines.chunks(3).map(|lines| {
(0..3).filter(|i| {
let (a, b, c): (usize, usize, usize) = ( lines[0][*i].parse().unwrap(), lines[1][*i].parse().unwrap(), lines[2][*i].parse().unwrap() );
let max = a.max(b).max(c);
a+b>max && b+c>max && a+c>max
}).count()
}).sum();
(first, second)
}
fn check_trinangle(line: &str) -> bool {
let components: Vec<_> = line.split_whitespace().collect();
assert_eq!(components.len(), 3);
let (a, b, c): (usize, usize, usize) = ( components[0].parse().unwrap(), components[1].parse().unwrap(), components[2].parse().unwrap() );
let max = a.max(b).max(c);
a+b>max && b+c>max && a+c>max
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::read_to_string;
fn read_file(name: &str) -> String {
read_to_string(name).expect(&format!("Unable to read file: {}", name)[..])
}
#[test]
fn test_challenge() {
let challenge_input = read_file("tests/challenge_input");
assert_eq!(run(&challenge_input), (1050, 1921));
}
}

File diff suppressed because it is too large Load diff