aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2023/05
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2024-01-27 14:02:35 +0800
committerMistivia <i@mistivia.com>2024-01-27 14:02:35 +0800
commit6580dcd9127f69aaa794472ec92bc46015dc4019 (patch)
treedc2c7e102c75180f7bd98c2f3a14f8b55f83c0f2 /advent-of-code/2023/05
init
Diffstat (limited to 'advent-of-code/2023/05')
-rw-r--r--advent-of-code/2023/05/input211
-rwxr-xr-xadvent-of-code/2023/05/part1.scm75
-rwxr-xr-xadvent-of-code/2023/05/part2.scm112
3 files changed, 398 insertions, 0 deletions
diff --git a/advent-of-code/2023/05/input b/advent-of-code/2023/05/input
new file mode 100644
index 0000000..2e51c69
--- /dev/null
+++ b/advent-of-code/2023/05/input
@@ -0,0 +1,211 @@
+seeds: 3416930225 56865175 4245248379 7142355 1808166864 294882110 863761171 233338109 4114335326 67911591 1198254212 504239157 3491380151 178996923 3965970270 15230597 2461206486 133606394 2313929258 84595688
+
+seed-to-soil map:
+3534435790 4123267198 50004089
+3584439879 3602712894 238659237
+2263758314 0 160870825
+2971481857 2850687195 31776688
+4173604159 3353763588 121363137
+3823099116 3003258545 350505043
+2850687195 2882463883 120794662
+1503174517 2076905328 347723811
+1850898328 195477286 412859986
+1265521310 1606247567 17062682
+3285153612 4173271287 121696009
+488201540 828927797 777319770
+453595079 160870825 34606461
+3406849621 3475126725 127586169
+1282583992 608337272 220590525
+3003258545 3841372131 281895067
+0 1623310249 453595079
+
+soil-to-fertilizer map:
+131427930 1185330183 180485664
+748806267 2475960003 160820884
+311913594 3858074623 436892673
+3738185633 2255483282 220476721
+909627151 2636780887 1221293736
+2848518198 1365815847 889667435
+2130920887 131427930 666553095
+2797473982 1134285967 51044216
+3958662354 797981025 336304942
+
+fertilizer-to-water map:
+318410581 1095359367 168721315
+1850530626 4267113166 11515024
+1868157768 2129267011 114327162
+3662276437 4191001581 22313216
+2980811924 3765310336 180818294
+3971289879 3991326516 15449292
+4217905563 2279561459 35118050
+2287003279 4213314797 47187938
+1837473204 2314679509 13057422
+1998824036 3946128630 45197886
+222462505 1264080682 95948076
+487131896 791770420 303588947
+4253023613 4068573955 41943683
+2334191217 3252696905 278223677
+3986739171 2786104784 83568826
+3495640623 3600917310 61383314
+3557023937 3662300624 15042395
+2222048360 3535962391 64954919
+2919013777 4006775808 61798147
+3660033649 4182646675 2242788
+3266573705 2327736931 229066918
+3684589653 2869673610 235425729
+3261531896 3530920582 5041809
+790720843 417781090 105163807
+895884650 0 159434487
+2044021922 2608078346 178026438
+1862045650 4184889463 6112118
+3572066332 3677343019 87967317
+1830862773 4260502735 6610431
+1338009275 159434487 258346603
+3233759255 1830862773 27772641
+1055319137 1360028758 282690138
+1982484930 4278628190 16339106
+2883046491 2243594173 35967286
+2612414894 1858635414 270631597
+4070307997 3105099339 147597566
+3161630218 4110517638 72129037
+1596355878 522944897 46363018
+0 569307915 222462505
+3920015382 2556803849 51274497
+
+water-to-light map:
+3185219492 1324735395 185266775
+3146586681 1249776213 38632811
+28244350 428471809 312696716
+340941066 3650819202 117391304
+458332370 963661621 286114592
+2785969088 3483794777 117695215
+1279106820 1583617352 194624401
+1473731221 3601489992 49329210
+0 2796264154 28244350
+744446962 3356330358 127464419
+2562107674 223281414 205190395
+3544929092 0 223281414
+2767298069 944990602 18671019
+2903664303 775683406 169307196
+3072971499 1510002170 73615182
+1140236238 741168525 34514881
+1242780449 1288409024 36326371
+1523060431 1778241753 949993071
+871911381 3088005501 268324857
+3370486267 2913562676 174442825
+4096117687 4180659844 114307452
+1174751119 2728234824 68029330
+4210425139 4096117687 84542157
+2473053502 2824508504 89054172
+
+light-to-temperature map:
+57304962 1726059676 351776583
+1567802332 965133212 510033927
+3296678005 3095070487 435408435
+2476702913 3609026358 293401880
+1363411758 0 204390574
+1017340727 204390574 346071031
+2770104793 2148583721 409359530
+994650876 1475167139 22689851
+0 550461605 20445762
+20445762 1692752937 33306739
+2144982382 2092188566 56395155
+799754929 1497856990 194895947
+53752501 961580751 3552461
+409081545 570907367 390673384
+2201377537 3902428238 275325376
+3988154754 2557943251 306812542
+3179464323 4177753614 117213682
+3732086440 3530478922 78547436
+3810633876 2864755793 177520878
+2092188566 3042276671 52793816
+
+temperature-to-humidity map:
+18928354 3414191527 36074961
+3774151818 3588716061 144651966
+2046448856 1384376044 7569690
+2737178317 903028814 27883660
+2981004508 930912474 349046239
+1609626976 3084565214 120015958
+2765061977 2248931514 215942531
+157942811 3887783726 359543023
+3330050747 2195105292 25002873
+2041971091 78356652 4477765
+1938796124 861459710 41569104
+1608860129 2474627144 766847
+1812604664 3795542966 74143188
+4196563819 3386335999 27024746
+1980365228 3204581172 61605863
+1136292153 82834417 94266783
+4015908448 1279958713 104417331
+2701246339 2062523244 35931978
+4223588565 3286123619 23738184
+91521115 3309861803 56668597
+1886747852 2552562071 52048272
+1729642934 0 61644451
+1791287385 1504576228 21317279
+517485834 1726582276 261020280
+2192468119 2604610343 479954871
+1413642666 1391945734 112630494
+71715516 3366530400 19805599
+3918803784 2475393991 77168080
+1546685190 3733368027 62174939
+2672422990 2220108165 28823349
+148189712 2464874045 9753099
+18097572 3413360745 830782
+0 3869686154 18097572
+3995971864 3266187035 19936584
+853426802 432206724 27759827
+2054018546 3450266488 138449573
+881186629 177101200 255105524
+778506114 1987602556 74920688
+1526273160 2098455222 20412030
+55003315 61644451 16712201
+3355053620 459966551 401493159
+4120325779 2118867252 76238040
+1230558936 1525893507 183083730
+3756546779 1708977237 17605039
+
+humidity-to-location map:
+166973311 827072705 21017988
+2420657564 1797988486 98294592
+3711057743 1724407580 50543704
+151140540 331672683 15832771
+2611368935 1098267426 35837870
+329110209 0 39413233
+848395356 3293184115 2008343
+850403699 1508109908 216297672
+3203490893 3278069000 15115115
+1322210923 4179894531 115072765
+3761601447 848395356 249872070
+230668724 39413233 27530262
+1437283688 2240143464 113813014
+1551096702 2957294790 77256502
+716039283 695021295 132051410
+258198986 109620920 70911223
+1314022212 2357817442 8188711
+2793895840 2474472017 281155944
+385486933 347505454 330552350
+3218606008 3561131747 206517169
+1066701371 3354107909 156810008
+4011473517 1134105296 283493779
+0 180532143 151140540
+368523442 678057804 16963491
+1223511379 1417599075 90510833
+3183517648 1988699857 19973245
+2270463718 4038012896 141881635
+1817187310 1774951284 23037202
+1963899457 2755627961 201666829
+2412345353 3295192458 8312211
+1840224512 3767648916 123674945
+187991299 66943495 42677425
+2518952156 1896283078 92416779
+1628353204 3034551292 138620276
+3075051784 2366006153 108465864
+3660454503 3303504669 50603240
+3425123177 2008673102 231470362
+3656593539 2353956478 3860964
+1766973480 3510917917 50213830
+2647206805 3891323861 146689035
+2165566286 3173171568 104897432
+
diff --git a/advent-of-code/2023/05/part1.scm b/advent-of-code/2023/05/part1.scm
new file mode 100755
index 0000000..5413bf0
--- /dev/null
+++ b/advent-of-code/2023/05/part1.scm
@@ -0,0 +1,75 @@
+#!/usr/bin/env guile
+!#
+
+(use-modules (ice-9 rdelim))
+
+(define port (open-input-file "input"))
+
+(define seed
+ (let ()
+ (define nums-str
+ (string-trim
+ (list-ref (string-split (read-line port) #\:) 1)))
+ (map string->number (string-split nums-str #\space))))
+
+(read-line port)
+
+(define (read-line-convert-eof port)
+ (define line (read-line port))
+ (if (eof-object? line) "" line))
+
+(define (read-map)
+ (define (loop ret)
+ (define line (string-trim (read-line-convert-eof port)))
+ (if (= 0 (string-length line))
+ (reverse ret)
+ (loop (cons (map string->number (string-split line #\space))
+ ret))))
+ (read-line port)
+ (loop '()))
+
+(define s2s (read-map))
+(define s2f (read-map))
+(define f2w (read-map))
+(define w2l (read-map))
+(define l2t (read-map))
+(define t2h (read-map))
+(define h2l (read-map))
+(define maps (list s2s s2f f2w w2l l2t t2h h2l))
+
+
+(define (gen-mapper the-map)
+ (define (mapper x)
+ (define (loop the-map)
+ (if (nil? the-map)
+ x
+ (let ()
+ (define cur-map (car the-map))
+ (define target (car cur-map))
+ (define start (cadr cur-map))
+ (define len (caddr cur-map))
+ (if (and (>= x start)
+ (<= x (+ start len)))
+ (+ target (- x start))
+ (loop (cdr the-map))))))
+ (loop the-map))
+ mapper)
+
+(define mappers (map gen-mapper maps))
+
+
+(define (comp-func funcs)
+ (define procs (reverse funcs))
+ (define (comp-rec arg)
+ (if (null? procs)
+ arg
+ (let ((proc (car procs))
+ (rest (cdr procs)))
+ (set! procs rest)
+ (proc (comp-rec arg)))))
+ comp-rec)
+
+(define (find-location x)
+ ((comp-func mappers) x))
+
+(display (apply min (map find-location seed)))
diff --git a/advent-of-code/2023/05/part2.scm b/advent-of-code/2023/05/part2.scm
new file mode 100755
index 0000000..87ad5e9
--- /dev/null
+++ b/advent-of-code/2023/05/part2.scm
@@ -0,0 +1,112 @@
+#!/usr/bin/env guile
+!#
+
+(use-modules (ice-9 rdelim))
+
+(define port (open-input-file "input"))
+
+(define seed
+ (let ()
+ (define nums-str
+ (string-trim
+ (list-ref (string-split (read-line port) #\:) 1)))
+ (define (pairing l)
+ (define (loop ret l)
+ (if (null? l)
+ ret
+ (loop
+ (cons (cons
+ (car l)
+ (+ (car l) (cadr l)))
+ ret)
+ (cddr l))))
+ (loop '() l))
+ (reverse
+ (pairing (map string->number (string-split nums-str #\space))))))
+
+(read-line port)
+
+(define (read-line-convert-eof port)
+ (define line (read-line port))
+ (if (eof-object? line) "" line))
+
+(define (read-map)
+ (define (loop ret)
+ (define line (string-trim (read-line-convert-eof port)))
+ (if (= 0 (string-length line))
+ (sort (reverse ret) (lambda (x y) (< (cadr x) (cadr y))))
+ (loop (cons (map string->number (string-split line #\space))
+ ret))))
+ (read-line port)
+ (loop '()))
+
+(define s2s (read-map))
+(define s2f (read-map))
+(define f2w (read-map))
+(define w2l (read-map))
+(define l2t (read-map))
+(define t2h (read-map))
+(define h2l (read-map))
+(define maps (list s2s s2f f2w w2l l2t t2h h2l))
+
+(define (range-map mlist r)
+ ;; r :: Range :: (start . end)
+ ;; mlist :: List of (Map :: (dest, src, len))
+ (define (loop result mlist r)
+ (define start (car r))
+ (define end (cdr r))
+ (if (null? mlist)
+ (cons r result)
+ (let ()
+ (define cur-map (car mlist))
+ (define map-target (car cur-map))
+ (define map-start (cadr cur-map))
+ (define map-end (+ map-start (caddr cur-map)))
+ (define offset (- map-target map-start))
+ (define (pair-offset p) (cons (+ offset (car p)) (+ offset (cdr p))))
+ (cond ((<= end start)
+ result)
+ ((>= start map-end)
+ (loop result (cdr mlist) r))
+ ((>= start map-start)
+ (loop
+ (cons (pair-offset (cons start (min end map-end)))
+ result)
+ (cdr mlist)
+ (cons (min end map-end) end)))
+ ((<= end map-start)
+ (cons r result))
+ ((< start map-start)
+ (loop
+ (cons (cons start map-start) result)
+ mlist
+ (cons map-start end)))
+ (else (error "unhandled cond in range-map"))))))
+ (reverse (loop '() mlist r)))
+
+(define (gen-range-mapper the-map)
+ (define (mapper range-list)
+ (apply append
+ (map
+ (lambda (range)
+ (range-map the-map range))
+ range-list)))
+ mapper)
+
+(define mappers (map gen-range-mapper maps))
+
+(define (comp-func funcs)
+ (define procs (reverse funcs))
+ (define (comp-rec arg)
+ (if (null? procs)
+ arg
+ (let ((proc (car procs))
+ (rest (cdr procs)))
+ (set! procs rest)
+ (proc (comp-rec arg)))))
+ comp-rec)
+
+(define (find-location x)
+ ((comp-func mappers) x))
+
+(display (apply min (map car (find-location seed))))