knitr::opts_chunk$set(echo = TRUE) # Benchmark code chunks knitr::knit_hooks$set( time = function(before, envir) { t <- proc.time()['elapsed'] if(before) { envir$.elapsed <- t } else { paste(signif(t - envir$.elapsed), 'sec.') } } ) library(JuliaCall)
```{julia setup, include=FALSE} using Pkg Pkg.activate("2020/julia") Pkg.instantiate()
```{julia libraries, message=FALSE} using DelimitedFiles using Combinatorics using Formatting
```{julia day1-1, code=readLines("2020/julia/src/1-1.jl"), time = TRUE}
#### Notes * **Array comprehension** has the following syntax: ```{julia} [x^2 for x = 1:3]
```{julia day1-2, code=readLines("2020/julia/src/1-2.jl"), time = TRUE}
#### Notes * **Conditional comprehension** has the following syntax: ```{julia} [x^2 for x = 1:3 if x > 1]
```{julia day2-1, code=readLines("2020/julia/src/2-1.jl"), time = TRUE}
#### Notes * The **ternary operator**, `?:` is related to `if-elseif-else` syntax and is written as `condition ? value_if_true : value_if_false`. * Julia complains of scoping issues when calling `results` += 1` inside a `for` loop (if `results` was defined outside of it). Rather than using `global results += 1`, putting everything into a function will achieve the expected behaviour. ### Part 2 ```{julia day2-2, code=readLines("2020/julia/src/2-2.jl"), time = TRUE}
```{julia day3-1, code=readLines("2020/julia/src/3-1.jl"), time = TRUE}
### Part 2 ```{julia day3-2, code=readLines("2020/julia/src/3-2.jl"), time = TRUE}
```{julia day4-1, code=readLines("2020/julia/src/4-1.jl"), time = TRUE}
### Part 2 ```{julia day4-2, code=readLines("2020/julia/src/4-2.jl"), time = TRUE}
```{julia day5-1, code=readLines("2020/julia/src/5-1.jl"), time = TRUE}
### Part 2 `\in` and `\notin`, then TAB: ```{julia day5-2, code=readLines("2020/julia/src/5-2.jl"), time = TRUE}
```{julia day6-1, code=readLines("2020/julia/src/6-1.jl"), time = TRUE}
## Day 7: [Handy Haversacks](https://adventofcode.com/2020/day/7) ### Part 1 ```{julia day7-1}
```{julia day8-1, code=readLines("2020/julia/src/8-1.jl"), time = TRUE}
### Part 2 ```{julia day8-2, code=readLines("2020/julia/src/8-2.jl"), time = TRUE}
```{julia day9-1, code=readLines("2020/julia/src/9-1.jl"), time = TRUE}
### Part 2 ```{julia day9-2, code=readLines("2020/julia/src/9-2.jl"), time = TRUE}
{julia day10-1, code=readLines("2020/julia/src/10-1.jl"), time = TRUE}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.