Skip to content

Commit f8977b2

Browse files
committed
Add day 20
1 parent c8d55dd commit f8977b2

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

2021/20/20.jl

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
1-
function main()
2-
asbin(s) = Int.(collect(s) .== '#') |> join
3-
lookup = readline() |> asbin
4-
readline()
5-
lines = readlines() .|> asbin
6-
default = '0'
7-
for iteration in 1:50
8-
sy, sx = length(lines), length(lines[1])
9-
atmap(y, x) = (y 1:sy && x 1:sx) ? lines[y][x] : default
10-
expanded = []
11-
for Y = 0:sy+1
12-
push!(expanded, [])
13-
for X = 0:sx+1
14-
index = parse(Int, join(atmap(Y+y, X+x) for x=-1:1, y=-1:1), base=2)
15-
push!(expanded[end], lookup[index + 1])
16-
end
17-
end
18-
lines = expanded
19-
default = (default == '0') ? lookup[1] : lookup[end]
20-
iteration [2, 50] && println(lines .|> (line -> line .== '1') |> sum |> sum)
21-
end
1+
lookup, _, lines... = readlines() .|> s -> (Int.(collect(s) .== '#') |> join)
2+
default = '0'
3+
for iteration in 1:50
4+
sy, sx = length(lines), length(lines[1])
5+
atmap(y, x) = (y 1:sy && x 1:sx) ? lines[y][x] : default
6+
get_index(Y, X) = parse(Int, join(atmap(Y+y, X+x) for x=-1:1, y=-1:1), base=2) + 1
7+
global lines = [[lookup[get_index(Y, X)] for X=0:sx+1] for Y=0:sy+1]
8+
global default = (default == '0') ? lookup[1] : lookup[end]
9+
iteration [2, 50] && println(lines .|> (line -> line .== '1') |> sum |> sum)
2210
end
23-
main()

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# My Advent of Code Solutions
22

3-
The solutions are meant to be ran with the `program-tester.sh` script (see [here](https://github.com/LiquidFun/misc-scripts)), which runs the program on all `*.in` files in the directory, and tests whether the corresponding `*.ans` file matches the given output. As of 2021, each program outputs both part 1 and part 2 on separate lines.
3+
All solutions expect the input via stdin, which can be easily achieved by piping the input to the program: `julia 1.jl < input.in`. I use the `program-tester.sh` script (see [here](https://github.com/LiquidFun/misc-scripts)), which runs the given program on all `*.in` files in the directory, and tests whether the corresponding `*.ans` file matches the given output. Each program outputs both part 1 and part 2 on separate lines. To validate a program manually, type `diff <(julia 1.jl < input.in) input.ans`.
44

55
Programs are initialized with the `init-day.sh` script. I.e. typing `init-day.sh 10` initializes the 10th day by creating a folder named `10`, downloading the input test case with the `session.cookie`, copying the `dummy.py` file and opening the solution file.
66

7-
* 2021: A mix of Julia, Python and APL in that order. Trying out Julia and APL for the first time.
7+
* 2021: There is a solution for each problem in Julia. Sometimes there is a Python or APL solution as well. I'm trying out Julia for the first time, mostly focusing on short and elegant code.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy