Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Advent of Coding
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Bohn
Advent of Coding
Commits
cc512ba5
Verified
Commit
cc512ba5
authored
1 year ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Completed 2023 Day 02
parent
d60a100c
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
2023/python/Day02.py
+83
-0
83 additions, 0 deletions
2023/python/Day02.py
with
83 additions
and
0 deletions
2023/python/Day02.py
0 → 100644
+
83
−
0
View file @
cc512ba5
from
enum
import
Enum
from
typing
import
List
,
Dict
from
ImportData
import
import_data
day
:
int
=
2
class
Color
(
Enum
):
RED
=
1
GREEN
=
2
BLUE
=
3
sample_data
:
List
[
str
]
=
'''
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
'''
.
split
(
'
\n
'
)[
1
:
-
1
]
data_structure
:
type
=
List
[
List
[
Dict
[
Color
,
int
]]]
def
parse_data
(
data
:
List
[
str
])
->
data_structure
:
games
:
List
[
List
[
Dict
[
Color
,
int
]]]
=
[]
expected_game_number
:
int
=
0
for
game
in
data
:
revelations
:
List
[
Dict
[
Color
,
int
]]
=
[]
game_identification
,
game_data
=
game
.
split
(
'
:
'
)
# validate my assumption that I can use a list without explicitly tracking the game number
expected_game_number
+=
1
actual_game_number
=
int
(
game_identification
.
split
(
'
'
)[
1
])
assert
expected_game_number
==
actual_game_number
# and now on with the show
for
reveal_data
in
game_data
.
split
(
'
;
'
):
revelation
:
Dict
[
Color
,
int
]
=
{
Color
.
RED
:
0
,
Color
.
GREEN
:
0
,
Color
.
BLUE
:
0
}
for
reveal_datum
in
reveal_data
.
split
(
'
,
'
):
number_of_cubes
=
int
(
reveal_datum
.
split
(
'
'
)[
0
])
color
=
getattr
(
Color
,
reveal_datum
.
split
(
'
'
)[
1
].
upper
())
revelation
[
color
]
=
number_of_cubes
revelations
.
append
(
revelation
)
games
.
append
(
revelations
)
return
games
def
is_possible
(
game
:
List
[
Dict
[
Color
,
int
]],
cubes
:
Dict
[
Color
,
int
])
->
bool
:
game_is_possible
:
bool
=
True
for
revelations
in
game
:
for
color
in
revelations
:
if
revelations
[
color
]
>
cubes
[
color
]:
game_is_possible
=
False
return
game_is_possible
def
part1
(
data
:
data_structure
)
->
int
:
score
:
int
=
0
cubes
:
Dict
[
Color
,
int
]
=
{
Color
.
RED
:
12
,
Color
.
GREEN
:
13
,
Color
.
BLUE
:
14
}
for
index
,
game
in
enumerate
(
data
):
if
is_possible
(
game
,
cubes
):
score
+=
index
+
1
return
score
def
fewest_cubes
(
game
:
List
[
Dict
[
Color
,
int
]])
->
Dict
[
Color
,
int
]:
reds
:
List
[
int
]
=
[
revelations
[
Color
.
RED
]
for
revelations
in
game
]
greens
:
List
[
int
]
=
[
revelations
[
Color
.
GREEN
]
for
revelations
in
game
]
blues
:
List
[
int
]
=
[
revelations
[
Color
.
BLUE
]
for
revelations
in
game
]
return
{
Color
.
RED
:
max
(
reds
),
Color
.
GREEN
:
max
(
greens
),
Color
.
BLUE
:
max
(
blues
)}
def
part2
(
data
:
data_structure
)
->
int
:
powers
=
[
cubes
[
Color
.
RED
]
*
cubes
[
Color
.
GREEN
]
*
cubes
[
Color
.
BLUE
]
for
cubes
in
[
fewest_cubes
(
game
)
for
game
in
data
]]
return
sum
(
powers
)
if
__name__
==
'
__main__
'
:
production_ready
=
True
raw_data
=
import_data
(
day
)
if
production_ready
else
sample_data
print
(
part1
(
parse_data
(
raw_data
)))
print
(
part2
(
parse_data
(
raw_data
)))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment