Watch
1
0
Fork
You've already forked advent-of-code
0

2022 Day 25 Part 1

This commit is contained in:
Jeremy Kaplan 2022-12-24 21:42:48 -08:00
commit b9661d77be

59
2022/src/bin/day25.rs Normal file
View file

@ -0,0 +1,59 @@
use std::collections::VecDeque;
const INPUT: &str = include_str!("../../input/day25.txt");
fn main() {
println!("{}", part1(INPUT));
}
fn part1(input: &str) -> String {
let mut sum = 0;
for line in input.lines() {
let mut num: i64 = 0;
for (i, digit) in line.chars().rev().enumerate() {
let val = match digit {
'=' => -2,
'-' => -1,
'0' => 0,
'1' => 1,
'2' => 2,
other => panic!("{:?}", other),
};
num += 5_i64.pow(i.try_into().unwrap()) * val;
}
sum += num;
}
snafu(sum)
}
fn snafu(mut n: i64) -> String {
assert!(n > 0);
let mut digits: VecDeque<i64> = VecDeque::new();
while n > 0 {
match n % 5 {
3 => digits.push_front(-2),
4 => digits.push_front(-1),
0 => digits.push_front(0),
1 => digits.push_front(1),
2 => digits.push_front(2),
other => panic!("{:?}", other),
};
n -= digits[0];
n /= 5;
}
digits
.iter()
.map(|digit| match digit {
-2 => '=',
-1 => '-',
0 => '0',
1 => '1',
2 => '2',
other => panic!("{:?}", other),
})
.collect()
}