diff options
author | j-james | 2022-12-05 07:15:53 +0000 |
---|---|---|
committer | j-james | 2022-12-05 07:15:53 +0000 |
commit | fb2dd0f45fa8f8a1045ee9c76c1bca3cc71cb0ef (patch) | |
tree | a49b3f49906bf4601bf6a1826784b6e0c5531dfe /2022 | |
parent | f5f940c52b38a5e17092f5896dc2fc13e8443478 (diff) |
Day Three
Diffstat (limited to '2022')
-rw-r--r-- | 2022/nim/day03/src/main.nim | 18 | ||||
-rw-r--r-- | 2022/rust/day03/Cargo.lock | 7 | ||||
-rw-r--r-- | 2022/rust/day03/Cargo.toml | 8 | ||||
-rw-r--r-- | 2022/rust/day03/src/main.rs | 43 |
4 files changed, 76 insertions, 0 deletions
diff --git a/2022/nim/day03/src/main.nim b/2022/nim/day03/src/main.nim new file mode 100644 index 0000000..782d9f6 --- /dev/null +++ b/2022/nim/day03/src/main.nim @@ -0,0 +1,18 @@ +# Day Three: Rucksack Reorganization +import std/[os, strutils, sequtils, sugar] + +let input = paramStr(1).readFile().strip().split("\n") + +func deascii(x: char): int = + if x.ord > 96: + x.ord - 96 + else: + x.ord - 38 + +echo input.mapIt((it[0..(it.len div 2 - 1)], it[(it.len div 2)..(it.len - 1)])) + .mapIt(it[0].filter(x => x in it[1])[0]) + .mapIt(it.deascii()).foldl(a+b, 0) + +echo input.distribute(100) + .mapIt(it[0].filter(x => x in it[1] and x in it[2])[0]) + .mapIt(it.deascii()).foldl(a+b, 0) diff --git a/2022/rust/day03/Cargo.lock b/2022/rust/day03/Cargo.lock new file mode 100644 index 0000000..21c74cb --- /dev/null +++ b/2022/rust/day03/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day03" +version = "0.1.0" diff --git a/2022/rust/day03/Cargo.toml b/2022/rust/day03/Cargo.toml new file mode 100644 index 0000000..2804bab --- /dev/null +++ b/2022/rust/day03/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day03" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2022/rust/day03/src/main.rs b/2022/rust/day03/src/main.rs new file mode 100644 index 0000000..db5b83e --- /dev/null +++ b/2022/rust/day03/src/main.rs @@ -0,0 +1,43 @@ +#![feature(iter_array_chunks)] +use std::env; +use std::fs; + +fn main() { + let args = env::args().nth(1).expect("missing input"); + let input = fs::read_to_string(args).unwrap(); + let rucksacks = input.trim().split("\n"); + + let compartments: Vec<(&str, &str)> = rucksacks.clone() + .map(|x| x.split_at(x.len() / 2)).collect(); + let mut sum = 0; + for (a, b) in compartments.iter() { + for c in a.chars() { + if b.contains(c) { + sum += deascii(c); + break; + } + } + // println!("{:?}, {:?}", a, b); + } + println!("{}", sum); + + sum = 0; + // yoo this is sick + for a in rucksacks.array_chunks::<3>() { + for c in a[0].chars() { + if a[1].contains(c) && a[2].contains(c) { + sum += deascii(c); + break; + } + } + } + println!("{}", sum); +} + +fn deascii(x: char) -> i64 { + if x as i64 > 96 { + return x as i64 - 96; + } else { + return x as i64 - 38; + } +} |