Added other 2 days

This commit is contained in:
Sebastian 2020-12-07 18:04:24 +01:00
parent bc3de2d4fb
commit 26444ce61b
9 changed files with 1330 additions and 1 deletions

19
Cargo.lock generated
View File

@ -3,4 +3,23 @@
[[package]]
name = "AoC2020"
version = "0.1.0"
dependencies = [
"itertools 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "either"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "itertools"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"either 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum either 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
"checksum itertools 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b"

View File

@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = "0.9.0"

200
inputs/day1.txt Normal file
View File

@ -0,0 +1,200 @@
1962
1577
1750
1836
1762
1691
1726
1588
1370
1043
1307
1552
1813
1804
1765
1893
1610
764
1512
1404
1711
1000
1694
1546
1880
1721
2006
1787
1510
1850
1420
1712
1926
1707
1983
1680
1436
389
1448
1875
1333
1733
1935
1794
1337
1863
1769
1635
1499
1807
1326
1989
1705
1673
1829
1684
1716
456
1696
1398
1942
1851
1690
1328
1356
1775
1564
1466
1273
1896
766
1814
1810
1537
1463
1755
1341
1665
1520
1366
1387
1976
1717
1737
1551
1760
1496
1664
1450
1319
1674
1630
1301
1330
1658
1637
1655
1439
1832
1948
1339
1656
1449
1296
1489
1758
1939
1857
1402
1394
1882
1446
1412
1430
1212
1377
1501
1873
1812
1667
1560
1654
1575
1999
1581
1792
1299
1843
1383
1351
1297
1822
1801
1977
1316
1477
1980
1693
1220
1554
1607
1903
1669
1593
1955
1286
1909
1280
1854
2005
1820
1803
1763
1660
1410
1974
1808
1816
1723
1936
1423
1818
1800
1294
857
496
1248
1670
1993
1929
1966
1381
1259
1285
1797
1644
1919
1267
1509
399
1300
1662
1556
1747
1517
1972
1729
1506
1544
1957
1930
1956
1753
1284
1389
1689
1709
1627
1770
847

1000
inputs/day2.txt Normal file

File diff suppressed because it is too large Load Diff

27
src/bin/day1.rs Normal file
View File

@ -0,0 +1,27 @@
use itertools::Itertools;
use std::error::Error;
use std::fs::File;
use std::io::{self, BufRead};
use std::vec::Vec;
fn main() -> Result<(), Box<dyn Error>> {
let file = File::open("inputs/day1.txt")?;
let lines = io::BufReader::new(file).lines();
let numbers: Vec<u32> = lines.map(|l| l.unwrap().parse().unwrap()).collect();
let count = 3;
for combo in numbers.iter().combinations(count) {
if combo.iter().fold(0u32, |acc, x| acc + **x) == 2020 {
println!(
"Combo: {:?} == {}",
combo,
combo.iter().fold(1u32, |acc, x| acc * **x)
);
break;
}
}
Ok(())
}

38
src/bin/day2.rs Normal file
View File

@ -0,0 +1,38 @@
use itertools::Itertools;
use std::error::Error;
use std::fs::File;
use std::io::{self, BufRead};
use std::vec::Vec;
fn is_valid(line: &Vec<String>) -> bool {
let min: usize = line[0].parse().unwrap();
let max: usize = line[1].parse().unwrap();
let policy_char = line[2].chars().nth(0).unwrap();
let count = line[3].matches(policy_char).count();
count >= min && count <= max
}
fn main() -> Result<(), Box<dyn Error>> {
let file = File::open("inputs/day2.txt")?;
let lines = io::BufReader::new(file).lines().map(|l| l.unwrap());
let parts: Vec<Vec<String>> = lines
.map(|l| {
l.split(|c| c == ' ' || c == ':' || c == '-')
.filter(|s| *s != "")
.map(|s| s.to_string())
.collect()
})
.collect();
let valid_count = parts
.iter()
.map(|p| if is_valid(p) { 1 } else { 0 })
.fold(0, |acc, x| acc + x);
println!("Total valid: {}", valid_count);
Ok(())
}

44
src/bin/day2_part2.rs Normal file
View File

@ -0,0 +1,44 @@
use itertools::Itertools;
use std::error::Error;
use std::fs::File;
use std::io::{self, BufRead};
use std::vec::Vec;
fn is_valid(line: &Vec<String>) -> bool {
let pos1: usize = line[0].parse().unwrap();
let pos2: usize = line[1].parse().unwrap();
let policy_char = line[2].chars().nth(0).unwrap();
let char1 = line[3].chars().nth(pos1-1);
let char2 = line[3].chars().nth(pos2-1);
match (char1, char2) {
(Some(c1), Some(c2)) => (c1 == policy_char) ^ (c2 == policy_char),
(Some(c), None) => (c == policy_char),
(None, Some(c)) => (c == policy_char),
(None, None) => false,
}
}
fn main() -> Result<(), Box<dyn Error>> {
let file = File::open("inputs/day2.txt")?;
let lines = io::BufReader::new(file).lines().map(|l| l.unwrap());
let parts: Vec<Vec<String>> = lines
.map(|l| {
l.split(|c| c == ' ' || c == ':' || c == '-')
.filter(|s| *s != "")
.map(|s| s.to_string())
.collect()
})
.collect();
let valid_count = parts
.iter()
.map(|p| if is_valid(p) { 1 } else { 0 })
.fold(0, |acc, x| acc + x);
println!("Total valid: {}", valid_count);
Ok(())
}

View File

@ -4,7 +4,7 @@ use std::io::{self, BufRead};
use std::vec::Vec;
fn main() -> Result<(), Box<dyn Error>> {
let file = File::open("inputs/problem3.txt")?;
let file = File::open("inputs/day3.txt")?;
let lines = io::BufReader::new(file).lines();
let trees: Vec<Vec<bool>> = lines