1
0
Fork 0

day(25): add initial solution

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2023-01-07 16:42:03 +01:00
parent 89a9758e64
commit 38a2440438
Signed by: mfocko
GPG key ID: 7C47D46246790496
2 changed files with 135 additions and 0 deletions

13
samples/day25.txt Normal file
View file

@ -0,0 +1,13 @@
1=-0-2
12111
2=0=
21
2=01
111
20012
112
1=-1=
1-12
12
1=
122

122
src/bin/day25.rs Normal file
View file

@ -0,0 +1,122 @@
use std::{char::from_digit, collections::HashMap};
use aoc_2022::*;
type Input = Vec<String>;
type Output = String;
fn from_snafu(s: &str) -> isize {
(0..)
.zip(s.chars().rev())
.map(|(e, d)| {
5_isize.pow(e)
* match d {
'-' => -1,
'=' => -2,
_ => d.to_digit(10).unwrap().try_into().unwrap(),
}
})
.sum()
}
fn to_snafu(mut n: isize) -> String {
let mut digits: Vec<char> = Vec::new();
while n > 0 {
let mut d = n % 5;
if d > 2 {
n += 5 - d;
d -= 5;
}
digits.push(match d {
-1 => '-',
-2 => '=',
_ => from_digit(d as u32, 10).unwrap(),
});
n /= 5;
}
digits.reverse();
digits.iter().join("")
}
struct Day25;
impl Solution<Input, Output> for Day25 {
fn parse_input<P: AsRef<Path>>(pathname: P) -> Input {
file_to_lines(pathname)
}
fn part_1(input: &Input) -> Output {
to_snafu(input.iter().map(|s| from_snafu(s)).sum())
}
fn part_2(_input: &Input) -> Output {
"Merry Chrysler!!!".to_string()
}
}
fn main() -> Result<()> {
// Day25::run("sample")
Day25::main()
}
test_sample!(
day_25,
Day25,
"2=-1=0".to_string(),
"Merry Chrysler!!!".to_string()
);
lazy_static! {
static ref EXAMPLES: HashMap<isize, &'static str> = HashMap::from([
(1, "1"),
(2, "2"),
(3, "1="),
(4, "1-"),
(5, "10"),
(6, "11"),
(7, "12"),
(8, "2="),
(9, "2-"),
(10, "20"),
(15, "1=0"),
(20, "1-0"),
(2022, "1=11-2"),
(12345, "1-0---0"),
(314159265, "1121-1110-1=0"),
(1747, "1=-0-2"),
(906, "12111"),
(198, "2=0="),
(11, "21"),
(201, "2=01"),
(31, "111"),
(1257, "20012"),
(32, "112"),
(353, "1=-1="),
(107, "1-12"),
(37, "122"),
(4890, "2=-1=0"),
]);
}
#[cfg(test)]
mod day_25_snafu {
use super::*;
#[test]
fn test_from() {
for (n, s) in EXAMPLES.iter() {
assert_eq!(from_snafu(s), *n);
}
}
#[test]
fn test_to() {
for (n, s) in EXAMPLES.iter() {
assert_eq!(to_snafu(*n), s.to_string());
}
}
}