added day 1-2

This commit is contained in:
Ishan Jain 2023-12-01 13:24:56 +05:30
parent 97bec285ce
commit af973212d8
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
4 changed files with 151 additions and 5 deletions

7
Cargo.lock generated
View File

@ -12,6 +12,7 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
name = "aoc2023" name = "aoc2023"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"tap",
"ureq", "ureq",
] ]
@ -170,6 +171,12 @@ version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"

View File

@ -18,6 +18,7 @@ codegen-units = 16
rpath = false rpath = false
[dependencies] [dependencies]
tap = "1.0.1"
ureq = { version = "2.9.1" } ureq = { version = "2.9.1" }

View File

@ -1 +1,134 @@
const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; use std::collections::VecDeque;
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
fn main() {
for input in INPUTS.iter() {
let mut total: u64 = 0;
for line in input.split('\n') {
let line: Vec<char> = line.chars().collect();
let mut first = None;
let mut last = 0;
let mut tmp: VecDeque<char> = VecDeque::new();
for c in line.iter() {
if tmp.len() >= 5 {
tmp.pop_front();
}
match c {
'1' => {
if first.is_none() {
first = Some(10 * 1);
}
last = 1;
tmp.clear();
}
'2' => {
if first.is_none() {
first = Some(10 * 2);
}
last = 2;
tmp.clear();
}
'3' => {
if first.is_none() {
first = Some(10 * 3);
}
last = 3;
tmp.clear();
}
'4' => {
if first.is_none() {
first = Some(10 * 4);
}
last = 4;
tmp.clear();
}
'5' => {
if first.is_none() {
first = Some(10 * 5);
}
last = 5;
tmp.clear();
}
'6' => {
if first.is_none() {
first = Some(10 * 6);
}
last = 6;
tmp.clear();
}
'7' => {
if first.is_none() {
first = Some(10 * 7);
}
last = 7;
tmp.clear();
}
'8' => {
if first.is_none() {
first = Some(10 * 8);
}
last = 8;
tmp.clear();
}
'9' => {
if first.is_none() {
first = Some(10 * 9);
}
last = 9;
tmp.clear();
}
c => {
tmp.push_back(*c);
}
}
if let Some(digit) = get_digit(tmp.make_contiguous()) {
if first.is_none() {
first = Some(10 * digit);
}
last = digit;
}
}
if let Some(digit) = get_digit(tmp.make_contiguous()) {
if first.is_none() {
first = Some(10 * digit);
}
last = digit;
}
total += first.unwrap_or(0) as u64 + last as u64;
}
println!("total = {}", total);
}
}
fn get_digit(set: &[char]) -> Option<u8> {
match set {
['t', 'h', 'r', 'e', 'e'] => Some(3),
['s', 'e', 'v', 'e', 'n'] => Some(7),
['e', 'i', 'g', 'h', 't'] => Some(8),
['o', 'n', 'e', _, _] | ['o', 'n', 'e'] | [_, 'o', 'n', 'e'] | [_, _, 'o', 'n', 'e'] => {
Some(1)
}
['t', 'w', 'o', _, _] | ['t', 'w', 'o'] | [_, 't', 'w', 'o'] | [_, _, 't', 'w', 'o'] => {
Some(2)
}
['s', 'i', 'x', _, _] | ['s', 'i', 'x'] | [_, 's', 'i', 'x'] | [_, _, 's', 'i', 'x'] => {
Some(6)
}
['n', 'i', 'n', 'e', _] | ['n', 'i', 'n', 'e'] | [_, 'n', 'i', 'n', 'e'] => Some(9),
['f', 'o', 'u', 'r', _] | ['f', 'o', 'u', 'r'] | [_, 'f', 'o', 'u', 'r'] => Some(4),
['f', 'i', 'v', 'e', _] | ['f', 'i', 'v', 'e'] | [_, 'f', 'i', 'v', 'e'] => Some(5),
_ => None,
}
}

View File

@ -1,4 +1,9 @@
1abc2 two1nine
pqr3stu8vwx eightwothree
a1b2c3d4e5f abcone2threexyz
treb7uchet xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
2tqbxgrrpmxqfglsqjkqthree6nhjvbxpflhr1eightwohr