Skip to content

Commit

Permalink
solve day 6 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
jromanowska committed Dec 7, 2023
1 parent ee131d4 commit 8e73bde
Show file tree
Hide file tree
Showing 9 changed files with 1,008 additions and 4 deletions.
2 changes: 2 additions & 0 deletions DATA/2023/input_day06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 58 81 96 76
Distance: 434 1041 2219 1218
75 changes: 75 additions & 0 deletions SOLUTIONS/2023/Day_06.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: "Day 6"
author: "Julia Romanowska"
jupyter: julia-1.9
---

```{julia}
#| label: setup
#| include: false
using Markdown;
```

## The input

The races' times and distances:

```{julia}
root = dirname(@__FILE__);
input_file = joinpath(root, "..", "..", "DATA", "2023", "input_day06.txt");
input_data = read(open(input_file, "r"), String);
println(input_data)
```

```{julia}
#| label: get_data
time_line , dist_line = split(input_data, "\n")[1:2]
times = split(time_line, r"\s+")[2:end] |>
x -> parse.(Int32, x)
distances = split(dist_line, r"\s+")[2:end] |>
x -> parse.(Int32, x)
```

## Part 1

### The problem

For each race, we can hold the button to charge the boat for varying number of
miliseconds but not exceed the maximum time given in the input file. With each
milisecond of holding the button, the boat gains 1ms/mm velocity.

```{julia}
#| label: function_max_dist
function find_max_dist(time_hold_button::Int64, max_time::Int32)
return (max_time - time_hold_button)*time_hold_button
end
```

### The solution

We need to find the number of ways we can get larger distance than what is in
the input file.

```{julia}
all_winning_approaches = Int32[];
for idx in 1:lastindex(times)
cur_max_time = times[idx];
cur_max_dist = distances[idx];
cur_winning_approaches = 0;
for t in 1:(cur_max_time - 1)
max_dist = find_max_dist(t, cur_max_time)
if max_dist > cur_max_dist
cur_winning_approaches += 1;
end
end
push!(all_winning_approaches, cur_winning_approaches);
end
```


```{julia}
#| echo: false
Markdown.parse("""
The total number of ways to win all the races: $(reduce(*, all_winning_approaches)).
""")
```
17 changes: 17 additions & 0 deletions SOLUTIONS/2023/Day_07.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: "Day 7"
author: "Julia Romanowska"
jupyter: julia-1.9
---

```{julia}
#| label: setup
#| include: false
using DataFrames;
using DataFramesMeta;
using CSV;
using Markdown;
```

## The input

4 changes: 2 additions & 2 deletions YEARS/2023.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ My take on the puzzles in 2023 edition of [Advent of Code](https://adventofcode.
[4](../SOLUTIONS/2023/Day_04.html)
:::
:::
::: {.card .border-success .mb-3}
::: {.card .border-warning .mb-3}
::: day
[5](../SOLUTIONS/2023/Day_05.html)
:::
Expand All @@ -35,7 +35,7 @@ My take on the puzzles in 2023 edition of [Advent of Code](https://adventofcode.
[6](../SOLUTIONS/2023/Day_06.html)
:::
:::
::: {.card .border-warning .mb-3}
::: {.card .border-success .mb-3}
::: day
[7](../SOLUTIONS/2023/Day_07.html)
:::
Expand Down
3 changes: 3 additions & 0 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ project:
render:
- "*.qmd"

execute:
freeze: auto

website:
title: "Advent_of_code"
site-path: "/docs/"
Expand Down
Loading

0 comments on commit 8e73bde

Please sign in to comment.