days 1–4

master
Matthew Butterick 4 years ago
parent 3dbf136103
commit 2f92fcc170

@ -0,0 +1,10 @@
#lang br
(require racket/file rackunit)
(define (solve combo-length)
(for/first ([c (in-combinations (map string->number (file->lines "01.rktd")) combo-length)]
#:when (eq? 2020 (apply + c)))
(apply * c)))
(check-equal? (solve 2) 1007331)
(check-equal? (solve 3) 48914340)

@ -0,0 +1,200 @@
1834
1546
1119
1870
1193
1198
1542
1944
1817
1249
1361
1856
1258
1425
1835
1520
1792
1130
2004
1366
1549
1347
1507
1699
1491
1557
1865
1948
1199
1229
1598
1756
1643
1306
1838
1157
1745
1603
1972
1123
1963
1759
1118
1526
1695
1661
1262
1117
1844
1922
1997
1630
1337
1721
1147
1848
1476
1975
1942
1569
1126
1313
1449
1206
1722
1534
1706
1596
1700
1811
906
1666
1945
1271
1629
1456
1316
1636
1884
1556
1317
1393
1953
1658
2005
1252
1878
1691
60
1872
386
1369
1739
1460
1267
1935
1992
1310
1818
1320
1437
1486
1205
1286
1670
1577
1237
1558
1937
1938
1656
1220
1732
1647
1857
1446
1516
1450
1860
1625
1377
1312
1588
1895
1967
1567
1582
1428
1415
1731
1919
1651
1597
1982
1576
1172
1568
1867
1660
1754
1227
1121
1733
537
1809
1322
1876
1665
1124
1461
1888
1368
1235
1479
1529
1148
1996
1939
1340
1531
1438
1897
1152
1321
1770
897
1750
1111
1772
1615
1798
1359
1470
1610
1362
1973
1892
1830
599
1341
1681
1572
1873
42
1246
1447
1800
1524
1214
1784
1664
1882
1989
1797
1211
1170
1854
1287
1641
1760

@ -0,0 +1,21 @@
#lang br
(require racket/file rackunit)
(struct rec (password target low high))
(define recs (for/list ([ln (file->lines "02.rktd")])
(match-define (list range target password) (string-split ln))
(match-define (list low high) (map string->number (string-split range "-")))
(rec password (string-ref (string-trim target ":") 0) low high)))
(check-equal?
(for/sum ([rec recs])
(define howmany (count (λ (c) (char=? c (rec-target rec))) (string->list (rec-password rec))))
(if (<= (rec-low rec) howmany (rec-high rec)) 1 0))
422)
(check-equal?
(for/sum ([rec recs])
(define low? (char=? (rec-target rec) (string-ref (rec-password rec) (sub1 (rec-low rec)))))
(define high? (char=? (rec-target rec) (string-ref (rec-password rec) (sub1 (rec-high rec)))))
(if (or (and low? (not high?)) (and high? (not low?))) 1 0))
451)

File diff suppressed because it is too large Load Diff

@ -0,0 +1,20 @@
#lang br
(require racket/file rackunit)
(define (trees-in-slope horiz vert)
(for/fold ([mod 0]
[sum 0]
#:result sum)
([(ln lidx) (in-indexed (file->lines "03.rktd"))]
#:when (zero? (modulo lidx vert)))
(values (+ mod horiz)
(if (char=? #\# (string-ref ln (modulo mod (string-length ln)))) (add1 sum) sum))))
(check-equal? (trees-in-slope 3 1) 151)
(check-equal? (apply * (list
(trees-in-slope 1 1)
(trees-in-slope 3 1)
(trees-in-slope 5 1)
(trees-in-slope 7 1)
(trees-in-slope 1 2))) 7540141059)

@ -0,0 +1,323 @@
.#......#..####.....#..#.......
#.#...#...#..#.#...#.#...##.##.
#.#....#..........#...##.....##
#.#.#.....##......#.#.......###
..#..###....#.#....#.#.#..#....
.......#.#....##..##...#...#...
..#..#..#..###.......#.....#.#.
.#.......#...##...##.##......##
#.#.##..##.#..#....#..###..#.#.
#.....#.#.........#.....##.#.#.
..#.#....##..#...#...##........
......#....#..##.#.#......###..
.......#.......#......##...#...
.##.....#.......#...###.....##.
.#...#.##..##.#..##....#.......
..#......##...#..#...#.#.##.###
.##.##.....##....#..#......#.#.
.#.....#..###..#.##.#.....##.#.
......##..........#..........#.
.##....#.....#..##.#..#.#..###.
..##.......#....#...##...#..#..
.##...#.....#.###.#.#..#...#.#.
.....##.#.##..##...#...........
..#..###.##.#.#.###...###..#.#.
.#........#..#.#........#.#...#
....##.......#....#.#.##.#.....
....##........######..###..#.#.
#.#.#............#.......#..#..
...##...#.##.....#.#..#......#.
......#.##.#....##..#.#..###...
##.....#.#....#....#.##.#.###..
#..#..#..##.#..##.##.##.#.##...
.###.####..#..#........#.....##
.......##..#.......#...........
.##...#............#.#.##...#..
....##.....#...##..#..#.#..###.
...#.....#####.#..#...##....##.
#.....#.#.#....##.......##.#.#.
......#.#..#.##.#######......#.
#.##...##....#..###.#.......#..
.....##...#....#...#....##.##.#
....###......#...###..#......##
..#...##..##.######..#.#......#
......##....#....##..#......##.
.#...#..##..#.###.#......#....#
##....##..#..####.#.....#...#..
.#.......#...#.......##......#.
......#...#...#........#.......
.#........#.###...#..####.#..#.
##...#.#............#.....###..
.....###.#.##...........###..#.
.#.#...#.....#.#.##..##...####.
..##.......#..#.##.#....#.....#
.#..#.#..####.....###.#.....#..
..#..###.....####..#.##.#.#.##.
.###..#.....#......#...####....
...#.#..#.#..#...#...#....##.##
..###....#.##.....#..........#.
###...#####......##............
..###.....#........##.#...#..#.
..##.##.#.....##........##..#.#
##..#.#...#.#..#..###.#....#..#
....#..#.#.....#..#####...#....
....#.........#......##.##.....
.#...####.##......##..##.#..#.#
...#...#.##..#...##..###...#...
###...#.....#.##.###.###..#.#..
..#......#.###.....#..##.#...#.
#.....##.########...#####....#.
........##..#..##..##.#........
....#.######....##..#..#.##..#.
#.......#..##..#..#.#.#..##.##.
...#.#..#..#.......#......###.#
.#.#..#.#..#.##.#.............#
#....#.##.#.#.....#..#.#..#....
...###..#...#....#.........#.#.
.#..#.....##..#.#..#.#.......#.
..#...##...#......#......####..
....#..#.......#.......#.#..#..
#...#..#...........#.#..#.....#
#...#.#.......#...#....###....#
.#..#.#.##....#......#........#
..#...#..##..#..#..#..#...#.#..
..#.#.........#....#....##.....
##.....##.#.#.#.........##.....
.##...#.##...........#...#...##
.##..##.#.#..........##..##....
#....#....#.#...#.#..#....#.#..
####....##.....#..##.###.......
#..#....#......##.#.#....#.....
.....#....#.###.##.........###.
#.......#.####..#..#..##.......
##.#.......#..##..#....#..#.#..
..###...#.#...#.....##.##.####.
....#...#.#....#..#..#.....#.##
#.....##.#.#..#.##..#..##......
................###..#....##...
..#.##.....#..........##.#...#.
..#.#..#.#....#.#.#..#..#..#.#.
#...#..##.#.#...#..#...#..#....
#..#.#.........#..###........#.
.#...#.............#..###..#..#
#.........#.#..#...#.#.....#..#
....#..#..#.#.#...#...#.....##.
##...###.#.####..#......#...#..
..#..##...#.#......#.#.......#.
#......###....##.#.##..........
#####....###..#...............#
##.#...####....#....#...#....#.
.#.......#..#.....#...#.....###
...#..#.#.#....##......##...#..
...#.....#...#.##.#..#.#....#..
#...###....#...#.#....#........
.#.......#........#...##.##.##.
.....#....#...##.....##...###.#
....#....#.#..#...##.##.##.....
.......#............#...#.#..#.
.#............#.....##.......#.
........#....#....##......##.##
.......##..#.#..#.##..###..##.#
#..##..##.........####.#.###...
#....#..#...##...#.............
#...#...###..........##..#..#..
....#...#..#.....##...#........
#.....#......#.#.....#...#..#..
..#.....#.....#....#..#........
..#..#.....#.#.........#..###..
................###..#.#....#..
#.....#.....#.#.#.#.#..#...#.#.
#....#....#.#..........#.#....#
....#..#......#..##.#...##.....
..#.#...#.####....#.#..#.#..#..
.........##......#.....##......
##.#.###.#.....#.....####.#..#.
.....#.....#..#....#..###.#....
##..#.#...#.##....#....#.......
.....#......#.#...##..#.#......
....##..#...#...##..##.#....#.#
............#..........##.#....
##..#..#.##..##..#.#....#.#.#..
.......#.#...#...#.#...#..#....
#....#.#...#...#........#..#...
...........#.......#...##..###.
.#..##......#.##.........##..#.
...#...#...###.#.##....##.#..#.
#...#..#.#.#.....##..#.......#.
.##..#.###.##......#.#....#.#.#
..#....#.......#..#..#.#.#.##..
#...#...###...###.........#....
.#.#...#.....##.#.#..#....#.##.
.........#.#.##.....#.#.###....
...#.#...#......#...####......#
...##..##....##......##...###..
###...#..#.......##.....#....#.
...#..#..#..###...##.##..#..#..
...#......#......##..#.#.##..#.
...#.........#....#.#....#.#...
##................#..#.#.....#.
....#.##...#..#.##...##.#.....#
......#..##.##..###.#..#.##.##.
.#.#...###.....###.....##...###
.##.....#.#.#..#..###..#..#..#.
#.......#..#..#....##.....#....
...#.#.##..#..#......##.##...#.
....##.#......#...#..#..#......
.####.#..#.....#..##.#...##..##
..#..#...#..........###..#....#
.#.#.##.##...#............#....
........##..##......#.##..#.###
...#.#....###......##.......#..
..##...#...#.#..#.....#.....#..
##..#...###..#..#.#.#...#...#..
.....#..#....##.....##.....###.
....##...###.#..#.#....##..#..#
#......#...#....#......#...##..
....#.##...#.#......#.#.##...#.
.......#.....#...#####...#.#...
...#.....##.#............#.....
...#.#........#.#.#..#.........
....###......#.#.#..#.####.#..#
#.....#.#.#.....#.#.#.....#..#.
..##.##......#...#.#...........
###..###....#.#####......###...
..##..............##.#.#....#.#
#..#...#..........#..#.#.#..###
##.###............#....#.#...#.
#.#..#.#..##.#.#....#...#......
#....#...#..##.....#..#.#..###.
..#.....#.#....#.#..#.##.#..##.
...##...#.#.##...#....###....#.
......###.####.......#..#.#.#.#
.#..............##........#....
...##.##...##....#..#.......#..
.....#.....#....###...#..#..#.#
.#.....#..#.....#......#.....##
#.#.##.#..#..#.....#.##..###...
..#......#...##.###..#.#...#..#
......#.....#...##......#......
##.#........#..........#.....#.
#........##.#............##....
...#......##...#.#.....##......
...##.......#....#.#..#.#.###..
..#....##..##.##.....###....#..
..#...#.#...#.....#..........#.
......#...#...#.#.##.#...#.#.#.
.#...#......#.##........#......
.##.##..#....#...#.#...##......
#..#......#.#...........#....#.
....##.#....#...#..#....#.#..##
#....##.##....#.#..##.#........
.##.##.#....##.....#..#....#..#
...#...#.....###.#.##..........
....#...#....##.......###......
#.........#......#.#.......#...
#..........#..##..#.#..........
.....#.......#..##.##....##...#
........................#.#....
#..#.........#.............#..#
#..#.....#.......#....#....#.#.
..##..##.......##....#...#.....
.##......#..##......#.###......
...#.#........#.......##..###..
..##...###.###......#...#....##
#...#...#.....###.#.#.#..#.....
#....#.........#..##...#...##..
#..###..#.#.#.##.#..#.#....#.##
#...#.#.....#.###.#.......#....
..##..#..#....#.#...........#.#
#.........#.#......#...##......
.######......#..#....#.#.#....#
##..#.#..####.###.........#....
###########.....##.##...#..#...
#...##.#.#....#.#....#......#..
...#..##..#..##..#......#....#.
.#....#...#....#.#..##....##...
#..#.#............#....#.#...#.
...#...#..#.#.##......#..#.#...
#.#...##.....#..#.##......####.
.#.#..##..#.....#.#..#.##......
#.#.##......##.....#..#.#..#...
#..##...#.##.#.......#.##......
..#.......#.#.#...##..##...#...
.#...#..#..#.#.........#..##...
#..#.......#....#.#...#.###...#
.......#..#.......##.#.#...#.#.
.#.................###.#..###..
..........#.#.....##..#####...#
#......#.#..##.#.#...#.##.#....
#......#.#..##.##.#...#....#...
....#..#......#....#....#######
.#...#......#....###......#.###
#.#....#.#...#.###......#..#..#
.###......#.#...#.####.#..####.
######.#.....###.#...#.#.....#.
.#.###....#..#.#.....#.....####
.......###.#.........#..#......
#...#.....##.#......####.......
..#.#..##.#.#...#...#..##..##..
.....#...##.....#...##......##.
##..#..#.##..#.#......#.....#..
##.........#.#.##.#..#.#....#.#
.#........###...#.........#....
...#..#.#..#....####...........
#.#....#..##..####.#...#.##....
.#.....#.......#..........#..##
...#.......#...###..#.....#..##
.........#.###.#..##...#.##...#
.#..........##..####...#..#.#.#
.#...##...#............##...#.#
...#....#.#..........#.#..#.#..
.#.#...##....##.#.#.#....#.....
....#..#.....#.#..#.#..#.##.###
.....#.#.....#..#......#.#.#...
.....#.#.#..###..#.#..###...#..
#.......####...#.#..#......##.#
....#..#..###......###.##....#.
##.....#.....#.............#..#
#..#..#...##.....##..#..#.#....
.....#.#.###...#...............
#.#.#.....#.#..#.#...#.......#.
..##.##............#....#..##..
#....##...#.....#.###...#.#....
#...##.#.........#...#....#....
##.##.#...#.#...###..#....##..#
....#....##..#..#.......#...##.
.#...#...#..#.....#..###.#..#.#
....#..###......#....##....#...
#.#.....#....##.#..#.#...###...
.......#............#......#...
.##..#.###.#.............###...
..##...##.#.#.#.....#........##
....#.###....#..#..#...#...#..#
.....#...#...#..#....#.....##..
###.#.#.....#......####.....#..
#.#.###............#......#....
..#.....#..#..#..#....#......#.
#...######...#....#.##...##.#.#
##.#.#.#..##......##.#..#.#...#
............#.#..#.##....#.....
......#............#.#...#..#.#
.#..##...##..#.#.#..###.....##.
#.###.#...........#...#....#...
....##.....#...##...#...###.#.#
.####.#.#.....#.#..#.#.##......
.#...##......###...#..##..#.#..
.#......#...#....##.....##..#..
..........##.....###.##.#...#.#
.#........##.#..............#..
#...###..#...#.....#....#.....#
...#......#..#...#...#..###.#..
.#...##..#........#.......#.#..
.#.#.##.........##.##......#..#
#...#.#.#...#.....#.#...#.#..#.
#.#..#...#...#...##..........#.
.#...........#....#..#.#..#.#..
#.......#......#..#...#........
.....#..#...##..###..##........
......#...#.....#..#.#.#....##.
....##..##..##....###.##.......
.#........##.#.#...#..#........
.....##...##...#......#..#...#.
..#.....#....###.#..##....#..#.
......#..#...####.#.....##.####

@ -0,0 +1,35 @@
#lang br
(require racket/file rackunit)
(define passports (for/list ([pstr (string-split (file->string "04.rktd") #rx"\n\n+")])
(for/hash ([kvstr (string-split pstr)])
(apply values (string-split kvstr ":")))))
(define required-fields '("byr" "iyr" "eyr" "hgt" "hcl" "ecl" "pid")) ; "cid" is not required
(define (passport-keys-valid? p)
(for*/and ([passport-keys (in-value (hash-keys p))]
[field required-fields])
(member field passport-keys)))
(check-equal? (count passport-keys-valid? passports) 202)
(define (stringval-in-range str low high)
(<= low (string->number str) high))
(define (passport-values-valid? p)
(and
(stringval-in-range (hash-ref p "byr") 1920 2002)
(stringval-in-range (hash-ref p "iyr") 2010 2020)
(stringval-in-range (hash-ref p "eyr") 2020 2030)
(let ([hgt (hash-ref p "hgt")])
(cond
[(string-suffix? hgt "cm") (stringval-in-range (string-trim hgt "cm") 150 193)]
[(string-suffix? hgt "in") (stringval-in-range (string-trim hgt "in") 59 76)]
[else #false]))
(let ([hcl (hash-ref p "hcl")])
(and (regexp-match #rx"#......" hcl) (string->number (string-trim hcl "#") 16)))
(member (hash-ref p "ecl") '("amb" "blu" "brn" "gry" "grn" "hzl" "oth"))
(let ([pid (hash-ref p "pid")]) (and (= 9 (string-length pid)) (string->number pid)))))
(check-equal? (count (λ (p) (and (passport-keys-valid? p) (passport-values-valid? p))) passports) 137)

File diff suppressed because it is too large Load Diff