day1 and day2
This commit is contained in:
commit
2560669997
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "aoc"
|
||||
version = "0.1.0"
|
||||
authors = ["Glenn Griffin <ggriffiniii@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
aoc-runner = "0.3.0"
|
||||
aoc-runner-derive = "0.3.0"
|
||||
200
input/2020/day1.txt
Normal file
200
input/2020/day1.txt
Normal file
@ -0,0 +1,200 @@
|
||||
1782
|
||||
1344
|
||||
1974
|
||||
1874
|
||||
1800
|
||||
1973
|
||||
1416
|
||||
1952
|
||||
1982
|
||||
1506
|
||||
1642
|
||||
1514
|
||||
1978
|
||||
1895
|
||||
1747
|
||||
1564
|
||||
1398
|
||||
1683
|
||||
1886
|
||||
1492
|
||||
1629
|
||||
1433
|
||||
295
|
||||
1793
|
||||
1740
|
||||
1852
|
||||
1697
|
||||
1471
|
||||
1361
|
||||
1751
|
||||
1426
|
||||
2004
|
||||
1763
|
||||
1663
|
||||
1742
|
||||
1666
|
||||
1733
|
||||
1880
|
||||
1600
|
||||
1723
|
||||
1478
|
||||
1912
|
||||
1820
|
||||
1615
|
||||
1875
|
||||
1547
|
||||
1554
|
||||
752
|
||||
1905
|
||||
1368
|
||||
954
|
||||
1425
|
||||
1391
|
||||
691
|
||||
1835
|
||||
744
|
||||
1850
|
||||
1713
|
||||
1995
|
||||
1926
|
||||
1817
|
||||
1774
|
||||
1986
|
||||
2010
|
||||
1427
|
||||
1609
|
||||
1927
|
||||
1362
|
||||
1420
|
||||
1722
|
||||
1590
|
||||
1925
|
||||
1617
|
||||
1434
|
||||
1826
|
||||
1636
|
||||
1687
|
||||
1946
|
||||
704
|
||||
1797
|
||||
1517
|
||||
1801
|
||||
1865
|
||||
1963
|
||||
1828
|
||||
1829
|
||||
1955
|
||||
1832
|
||||
1987
|
||||
1585
|
||||
1646
|
||||
1575
|
||||
1351
|
||||
1345
|
||||
1729
|
||||
1933
|
||||
1918
|
||||
1902
|
||||
1490
|
||||
1627
|
||||
1370
|
||||
1650
|
||||
1340
|
||||
1539
|
||||
1588
|
||||
1715
|
||||
1573
|
||||
1384
|
||||
1403
|
||||
1673
|
||||
1750
|
||||
1578
|
||||
1831
|
||||
1849
|
||||
1719
|
||||
1359
|
||||
2008
|
||||
1837
|
||||
1958
|
||||
480
|
||||
1388
|
||||
1770
|
||||
1999
|
||||
1066
|
||||
1730
|
||||
1541
|
||||
1802
|
||||
1962
|
||||
1891
|
||||
1816
|
||||
1505
|
||||
1665
|
||||
1551
|
||||
1954
|
||||
1378
|
||||
1998
|
||||
1612
|
||||
1544
|
||||
1953
|
||||
1502
|
||||
1888
|
||||
1655
|
||||
1614
|
||||
1903
|
||||
1675
|
||||
1498
|
||||
1653
|
||||
1769
|
||||
1863
|
||||
1607
|
||||
1945
|
||||
1651
|
||||
1558
|
||||
1777
|
||||
1460
|
||||
1711
|
||||
1677
|
||||
1988
|
||||
1441
|
||||
1821
|
||||
1867
|
||||
1656
|
||||
1731
|
||||
1885
|
||||
1482
|
||||
1439
|
||||
1990
|
||||
1809
|
||||
1794
|
||||
1951
|
||||
1858
|
||||
1969
|
||||
509
|
||||
1486
|
||||
1971
|
||||
1557
|
||||
1896
|
||||
1884
|
||||
1834
|
||||
1814
|
||||
1216
|
||||
1997
|
||||
1966
|
||||
1808
|
||||
1754
|
||||
1804
|
||||
1684
|
||||
2001
|
||||
1699
|
||||
1781
|
||||
1429
|
||||
1322
|
||||
1603
|
||||
1596
|
||||
1823
|
||||
1700
|
||||
1552
|
||||
1352
|
||||
1621
|
||||
1669
|
||||
1000
input/2020/day2.txt
Normal file
1000
input/2020/day2.txt
Normal file
File diff suppressed because it is too large
Load Diff
75
src/lib.rs
Normal file
75
src/lib.rs
Normal file
@ -0,0 +1,75 @@
|
||||
use aoc_runner_derive::{aoc_lib, aoc};
|
||||
|
||||
#[aoc(day1, part1)]
|
||||
pub fn solve_d1_p1(input: &str) -> u64 {
|
||||
let mut entries: Vec<u64> = input.split('\n').map(|x| x.parse().unwrap()).collect();
|
||||
entries.sort();
|
||||
for entry in &entries {
|
||||
let needed = 2020 - entry;
|
||||
if let Ok(idx) = entries.binary_search(&needed) {
|
||||
return entry * entries[idx];
|
||||
}
|
||||
}
|
||||
panic!("not found");
|
||||
}
|
||||
|
||||
#[aoc(day1, part2)]
|
||||
pub fn solve_d1_p2(input: &str) -> Option<u64> {
|
||||
let mut entries: Vec<u64> = input.split('\n').map(|x| x.parse().unwrap()).collect();
|
||||
entries.sort();
|
||||
for i in 0 .. entries.len()-2 {
|
||||
let entry1 = entries[i];
|
||||
for j in i+1 .. entries.len()-1 {
|
||||
let entry2 = entries[j];
|
||||
if entry1 + entry2 > 2020 {
|
||||
break;
|
||||
}
|
||||
for k in j+1 .. entries.len() {
|
||||
let entry3 = entries[k];
|
||||
if entry1 + entry2 + entry3 == 2020 {
|
||||
return Some(entry1 * entry2 * entry3)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return None
|
||||
}
|
||||
|
||||
#[aoc(day2, part1)]
|
||||
pub fn solve_d2_p1(input: &str) -> usize {
|
||||
fn line_is_valid(mut line: &str) -> bool {
|
||||
let lb_idx = line.find('-').unwrap();
|
||||
let lb: usize = (&line[..lb_idx]).parse().unwrap();
|
||||
line = &line[lb_idx+1..];
|
||||
|
||||
let ub_idx = line.find(' ').unwrap();
|
||||
let ub: usize = (&line[..ub_idx]).parse().unwrap();
|
||||
line = &line[ub_idx+1..];
|
||||
|
||||
let letter = line.as_bytes()[0];
|
||||
let passwd = &line[3..];
|
||||
let count = passwd.bytes().filter(|&b| b == letter).count();
|
||||
count >= lb && count <= ub
|
||||
}
|
||||
input.split('\n').filter(|x| line_is_valid(x)).count()
|
||||
}
|
||||
|
||||
#[aoc(day2, part2)]
|
||||
pub fn solve_d2_p2(input: &str) -> usize {
|
||||
fn line_is_valid(mut line: &str) -> bool {
|
||||
let lb_idx = line.find('-').unwrap();
|
||||
let lb: usize = (&line[..lb_idx]).parse().unwrap();
|
||||
line = &line[lb_idx+1..];
|
||||
|
||||
let ub_idx = line.find(' ').unwrap();
|
||||
let ub: usize = (&line[..ub_idx]).parse().unwrap();
|
||||
line = &line[ub_idx+1..];
|
||||
|
||||
let letter = line.as_bytes()[0];
|
||||
let passwd = &line[3..].as_bytes();
|
||||
(passwd[lb-1] == letter) ^ (passwd[ub-1] == letter)
|
||||
}
|
||||
input.split('\n').filter(|x| line_is_valid(x)).count()
|
||||
}
|
||||
|
||||
aoc_lib!{ year = 2020 }
|
||||
Loading…
x
Reference in New Issue
Block a user