Skip to content

Commit

Permalink
ehhh
Browse files Browse the repository at this point in the history
  • Loading branch information
Moelf committed Dec 4, 2023
1 parent da8f713 commit cbff437
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
**/*.o
**/Manifest.toml
bazel-*
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = "AoC2023"
uuid = "6a329b9a-879f-4671-b49f-bf1b928a466a"
authors = ["all authors"]
version = "0.1.0"
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,16 @@ AoC2023/
- the input files should be named as `<day>_<name>.txt`, the content should be the input of the day without any dangling empty lines at the end
- the solutions files should be named exactly as corresponding input file, the content should be exactly one or two lines (depending on if you solved both parts)
- the `src/<lang>/<day>_<optional name>.<ext>` should matched the double-digit day number convention. The `<lang>` should match what appears in [`runtests.jl`](https://github.com/Moelf/AoC2023/blob/main/runtests.jl#L18-L28)

## Note for Julia
Each `<day>_julia.jl` script should use a `main()` and `@__FILE__` pattern similar to:
```julia
function main(path)
println("hello world")
println("solution 2")
end

(abspath(PROGRAM_FILE) == @__FILE__) && main(ARGS[1])
```

this way we can put each script int a `DayN` module and compile only once per day.
9 changes: 8 additions & 1 deletion runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Test
using AoC2023

const ALL_LANGUAGES = isempty(ARGS) ? readdir("src") : ARGS
const INPUTS = readdir("./inputs")
Expand All @@ -22,7 +23,13 @@ function run_solution(source_path, input_path, ::Val{:cpp})
end

function run_solution(source_path, input_path, ::Val{:julia})
return readlines(`julia $source_path $input_path`)
original_stdout = stdout
rd, wr = redirect_stdout()
AoC2023.DAY_MODULES[source_path].main(input_path)
close(wr)
redirect_stdout(original_stdout)
result = readlines(rd)
return result
end

function run_solution(source_path, input_path, ::Val{:python})
Expand Down
12 changes: 12 additions & 0 deletions src/AoC2023.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module AoC2023

const DAY_MODULES = Dict{String, Module}()
function __init__()
files = readdir(joinpath(@__DIR__, "julia"); join=true)
for path in files
m = get!(DAY_MODULES, path, Module())
Base.include(m, path)
end
end

end
8 changes: 6 additions & 2 deletions src/julia/00_test.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
println("hello world")
println("solution 2")
function main(path)
println("hello world")
println("solution 2")
end

(abspath(PROGRAM_FILE) == @__FILE__) && main(ARGS[1])
21 changes: 9 additions & 12 deletions src/julia/01_bauerc_moelf.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
const INPUT_PATH = ARGS[1]

# Part I
function getnumber(line)
i = findfirst(isdigit, line)
j = findlast(isdigit, line)
return parse(Int, string(line[i], line[j]))
end

function part1()
lines = readlines(INPUT_PATH)
println(sum(getnumber, lines))
end
part1(lines) = sum(getnumber, lines)

part1() # Answer: 55123

# Part II
const lookup = Dict(
"one" => 1,
"two" => 2,
Expand All @@ -28,7 +23,6 @@ const lookup = Dict(

const RE = Regex("$(join(keys(lookup), '|'))|\\d")

# Part II
function get_num_from_string(str)
dig = tryparse(Int, str)
!isnothing(dig) && return dig
Expand All @@ -44,9 +38,12 @@ function getnumber_part2(line)
return num_first * 10 + num_last
end

function part2()
lines = readlines(INPUT_PATH)
println(sum(getnumber_part2, lines))
part2(lines) = sum(getnumber_part2, lines)

function main(path)
lines = readlines(path)
println(part1(lines))
println(part2(lines))
end

part2() # Answer: 55260
(abspath(PROGRAM_FILE) == @__FILE__) && main(ARGS[1])
16 changes: 9 additions & 7 deletions src/julia/02_moelf.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
const INPUT_PATH = ARGS[1]
const ALL_LINES = readlines(INPUT_PATH)

function color_dict(a::RegexMatch)
num, color = a.captures
Dict(color => parse(Int, num))
Expand All @@ -18,10 +15,15 @@ function part1_pred(line)
return dict["red"] <= 12 && dict["green"] <= 13 && dict["blue"] <= 14
end

p1 = sum(findall(part1_pred, ALL_LINES))
println(p1)

# Part II
part2(line) = line |> line_max |> values |> prod
p2 = sum(part2, ALL_LINES)
println(p2)

function main(path)
lines = readlines(path)
p1 = sum(findall(part1_pred, lines))
println(p1)
println(sum(part2, lines))
end

(abspath(PROGRAM_FILE) == @__FILE__) && main(ARGS[1])
13 changes: 8 additions & 5 deletions src/julia/03_moelf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ function seek_and_accumulate(schematics; seeder, reducer)
return s
end

const SCHEMATICS = stack(readlines(ARGS[1]); dims=1)

# Part 1
issymbol(c) = !isdigit(c) && c != '.'
p1 = seek_and_accumulate(SCHEMATICS; seeder = issymbol, reducer = sum)
println(p1)

# Part 2
p2_reducer(nums) = length(nums) == 2 ? prod(nums) : 0
p2 = seek_and_accumulate(SCHEMATICS; seeder = ==('*'), reducer = p2_reducer)
println(p2)

function main(path)
SCHEMATICS = stack(readlines(path); dims=1)
println(seek_and_accumulate(SCHEMATICS; seeder = issymbol, reducer = sum))
println(seek_and_accumulate(SCHEMATICS; seeder = ==('*'), reducer = p2_reducer))
end

(abspath(PROGRAM_FILE) == @__FILE__) && main(ARGS[1])

0 comments on commit cbff437

Please sign in to comment.