Compare commits
2 Commits
02fc87d020
...
5759924ba8
| Author | SHA1 | Date | |
|---|---|---|---|
| 5759924ba8 | |||
| 2b67433f49 |
3
README.md
Normal file
3
README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Advent of Rust 2024
|
||||||
|
|
||||||
|
A few of the solutions for [Advent of Code 2024](https://adventofcode.com/2024) in Rust.
|
||||||
50
src/main.rs
50
src/main.rs
@@ -3,23 +3,14 @@ use std::fs::read_to_string;
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = read_to_string("input/day01.txt").unwrap();
|
let input = read_to_string("input/day01.txt").unwrap();
|
||||||
let part1 = part1(&input);
|
println!("part1: {}", part1(&input));
|
||||||
println!("part1: {}", part1);
|
println!("part2: {}", part2(&input));
|
||||||
}
|
}
|
||||||
|
|
||||||
type Int = i32;
|
type Int = i32;
|
||||||
|
|
||||||
fn part1(input: &str) -> Int {
|
fn part1(input: &str) -> Int {
|
||||||
let (mut l, mut r) = input
|
let (mut l, mut r) = make_lists(input);
|
||||||
.trim()
|
|
||||||
.lines()
|
|
||||||
.map(|line| line.split_ascii_whitespace().collect_tuple().unwrap())
|
|
||||||
.map(|(x, y)| (x.parse::<Int>().unwrap(), y.parse::<Int>().unwrap()))
|
|
||||||
.fold((vec![], vec![]), |(mut v1, mut v2), (a, b)| {
|
|
||||||
v1.push(a);
|
|
||||||
v2.push(b);
|
|
||||||
(v1, v2)
|
|
||||||
});
|
|
||||||
|
|
||||||
l.sort();
|
l.sort();
|
||||||
r.sort();
|
r.sort();
|
||||||
@@ -30,8 +21,32 @@ fn part1(input: &str) -> Int {
|
|||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn part2(input: &str) -> Int {
|
||||||
|
let (l, r) = make_lists(input);
|
||||||
|
|
||||||
|
l.iter()
|
||||||
|
.map(|l| r.iter().filter(|r| *r == l).count() as Int * l)
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_lists(input: &str) -> (Vec<Int>, Vec<Int>) {
|
||||||
|
input
|
||||||
|
.trim()
|
||||||
|
.lines()
|
||||||
|
.map(|line| line.split_ascii_whitespace().collect_tuple().unwrap())
|
||||||
|
.map(|(x, y)| (x.parse::<Int>().unwrap(), y.parse::<Int>().unwrap()))
|
||||||
|
.fold((vec![], vec![]), |(mut v1, mut v2), (a, b)| {
|
||||||
|
v1.push(a);
|
||||||
|
v2.push(b);
|
||||||
|
(v1, v2)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
const TEST_INPUT: &str = r"
|
mod test {
|
||||||
|
use crate::{part1, part2};
|
||||||
|
|
||||||
|
const TEST_INPUT: &str = r"
|
||||||
3 4
|
3 4
|
||||||
4 3
|
4 3
|
||||||
2 5
|
2 5
|
||||||
@@ -40,7 +55,12 @@ const TEST_INPUT: &str = r"
|
|||||||
3 3
|
3 3
|
||||||
";
|
";
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test() {
|
fn test1() {
|
||||||
assert_eq!(part1(TEST_INPUT), 11);
|
assert_eq!(part1(TEST_INPUT), 11);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test2() {
|
||||||
|
assert_eq!(part2(TEST_INPUT), 31);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user