Advent of Code solutions

2021 day 20

+116
+55
2021/20/p1.tri
···
··· 1 + import "trilogy:debug" use dbg 2 + import "trilogy:io" use readline, readall 3 + import "trilogy:parsec" use parse, apply, repeat, char_of, per_line, many_1 4 + import "trilogy:array" as array use map 5 + import "trilogy:bits" as bits 6 + import "trilogy:number" as number 7 + import "trilogy:core" use length 8 + import "trilogy:iterator" as it 9 + 10 + func infinite_image true cb = with cb!() { when 'outside resume 0bb111 } 11 + func infinite_image false cb = with cb!() { when 'outside resume 0bb000 } 12 + 13 + proc read_at!(image, row, col) { 14 + let outside = yield 'outside 15 + let unpadded = with image.row { when 'mia { return outside } } 16 + let padded = bits::concat outside <| bits::concat unpadded outside 17 + return (padded <~ (col + 3)) ~>> (length padded - 3) 18 + } 19 + 20 + func read_around image row col = { 21 + let a = read_at!(image, row - 1, col - 1) 22 + let b = read_at!(image, row, col - 1) 23 + let c = read_at!(image, row + 1, col - 1) 24 + number::from (a | b ~~> 3 | c ~~> 6) 25 + } 26 + 27 + func enhance_row enhancement image row = 28 + it::range (-1) (length (image.0)) 29 + |> it::map (read_around image row) 30 + |> it::map ((.) enhancement) 31 + |> bits::collect 32 + 33 + func enhance outside enhancement image = { 34 + using infinite_image outside 35 + let enhanced = it::range (-1) (length image) 36 + |> it::map (enhance_row enhancement image) 37 + |> array::collect 38 + let new_outside = enhancement.(read_around image (-3) (-3)) 39 + new_outside:enhanced 40 + } 41 + 42 + proc main!() { 43 + let enhancement = readline!() 44 + |> parse (repeat 512 (char_of ".#")) 45 + |> map ((==) '#') 46 + |> bits::from_array 47 + let "\n" = readline!() 48 + let image = readall!() 49 + |> parse (per_line (many_1 (char_of ".#"))) 50 + |> map (bits::from_array << map ((==) '#')) 51 + let outside:enhanced = enhance false enhancement image 52 + let _:enhanced_2 = enhance outside enhancement enhanced 53 + dbg!(array::reduce (+) <| map bits::pop_count enhanced_2) 54 + exit 0 55 + }
+61
2021/20/p2.tri
···
··· 1 + import "trilogy:debug" use dbg 2 + import "trilogy:io" use readline, readall 3 + import "trilogy:parsec" use parse, apply, repeat, char_of, per_line, many_1 4 + import "trilogy:array" as array use map 5 + import "trilogy:bits" as bits 6 + import "trilogy:number" as number 7 + import "trilogy:core" use length 8 + import "trilogy:iterator" as it 9 + 10 + func infinite_image true cb = with cb!() { when 'outside resume 0bb111 } 11 + func infinite_image false cb = with cb!() { when 'outside resume 0bb000 } 12 + 13 + proc read_at!(image, row, col) { 14 + let outside = yield 'outside 15 + let unpadded = with image.row { when 'mia { return outside } } 16 + let padded = bits::concat outside <| bits::concat unpadded outside 17 + return (padded <~ (col + 3)) ~>> (length padded - 3) 18 + } 19 + 20 + func read_around image row col = { 21 + let a = read_at!(image, row - 1, col - 1) 22 + let b = read_at!(image, row, col - 1) 23 + let c = read_at!(image, row + 1, col - 1) 24 + number::from (a | b ~~> 3 | c ~~> 6) 25 + } 26 + 27 + func enhance_row enhancement image row = 28 + it::range (-1) (length (image.0)) 29 + |> it::map (read_around image row) 30 + |> it::map ((.) enhancement) 31 + |> bits::collect 32 + 33 + func enhance outside enhancement image = { 34 + using infinite_image outside 35 + let enhanced = it::range (-1) (length image) 36 + |> it::map (enhance_row enhancement image) 37 + |> array::collect 38 + let new_outside = enhancement.(read_around image (-3) (-3)) 39 + new_outside:enhanced 40 + } 41 + 42 + proc main!() { 43 + let enhancement = readline!() 44 + |> parse (repeat 512 (char_of ".#")) 45 + |> map ((==) '#') 46 + |> bits::from_array 47 + let "\n" = readline!() 48 + let mut image = readall!() 49 + |> parse (per_line (many_1 (char_of ".#"))) 50 + |> map (bits::from_array << map ((==) '#')) 51 + let mut outside = false 52 + let mut i = 0 53 + while i < 50 { 54 + let outside_2:image_2 = enhance outside enhancement image 55 + outside = outside_2 56 + image = image_2 57 + i += 1 58 + } 59 + dbg!(array::reduce (+) <| map bits::pop_count image) 60 + exit 0 61 + }