~sgeisenh/aoc

96ae00a42d9124ff66b99030ff5d0c30838646ff — Samuel Eisenhandler 10 months ago 74614df
2018 day 2
A 2018/inputs/02.txt => 2018/inputs/02.txt +250 -0
@@ 0,0 1,250 @@
tjxmoewpqkyaiqvmndgflunszc
tjxmobwpqkyaihvrndgfjubszm
tjxmzewpqkyaihvrydgflrbszc
tjxmoeypqkyvihvrndgflubsza
tjcmoewpqkytihvrndgflgbszc
tjvmoewpqkyanevrndgflubszc
tjxmoewpqkdiihirndgflubszc
tjxboewpqkyaihbrnogflubszc
ojpmoewpqkyaihvjndgflubszc
tjxyoewpqkyaiuvrndgflutszc
tjxmoewpqkyalhvrndmflebszc
tjxmoewpqzyaihhrndgflubszf
tjxmrewpqkyaihirndgfjubszc
pjxmoewpqkyaihvendgfbubszc
txxmkewpqkyjihvrndgflubszc
tjxmoewcqkyaihvrnmgflubczc
tjxmoewkqkyaghvrndgfluvszc
tjxmoewfqkhaihvrndgflubhzc
jjqmoewpqkyaihvrndzflubszc
tjxmoewmqksaihvcndgflubszc
tjrmoewpqkyaihvrvdgflubzzc
tjxxoewpqkyaiiwrndgflubszc
cjxmoawxqkyaihvrndgflubszc
tjxdoewpvkyaihvrndgflubsoc
tjxmsewpqkyaihvrndgfluzozc
tjxmoewpqkyafhvrnyeflubszc
tjxmlewpqkyawhvondgflubszc
tjxmonwpqkyaiqvrnxgflubszc
tjxmoewcqkyaihvrnjgflumszc
tjvmoewpqkyaihveadgflubszc
tjxmogfpqkyaigvrndgflubszc
tybmoewpqkyaihvrndgllubszc
tjxmoewpdkyaihvrndgfluwbzc
etxmbewpqkyaihvrndgflubszc
tjxmoeapqcynihvrndgflubszc
tbxmoewpqkyaihvrndgfdebszc
haxmoewpqyyaihvrndgflubszc
ojxmoewpqkyaihvrnegflubszr
tjxmoewpqkyaihvrndoflubarc
ljxmoewpqkykihvrndgflvbszc
tjxmovwpqkyaihvrndgfluzsyc
tvxmoewpqkyanhvrkdgflubszc
tjxmoewpqkyaihkrndgfluwwzc
zjxmoewpfkyaihvrndgfrubszc
tjxyoegpqkyaihvrndlflubszc
tjxmoewpqkyamhvrnsgflubmzc
tjmmoewpqkyaihvrndgftuwszc
tjxmoewpqbraihvrncgflubszc
tjxmeeepqkyainvrndgflubszc
tjemoegpqkyaihvredgflubszc
tjxmoewpqkyaihvdndgfzubqzc
tjxmoegrqkyaihfrndgflubszc
tjxmoewpqxyaihvrndgfluyvzc
qjxmoewpqkyaiwvrnfgflubszc
tjxwoewpqkyashkrndgflubszc
tjzmoewiqkyaihurndgflubszc
tjumuewpqkyaihvrndgflubssc
tyxooewpukyaihvrndgflubszc
tjxvoewpqkyaiivindgflubszc
ijxmoqwpqkyaihvradgflubszc
tjxmlewpqkyaihvrhdgflubwzc
tjxmkewpqkyajhqrndgflubszc
tjxmoewpqkqaiherndgflurszc
tjamoewpqkyaizvondgflubszc
tjxgogwpqkyalhvrndgflubszc
tjxmoewpqkyachvrndgflubuzq
tjxmowqpqkyaihvrnegflubszc
mjxmoewpwkyaihvrndgfkubszc
tpbmoewpqkyaihvrzdgflubszc
tjbmoewpqkyaiuvrndgflsbszc
tjxmoewpqklaghvrndgflubazc
tjxmoewpqkyrihvrndgwlpbszc
tjcmoewpqksaiyvrndgflubszc
tjxmoeapqkymihvindgflubszc
tjxmdewpqkyafhvrndgflqbszc
tjxmoewpqxyaihvrndsflubszi
tjxmoeppqkyaihvrcdgflubszd
tjxmomwpqkyainvrmdgflubszc
tjxmovwpqkyaihvrndgfdubdzc
tjxmoewwqkiaihvrjdgflubszc
tmxmoewpqkyaifvrndgflubszs
tbxmoewpqkyaihvrbdgflunszc
tjxmoewrqkyaihvxndgflubszp
ujxmoewpqkyaihvxndgflubpzc
tdxmotwpqkyaihvdndgflubszc
tjxmvewpqkyaihfrndgtlubszc
tjfmoewpqkyaihvrnyqflubszc
tjxfolwzqkyaihvrndgflubszc
ojrmoiwpqkyaihvrndgflubszc
tjsmoqwpqkyqihvrndgflubszc
tjxmohwpqkyaihvrudgflubslc
tjxtoiwpqkyaihvrnogflubszc
taxmoewpqkyaiyvrndgfwubszc
tjxwnezpqkyaihvrndgflubszc
tjxmyevpqkyaivvrndgflubszc
tjxdoeopqkyaihvgndgflubszc
tjxaoewpqkmaihvrndgflufszc
tjxmoewpqkyaxhvrndgflubncc
tjxmoewpqkyaihurndgflubbjc
tjxmjewpqgyaihvrnngflubszc
tjxmogwpqkyaihvrndgflubbcc
tjxmoewplkyaihvrnpgflibszc
tjwmoewpqkyaohvrndgfbubszc
tjwmoewpqkyaihvrndgfsubszm
tjxmogwpqkyaihvrndiflubqzc
tjxmoewpqkyaihvrndgflopshc
rjxmlewpvkyaihvrndgflubszc
tjxmogwpakyaihvrndgflzbszc
tjxmoeppqkyaihvrndgflmxszc
tjxmoewpqkyhihgrndgfzubszc
tjxqoewpqkyaihtrndgwlubszc
tjxnoespqkyaihvrndgflubsuc
tjmmoewpqkraihvrndgflfbszc
tjxmoewnqkwaihvrndgflubstc
tjxmoewpqqyaihvrndgfljbszi
tjxmoewpqkyaihkrkdgalubszc
tjxmoewpqkyaihvradgjlurszc
tvxmoewpqkybihvrndbflubszc
tjxvoewpqkyaihvradgfoubszc
tjxmoewpqfyaihvlodgflubszc
tjxmoewmnkyaiivrndgflubszc
kjxmoewpqkyaihprndgflcbszc
hjxmoewpqkcaihvrndgvlubszc
tjxmoewcqkyaihvrncgfllbszc
tuxmoewpckyaihvrndoflubszc
tjxmdewpokyaihvrndgflubszn
mjxmaewpqkyaqhvrndgflubszc
tjxmoewpmzyaihvrndgfiubszc
tjxmoewnqkyvihvrndgflubszk
tjxmoewpmnyaihvrndgftubszc
zjxmoewpqkysihvrndgfmubszc
tjxmoewpqkyaihzrntgflubbzc
tjxmoewpqkgaihwrndsflubszc
tjxjoewpqkyaihvrndgflgbizc
oqxmoewpqkyaihvrndgfldbszc
wjamoewpqkyaihvfndgflubszc
tjxmoewtmkyvihvrndgflubszc
tjlmojwpqkyaihvrndgfludszc
tjxmowwpqkyaihvrndefludszc
tjxmoewpqkbaihvrndgfluaszt
tjxmoewpqkzaahvrodgflubszc
tjxmoewpqkgaihvrndgflubtpc
tjxmoenpqkyaihcrndgfqubszc
tbxmoewpqbyaihvrndgalubszc
tjvmoewqqkyaihvrndvflubszc
tjxmoewpqkeaihvundgfaubszc
txxmoewpqkyaihvrwdgflpbszc
tzxmoewpqkijihvrndgflubszc
tjxmoewoqkytiuvrndgflubszc
tjxmrejplkyaihvrndgflubszc
tjxmoewpqkynihvrpxgflubszc
tjxmoewpqkvanhvrndgvlubszc
tjxmoewpdkyiihvrndgflubszs
tpxmyewpqkyaihvrndgfeubszc
tpxmoewpqyyaihvrndhflubszc
tjsmoewpqkyaihvrndhflubnzc
tjxmoewpukyaihvrnmgflubwzc
txxmoewpqlyaihwrndgflubszc
tjxmoewprkyaiovrndgflubxzc
tjxmouwpqkyaihzrodgflubszc
tjxmojwpqkywimvrndgflubszc
tjxsoewpqkyaihvrzdgqlubszc
tfxmoewpakyaihvrndgllubszc
tjhmoewpqiyaihvrndgflubsac
tjxmoewpqkoaihvrndoflubsxc
tjxmoewpqkyzpjvrndgflubszc
tjxmoewpqkyaiharndgzlnbszc
tjimoevpqkyaihvrndgflubbzc
tjxsoewpqkyahhvrndgfzubszc
txxmoewpqkyaimvrrdgflubszc
tjxmoewpwkyaihvrndpylubszc
tjxmoewpskyaghvrndgfbubszc
tjxmuewpqmyaihvrndgfyubszc
tjxmoewpqkyaihvdndgglubsxc
xjxmoewpqkyjiovrndgflubszc
gjxmoewpqkyaihvrndodlubszc
tjbmoewpqkyaihvridgflvbszc
tjxmozwpqkyapbvrndgflubszc
tjxeoewpqkyqihvrndgflubhzc
tjxdoewpqzyaihvrndgflubsmc
tjxmwewpqkyathvcndgflubszc
tjxmoewpszyaihvrndgflusszc
tuxmoewpqkyaihvrndgfluasxc
tjemoewpnvyaihvrndgflubszc
tjxmoewpjkyaihvrndgjlufszc
tjomoewppkyaihvzndgflubszc
tjxmvewpqkyaimvrntgflubszc
rjxmoewpqkyaihvpndgflubszq
hjxzoewpqkyaihvridgflubszc
texmoejpqkyaihvrndgflubszx
tjxcoewpqkyaihbrxdgflubszc
tjxmoewpnkyaihvrndgfltbsze
tjxmoewpdkyaihvrndwfluwbzc
tjxmoewpqryjihkrndgflubszc
tjlmoewpqkhaihvrndgflubsnc
tjxmovapqkjaihvrndgflubszc
tjxvoewpqkyaihqrndgfluyszc
tjxwoewnqkyaihvrndgfgubszc
tjdmoewpqklaihvcndgflubszc
tjxmoewpvkynihvrndgflubskc
tjxmtewpqkyaihvhndgfluaszc
tjxmoewpqkyanhvrnpgfluvszc
tjxmoewpqkyaifvbndgflubspc
tjxmoexpqknaihvrndgxlubszc
qjxmoewqqkyaihvrndgflubpzc
tjxmoewppkyaihvaxdgflubszc
myxmoewpqkyaihvrudgflubszc
tjxmwewpmkyaihvrndgflubssc
tjxmoewpqkyaihvrndgfxqbszq
tjxmoewhqkyaahvrndgflubbzc
tbxmoewmqkyaihvrndgflubszu
toxmolwpqkyaihvrndnflubszc
tjxmoewhqkyaihvrnrgflubvzc
yjxmoewcqkyaihvrndgflubfzc
tjxmoewpqkyamhvrgdgflmbszc
tjxmtewpqkyaizvrndgfluoszc
tjxmoewpqzyaihvrntsflubszc
fjxmoewpqkyaihyrmdgflubszc
tjxwoewpqcyaihbrndgflubszc
tjxmoebpqkzaihvrndcflubszc
tjxmoewpqkyaihvrndnlmubszc
tjxmoewpqkyaihvrndgeyubskc
tfxmoewpqryaihvrndgfluiszc
tjxmoewpqkjaihvynngflubszc
tjxmoewpqkqaihvonjgflubszc
tjfmokwpqkyeihvrndgflubszc
djxmoewpkkyaihvrnmgflubszc
tjxmiewpqkyaihvrndgflubhlc
tjxmmejpqkyaihvrnhgflubszc
djxmoewmqkyaihvrnggflubszc
tjxmoewpqkyaihvrkggflubsze
gjxmoewpqkyaihjrndgflvbszc
tjxmoewpqkyaidvrndgkzubszc
tjxmoewpqkyaedvrnpgflubszc
sjxmoewpqkyaihvrnngfluhszc
tjxmoewpqkuaihvrndghlubxzc
tjxmoewgqkyuihvrndgftubszc
tjxmoewpqsyaifvrkdgflubszc
tjxrrewpqkyaihvrnpgflubszc
tjxmoezpqkyaihvrwdgflabszc
tjfmoewpqknaihvrndgflubkzc
tjxmoewnqkxaihvrndgflubtzc
tjxmoewpkkyaihvrndgfrnbszc
tjxmorwpnkqaihvrndgflubszc
tsxmoewwqkyathvrndgflubszc
tjxmoeupqkyaihvrndyflubszp
bjxmoewdqkyaihvrndgflurszc
tjxmoewpvkyaihvrndoflubszq
sjxmoewpqkyaihvrndgflubyec
tjxmoewpqkyaizcrndgfnubszc
\ No newline at end of file

A 2018/rust/day_02/Cargo.lock => 2018/rust/day_02/Cargo.lock +7 -0
@@ 0,0 1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "day_02"
version = "0.1.0"

A 2018/rust/day_02/Cargo.toml => 2018/rust/day_02/Cargo.toml +8 -0
@@ 0,0 1,8 @@
[package]
name = "day_02"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

A 2018/rust/day_02/src/main.rs => 2018/rust/day_02/src/main.rs +85 -0
@@ 0,0 1,85 @@
fn histogram(line: &str) -> [i32; 26] {
    let mut counts = [0; 26];
    for c in line.chars() {
        counts[c as usize - 'a' as usize] += 1;
    }
    counts
}

fn has_exactly_n(counts: &[i32; 26], count: i32) -> bool {
    counts.iter().any(|&c| c == count)
}

fn part_one(input: &str) -> i32 {
    let mut twos = 0;
    let mut threes = 0;
    for line in input.lines() {
        let counts = histogram(line);
        if has_exactly_n(&counts, 2) {
            twos += 1;
        }
        if has_exactly_n(&counts, 3) {
            threes += 1;
        }
    }
    twos * threes
}

fn difference(left: &str, right: &str) -> i32 {
    left.chars().zip(right.chars()).map(|(l, r)| (l != r) as i32).sum()
}

fn part_two(input: &str) -> String {
    let lines: Vec<_> = input.lines().collect();
    for i in 0..lines.len() {
        for j in i+1..lines.len() {
            if difference(lines[i], lines[j]) == 1 {
                return lines[i].chars().zip(lines[j].chars())
                    .filter(|(l, r)| l == r)
                    .map(|(l, _)| l)
                    .collect();
            }
        }
    }
    panic!("No solution found");
}

fn main() {
    let input = include_str!("../../../inputs/02.txt");
    println!("Part one: {}", part_one(input));
    println!("Part two: {}", part_two(input));
}


#[cfg(test)]
mod tests {
    use super::*;

    const EXAMPLE: &str = "\
abcdef
bababc
abbcde
abcccd
aabcdd
abcdee
ababab";

    #[test]
    fn test_part_one() {
        assert_eq!(part_one(EXAMPLE), 12);
    }

    const EXAMPLE2: &str = "\
abcde
fghij
klmno
pqrst
fguij
axcye
wvxyz";

    #[test]
    fn test_part_two() {
        assert_eq!(part_two(EXAMPLE2), "fgij");
    }
}
\ No newline at end of file