Compare commits
3 Commits
1910caab51
...
048c66102b
| Author | SHA1 | Date | |
|---|---|---|---|
| 048c66102b | |||
| c414be2200 | |||
| 2a60712f44 |
9
advent_of_code/2015/9/Cargo.toml
Normal file
9
advent_of_code/2015/9/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
nom = "7.1.3"
|
||||||
28
advent_of_code/2015/9/input.txt
Normal file
28
advent_of_code/2015/9/input.txt
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
Faerun to Norrath = 129
|
||||||
|
Faerun to Tristram = 58
|
||||||
|
Faerun to AlphaCentauri = 13
|
||||||
|
Faerun to Arbre = 24
|
||||||
|
Faerun to Snowdin = 60
|
||||||
|
Faerun to Tambi = 71
|
||||||
|
Faerun to Straylight = 67
|
||||||
|
Norrath to Tristram = 142
|
||||||
|
Norrath to AlphaCentauri = 15
|
||||||
|
Norrath to Arbre = 135
|
||||||
|
Norrath to Snowdin = 75
|
||||||
|
Norrath to Tambi = 82
|
||||||
|
Norrath to Straylight = 54
|
||||||
|
Tristram to AlphaCentauri = 118
|
||||||
|
Tristram to Arbre = 122
|
||||||
|
Tristram to Snowdin = 103
|
||||||
|
Tristram to Tambi = 49
|
||||||
|
Tristram to Straylight = 97
|
||||||
|
AlphaCentauri to Arbre = 116
|
||||||
|
AlphaCentauri to Snowdin = 12
|
||||||
|
AlphaCentauri to Tambi = 18
|
||||||
|
AlphaCentauri to Straylight = 91
|
||||||
|
Arbre to Snowdin = 129
|
||||||
|
Arbre to Tambi = 53
|
||||||
|
Arbre to Straylight = 40
|
||||||
|
Snowdin to Tambi = 15
|
||||||
|
Snowdin to Straylight = 99
|
||||||
|
Tambi to Straylight = 70
|
||||||
33
advent_of_code/2015/9/src/main.rs
Normal file
33
advent_of_code/2015/9/src/main.rs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
use nom::{
|
||||||
|
bytes::complete::tag,
|
||||||
|
character::complete::{alpha1, i64, multispace1},
|
||||||
|
multi::separated_list1,
|
||||||
|
sequence::tuple,
|
||||||
|
IResult,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn parse_input(input: &str) -> IResult<&str, Vec<(String, String, i64)>> {
|
||||||
|
let (input, result) = separated_list1(
|
||||||
|
multispace1,
|
||||||
|
tuple((alpha1, tag(" to "), alpha1, tag(" = "), i64))
|
||||||
|
.map(|(from, _, to, _, distance)| (from.to_string(), to.to_string(), distance)),
|
||||||
|
)(input)?;
|
||||||
|
|
||||||
|
Ok((input, result))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_first(distances: &[(String, String, i64)]) -> i64 {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, Patrick!");
|
||||||
|
|
||||||
|
let input_txt = include_str!("../input.txt");
|
||||||
|
|
||||||
|
let (_, distances) = parse_input(input_txt).unwrap();
|
||||||
|
|
||||||
|
let first_result = solve_first(&distances[..]);
|
||||||
|
|
||||||
|
println!("The shortest route is {}", first_result);
|
||||||
|
}
|
||||||
8
advent_of_code/2023/1/Cargo.toml
Normal file
8
advent_of_code/2023/1/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
1000
advent_of_code/2023/1/input.txt
Normal file
1000
advent_of_code/2023/1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
160
advent_of_code/2023/1/src/main.rs
Normal file
160
advent_of_code/2023/1/src/main.rs
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
fn solve_1(input: &str) -> u32 {
|
||||||
|
input
|
||||||
|
.lines()
|
||||||
|
.map(|l| {
|
||||||
|
// Digits are 0-9, so we know that if they
|
||||||
|
// are still 10 they haven't been changed
|
||||||
|
let mut first = 10;
|
||||||
|
let mut last = 10;
|
||||||
|
|
||||||
|
for c in l.chars() {
|
||||||
|
if let Some(digit) = c.to_digit(10) {
|
||||||
|
if first == 10 {
|
||||||
|
first = digit;
|
||||||
|
}
|
||||||
|
last = digit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
first * 10 + last
|
||||||
|
})
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn digitify(chars: &str) -> u32 {
|
||||||
|
let mut chars_left = &chars[..];
|
||||||
|
|
||||||
|
while chars_left.len() > 0 {
|
||||||
|
if chars_left.len() >= 3 {
|
||||||
|
if let Some(digit) = chars_left.chars().next().unwrap().to_digit(10) {
|
||||||
|
return digit;
|
||||||
|
} else if &chars_left[0..3] == "one" {
|
||||||
|
return 1;
|
||||||
|
} else if &chars_left[0..3] == "two" {
|
||||||
|
return 2;
|
||||||
|
} else if &chars_left[0..3] == "six" {
|
||||||
|
return 6;
|
||||||
|
} else if chars_left.len() >= 4 {
|
||||||
|
if &chars_left[0..4] == "four" {
|
||||||
|
return 4;
|
||||||
|
} else if &chars_left[0..4] == "five" {
|
||||||
|
return 5;
|
||||||
|
} else if &chars_left[0..4] == "nine" {
|
||||||
|
return 9;
|
||||||
|
} else if chars_left.len() >= 5 {
|
||||||
|
if &chars_left[0..5] == "three" {
|
||||||
|
return 3;
|
||||||
|
} else if &chars_left[0..5] == "seven" {
|
||||||
|
return 7;
|
||||||
|
} else if &chars_left[0..5] == "eight" {
|
||||||
|
return 8;
|
||||||
|
} else {
|
||||||
|
chars_left = &chars_left[1..];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
chars_left = &chars_left[1..];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
chars_left = &chars_left[1..];
|
||||||
|
}
|
||||||
|
} else if let Some(digit) = chars_left.chars().next().unwrap().to_digit(10) {
|
||||||
|
return digit;
|
||||||
|
} else {
|
||||||
|
chars_left = &chars_left[1..];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn digitify_backwards(line: &str) -> u32 {
|
||||||
|
let chars_left = line.chars().rev().collect::<String>();
|
||||||
|
let mut chars_left = chars_left.as_str();
|
||||||
|
|
||||||
|
while chars_left.len() > 0 {
|
||||||
|
if chars_left.len() >= 3 {
|
||||||
|
if let Some(digit) = chars_left.chars().next().unwrap().to_digit(10) {
|
||||||
|
return digit;
|
||||||
|
} else if &chars_left[0..3] == "eno" {
|
||||||
|
return 1;
|
||||||
|
} else if &chars_left[0..3] == "owt" {
|
||||||
|
return 2;
|
||||||
|
} else if &chars_left[0..3] == "xis" {
|
||||||
|
return 6;
|
||||||
|
} else if chars_left.len() >= 4 {
|
||||||
|
if &chars_left[0..4] == "ruof" {
|
||||||
|
return 4;
|
||||||
|
} else if &chars_left[0..4] == "evif" {
|
||||||
|
return 5;
|
||||||
|
} else if &chars_left[0..4] == "enin" {
|
||||||
|
return 9;
|
||||||
|
} else if chars_left.len() >= 5 {
|
||||||
|
if &chars_left[0..5] == "eerht" {
|
||||||
|
return 3;
|
||||||
|
} else if &chars_left[0..5] == "neves" {
|
||||||
|
return 7;
|
||||||
|
} else if &chars_left[0..5] == "thgie" {
|
||||||
|
return 8;
|
||||||
|
} else {
|
||||||
|
chars_left = &chars_left[1..];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
chars_left = &chars_left[1..];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
chars_left = &chars_left[1..];
|
||||||
|
}
|
||||||
|
} else if let Some(digit) = chars_left.chars().next().unwrap().to_digit(10) {
|
||||||
|
return digit;
|
||||||
|
} else {
|
||||||
|
chars_left = &chars_left[1..];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_2(input: &str) -> u32 {
|
||||||
|
input
|
||||||
|
.lines()
|
||||||
|
.map(|l| {
|
||||||
|
let first = digitify(&l[..]);
|
||||||
|
let last = digitify_backwards(&l[..]);
|
||||||
|
|
||||||
|
first * 10 + last
|
||||||
|
})
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, this is Patrick!");
|
||||||
|
|
||||||
|
let input_txt = include_str!("../input.txt");
|
||||||
|
|
||||||
|
let result_1 = solve_1(input_txt);
|
||||||
|
|
||||||
|
println!("The sum of all calibration values is {}", result_1);
|
||||||
|
|
||||||
|
let result_2 = solve_2(input_txt);
|
||||||
|
|
||||||
|
println!("The sum of all improved calibration values is {}", result_2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_1() {
|
||||||
|
let test_input = include_str!("../test.txt");
|
||||||
|
|
||||||
|
assert_eq!(solve_1(test_input), 142);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_2() {
|
||||||
|
let test_input = include_str!("../test2.txt");
|
||||||
|
|
||||||
|
assert_eq!(solve_2(test_input), 281);
|
||||||
|
}
|
||||||
|
}
|
||||||
4
advent_of_code/2023/1/test.txt
Normal file
4
advent_of_code/2023/1/test.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
1abc2
|
||||||
|
pqr3stu8vwx
|
||||||
|
a1b2c3d4e5f
|
||||||
|
treb7uchet
|
||||||
7
advent_of_code/2023/1/test2.txt
Normal file
7
advent_of_code/2023/1/test2.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
two1nine
|
||||||
|
eightwothree
|
||||||
|
abcone2threexyz
|
||||||
|
xtwone3four
|
||||||
|
4nineeightseven2
|
||||||
|
zoneight234
|
||||||
|
7pqrstsixteen
|
||||||
Reference in New Issue
Block a user